All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp.ApiClient.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
{{>partial_header}}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.IO;
{{^supportsUWP}}
using System.Web;
{{/supportsUWP}}
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using RestSharp;

namespace {{packageName}}.Client
{
    /// 
    /// 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
        };

        /// 
        /// 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 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.
        [Obsolete("ApiClient.Default is deprecated, please use 'Configuration.Default.ApiClient' instead.")]
        public static ApiClient 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, 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)
            {
                {{^supportsUWP}}
                request.AddFile(param.Value.Name, param.Value.Writer, param.Value.FileName, param.Value.ContentType);
                {{/supportsUWP}}
                {{#supportsUWP}}
                byte[] paramWriter = null;
                param.Value.Writer = delegate (Stream stream) { paramWriter = ToByteArray(stream); };
                request.AddFile(param.Value.Name, paramWriter, param.Value.FileName, param.Value.ContentType);
                {{/supportsUWP}}
            }

            if (postBody != null) // http body (model or byte[]) parameter
            {
                if (postBody.GetType() == typeof(String))
                {
                    request.AddParameter("application/json", postBody, ParameterType.RequestBody);
                }
                else if (postBody.GetType() == typeof(byte[]))
                {
                    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, Dictionary 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);
            {{^supportsUWP}}
            var response = RestClient.Execute(request);
            {{/supportsUWP}}
            {{#supportsUWP}}
            // Using async method to perform sync call (uwp-only)
            var response = RestClient.ExecuteTaskAsync(request).Result;
            {{/supportsUWP}}
            InterceptResponse(request, response);

            return (Object) response;
        }
        {{#supportsAsync}}
        /// 
        /// 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, Dictionary 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.ExecuteTaskAsync(request);
            InterceptResponse(request, response);
            return (Object)response;
        }{{/supportsAsync}}

        /// 
        /// 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;
            }

            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);
            }
        }

        /// 
        /// Select the Content-Type header's value from the given content-type array:
        /// if JSON 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 null;

            if (contentTypes.Contains("application/json", StringComparer.OrdinalIgnoreCase))
                return "application/json";

            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.
        /// Ref: http://stackoverflow.com/questions/4925718/c-dynamic-runtime-cast
        /// 
        /// Object to be casted
        /// Target type
        /// Casted object
        {{#supportsAsync}}public static dynamic ConvertType(dynamic source, Type dest){{/supportsAsync}}{{^supportsAsync}}public static object ConvertType(T source, Type dest) where T : class{{/supportsAsync}}
        {
            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();
        }

        /// 
        /// 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;
            }
        }
        {{#supportsUWP}}
        /// 
        /// Convert stream to byte array
        /// 
        /// IO stream
        /// Byte array
        public static byte[] ToByteArray(Stream stream)
        {
            stream.Position = 0;
            byte[] buffer = new byte[stream.Length];
            for (int totalBytesCopied = 0; totalBytesCopied < stream.Length;)
                totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
            return buffer;
        }
        {{/supportsUWP}}
    }
}