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 {{packageName}}.Client { /// /// API client is mainly responible for making the HTTP call to the API backend. /// public class ApiClient { /// /// Initializes a new instance of the class /// with default configuration and base path ({{basePath}}). /// public ApiClient() { Configuration = Configuration.Default; RestClient = new RestClient("{{basePath}}"); } /// /// Initializes a new instance of the class /// with default base path ({{basePath}}). /// /// An instance of Configuration. public ApiClient(Configuration config = null) { if (config == null) Configuration = Configuration.Default; else Configuration = config; RestClient = new RestClient("{{basePath}}"); } /// /// Initializes a new instance of the class /// with default configuration. /// /// The base path. public ApiClient(String basePath = "{{basePath}}") { if (String.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty"); RestClient = new RestClient(basePath); Configuration = Configuration.Default; } /// /// Gets or sets the default API client for making HTTP calls. /// /// The default API client. public static ApiClient Default = new ApiClient(Configuration.Default); /// /// Gets or sets the Configuration. /// /// An instance of the Configuration. public Configuration 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, Dictionary queryParams, String postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams) { 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.ContentType); if (postBody != null) // http body (model) parameter request.AddParameter("application/json", 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. /// Object public Object CallApi( String path, RestSharp.Method method, Dictionary queryParams, String postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams) { var request = PrepareRequest( path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams); var response = RestClient.Execute(request); 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. /// The Task instance. public async System.Threading.Tasks.Task CallApiAsync( String path, RestSharp.Method method, Dictionary queryParams, String postBody, Dictionary headerParams, Dictionary formParams, Dictionary fileParams, Dictionary pathParams) { var request = PrepareRequest( path, method, queryParams, postBody, headerParams, formParams, fileParams, pathParams); var response = await RestClient.ExecuteTaskAsync(request); 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 IList) { string flattenString = ""; string separator = ","; foreach (var param in (IList)obj) { flattenString += param.ToString() + separator; } return flattenString.Remove(flattenString.Length - 1);; } 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) { byte[] data = response.RawBytes; string content = response.Content; IList headers = response.Headers; if (type == typeof(Object)) // return an object { return content; } 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]+)['""]?$"); var match = regex.Match(headers.ToString()); if (match.Success) { string fileName = filePath + match.Value.Replace("\"", "").Replace("'", ""); File.WriteAllBytes(fileName, data); return new FileStream(fileName, FileMode.Open); } } var stream = new MemoryStream(data); return stream; } if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object { return DateTime.Parse(content, null, System.Globalization.DateTimeStyles.RoundtripKind); } if (type == typeof(String) || type.Name.StartsWith("System.Nullable")) // return primitive type { return ConvertType(content, type); } // at this point, it must be a model (json) try { return JsonConvert.DeserializeObject(content, type); } catch (Exception e) { throw new ApiException(500, e.Message); } } /// /// Serialize an object 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); } } /// /// 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. /// Ref: http://stackoverflow.com/questions/4925718/c-dynamic-runtime-cast /// /// Object to be casted /// Target type /// Casted object public static dynamic ConvertType(dynamic source, Type dest) { return Convert.ChangeType(source, dest); } /// /// Convert stream to byte array /// Credit/Ref: http://stackoverflow.com/a/221941/677735 /// /// Input stream to be converted /// Byte array public static byte[] ReadAsBytes(Stream input) { byte[] buffer = new byte[16*1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } 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(); } } }