/* * Service Collaborateur API * * Api du service Collaborateur, utilisée pour récupérer les données des collaborateurs d'Apside * * OpenAPI spec version: 1.1.3 * Contact: lilian.gayet@apside-groupe.com * Generated by: https://github.com/swagger-api/swagger-codegen.git */ using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Text.RegularExpressions; using System.IO; using System.Web; using System.Linq; using System.Net; using System.Text; using Newtonsoft.Json; using RestSharp; namespace IO.Swagger.ClientCollaborateur { /// /// API client is mainly responsible for making the HTTP call to the API backend. /// public partial class ApiClient { private JsonSerializerSettings serializerSettings = new JsonSerializerSettings { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, DateFormatString = "yyyy/MM/dd" }; /// /// Allows for extending request processing for generated code. /// /// The RestSharp request object partial void InterceptRequest(IRestRequest request); /// /// Allows for extending response processing for generated code. /// /// The RestSharp request object /// The RestSharp response object partial void InterceptResponse(IRestRequest request, IRestResponse response); /// /// Initializes a new instance of the class /// with default configuration. /// public ApiClient() { Configuration = IO.Swagger.ClientCollaborateur.Configuration.Default; RestClient = new RestClient("http://localhost:3000"); } /// /// Initializes a new instance of the class /// with default base path (http://localhost:3000). /// /// An instance of Configuration. public ApiClient(Configuration config) { Configuration = config ?? IO.Swagger.ClientCollaborateur.Configuration.Default; RestClient = new RestClient(Configuration.BasePath); } /// /// Initializes a new instance of the class /// with default configuration. /// /// The base path. public ApiClient(String basePath = "http://localhost:3000") { if (String.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty"); RestClient = new RestClient(basePath); Configuration = ClientCollaborateur.Configuration.Default; } /// /// Gets or sets the default API client for making HTTP calls. /// /// The default API client. [Obsolete("ApiClient.Default is deprecated, please use 'Configuration.Default.ApiClient' instead.")] public static ApiClient Default; /// /// Gets or sets an instance of the IReadableConfiguration. /// /// An instance of the IReadableConfiguration. /// /// helps us to avoid modifying possibly global /// configuration values from within a given client. It does not guarantee thread-safety /// of the instance in any way. /// public IReadableConfiguration Configuration { get; set; } /// /// Gets or sets the RestClient. /// /// An instance of the RestClient public RestClient RestClient { get; set; } // Creates and sets up a RestRequest prior to a call. private RestRequest PrepareRequest( String path, RestSharp.Method method, List> queryParams, Object postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams, String contentType) { var request = new RestRequest(path, method); // add path parameter, if any foreach (var param in pathParams) request.AddParameter(param.Key, param.Value, ParameterType.UrlSegment); // add header parameter, if any foreach (var param in headerParams) request.AddHeader(param.Key, param.Value); // add query parameter, if any foreach (var param in queryParams) request.AddQueryParameter(param.Key, param.Value); // add form parameter, if any foreach (var param in formParams) request.AddParameter(param.Key, param.Value); // add file parameter, if any foreach (var param in fileParams) { request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentLength, param.Value.ContentType); } if (postBody != null) // http body (model or byte[]) parameter { request.AddParameter(contentType, postBody, ParameterType.RequestBody); } return request; } /// /// Makes the HTTP request (Sync). /// /// URL path. /// HTTP method. /// Query parameters. /// HTTP body (POST request). /// Header parameters. /// Form parameters. /// File parameters. /// Path parameters. /// Content Type of the request /// Object public Object CallApi( String path, RestSharp.Method method, List> queryParams, Object postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams, String contentType) { var request = PrepareRequest( path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams, contentType); // set timeout RestClient.Timeout = Configuration.Timeout; // set user agent RestClient.UserAgent = Configuration.UserAgent; InterceptRequest(request); var response = RestClient.Execute(request); InterceptResponse(request, response); return (Object)response; } /// /// Makes the asynchronous HTTP request. /// /// URL path. /// HTTP method. /// Query parameters. /// HTTP body (POST request). /// Header parameters. /// Form parameters. /// File parameters. /// Path parameters. /// Content type. /// The Task instance. public async System.Threading.Tasks.Task CallApiAsync( String path, RestSharp.Method method, List> queryParams, Object postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams, String contentType) { var request = PrepareRequest( path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams, contentType); InterceptRequest(request); var response = await RestClient.ExecuteAsync(request); //var response = await RestClient.ExecuteAsync(request); InterceptResponse(request, response); return (Object)response; } /// /// Escape string (url-encoded). /// /// String to be escaped. /// Escaped string. public string EscapeString(string str) { return UrlEncode(str); } /// /// Create FileParameter based on Stream. /// /// Parameter name. /// Input stream. /// FileParameter. public FileParameter ParameterToFile(string name, Stream stream) { if (stream is FileStream) return FileParameter.Create(name, ReadAsBytes(stream), Path.GetFileName(((FileStream)stream).Name)); else return FileParameter.Create(name, ReadAsBytes(stream), "no_file_name_provided"); } /// /// If parameter is DateTime, output in a formatted string (default ISO 8601), customizable with Configuration.DateTime. /// If parameter is a list, join the list with ",". /// Otherwise just return the string. /// /// The parameter (header, path, query, form). /// Formatted string. public string ParameterToString(object obj) { if (obj is DateTime) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 return ((DateTime)obj).ToString(Configuration.DateTimeFormat); else if (obj is DateTimeOffset) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 return ((DateTimeOffset)obj).ToString(Configuration.DateTimeFormat); else if (obj is IList) { var flattenedString = new StringBuilder(); foreach (var param in (IList)obj) { if (flattenedString.Length > 0) flattenedString.Append(","); flattenedString.Append(param); } return flattenedString.ToString(); } else return Convert.ToString(obj); } /// /// Deserialize the JSON string into a proper object. /// /// The HTTP response. /// Object type. /// Object representation of the JSON string. public object Deserialize(IRestResponse response, Type type) { IList headers = response.Headers; if (type == typeof(byte[])) // return byte array { return response.RawBytes; } // TODO: ? if (type.IsAssignableFrom(typeof(Stream))) if (type == typeof(Stream)) { if (headers != null) { var filePath = String.IsNullOrEmpty(Configuration.TempFolderPath) ? Path.GetTempPath() : Configuration.TempFolderPath; var regex = new Regex(@"Content-Disposition=.*filename=['""]?([^'""\s]+)['""]?$"); foreach (var header in headers) { var match = regex.Match(header.ToString()); if (match.Success) { string fileName = filePath + SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", "")); File.WriteAllBytes(fileName, response.RawBytes); return new FileStream(fileName, FileMode.Open); } } } var stream = new MemoryStream(response.RawBytes); return stream; } if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object { return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind); } if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type { return ConvertType(response.Content, type); } // at this point, it must be a model (json) try { return JsonConvert.DeserializeObject(response.Content, type, serializerSettings); } catch (Exception e) { throw new ApiException(500, e.Message); } } /// /// Serialize an input (model) into JSON string /// /// Object. /// JSON string. public String Serialize(object obj) { try { return obj != null ? JsonConvert.SerializeObject(obj) : null; } catch (Exception e) { throw new ApiException(500, e.Message); } } /// ///Check if the given MIME is a JSON MIME. ///JSON MIME examples: /// application/json /// application/json; charset=UTF8 /// APPLICATION/JSON /// application/vnd.company+json /// /// MIME /// Returns True if MIME type is json. public bool IsJsonMime(String mime) { var jsonRegex = new Regex("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$"); return mime != null && (jsonRegex.IsMatch(mime) || mime.Equals("application/json-patch+json")); } /// /// Select the Content-Type header's value from the given content-type array: /// if JSON type exists in the given array, use it; /// otherwise use the first one defined in 'consumes' /// /// The Content-Type array to select from. /// The Content-Type header to use. public String SelectHeaderContentType(String[] contentTypes) { if (contentTypes.Length == 0) return "application/json"; foreach (var contentType in contentTypes) { if (IsJsonMime(contentType.ToLower())) return contentType; } return contentTypes[0]; // use the first content type specified in 'consumes' } /// /// Select the Accept header's value from the given accepts array: /// if JSON exists in the given array, use it; /// otherwise use all of them (joining into a string) /// /// The accepts array to select from. /// The Accept header to use. public String SelectHeaderAccept(String[] accepts) { if (accepts.Length == 0) return null; if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase)) return "application/json"; return String.Join(",", accepts); } /// /// Encode string in base64 format. /// /// String to be encoded. /// Encoded string. public static string Base64Encode(string text) { return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// /// Dynamically cast the object into target type. /// /// Object to be casted /// Target type /// Casted object public static dynamic ConvertType(dynamic fromObject, Type toObject) { return Convert.ChangeType(fromObject, toObject); } /// /// Convert stream to byte array /// /// Input stream to be converted /// Byte array public static byte[] ReadAsBytes(Stream inputStream) { byte[] buf = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int count; while ((count = inputStream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, count); } return ms.ToArray(); } } /// /// URL encode a string /// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50 /// /// String to be URL encoded /// Byte array public static string UrlEncode(string input) { const int maxLength = 32766; if (input == null) { throw new ArgumentNullException("input"); } if (input.Length <= maxLength) { return Uri.EscapeDataString(input); } StringBuilder sb = new StringBuilder(input.Length * 2); int index = 0; while (index < input.Length) { int length = Math.Min(input.Length - index, maxLength); string subString = input.Substring(index, length); sb.Append(Uri.EscapeDataString(subString)); index += subString.Length; } return sb.ToString(); } /// /// Sanitize filename by removing the path /// /// Filename /// Filename public static string SanitizeFilename(string filename) { Match match = Regex.Match(filename, @".*[/\\](.*)$"); if (match.Success) { return match.Groups[1].Value; } else { return filename; } } /// /// Convert params to key/value pairs. /// Use collectionFormat to properly format lists and collections. /// /// Key name. /// Value object. /// A list of KeyValuePairs public IEnumerable> ParameterToKeyValuePairs(string collectionFormat, string name, object value) { var parameters = new List>(); if (IsCollection(value) && collectionFormat == "multi") { var valueCollection = value as IEnumerable; parameters.AddRange(from object item in valueCollection select new KeyValuePair(name, ParameterToString(item))); } else { parameters.Add(new KeyValuePair(name, ParameterToString(value))); } return parameters; } /// /// Check if generic object is a collection. /// /// /// True if object is a collection type private static bool IsCollection(object value) { return value is IList || value is ICollection; } } }