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: 7.6.0
Show newest version
{{>partial_header}}

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
{{^netStandard}}
using System.Web;
{{/netStandard}}
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using RestSharp;
using RestSharp.Serializers;
using RestSharpMethod = RestSharp.Method;
{{#supportsRetry}}
using Polly;
{{/supportsRetry}}
{{#hasOAuthMethods}}
using {{packageName}}.Client.Auth;
{{/hasOAuthMethods}}

namespace {{packageName}}.Client
{
    /// 
    /// Allows RestSharp to Serialize/Deserialize JSON using our custom logic, but only when ContentType is JSON.
    /// 
    internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer
    {
        private readonly IReadableConfiguration _configuration;
        private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings
        {
            // OpenAPI generated types generally hide default constructors.
            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
            ContractResolver = new DefaultContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy
                {
                    OverrideSpecifiedNames = false
                }
            }
        };

        public CustomJsonCodec(IReadableConfiguration configuration)
        {
            _configuration = configuration;
        }

        public CustomJsonCodec(JsonSerializerSettings serializerSettings, IReadableConfiguration configuration)
        {
            _serializerSettings = serializerSettings;
            _configuration = configuration;
        }

        /// 
        /// Serialize the object into a JSON string.
        /// 
        /// Object to be serialized.
        /// A JSON string.
        public string Serialize(object obj)
        {
            if (obj != null && obj is {{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema)
            {
                // the object to be serialized is an oneOf/anyOf schema
                return (({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema)obj).ToJson();
            }
            else
            {
                return JsonConvert.SerializeObject(obj, _serializerSettings);
            }
        }

        public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);

        public T Deserialize(RestResponse response)
        {
            var result = (T)Deserialize(response, typeof(T));
            return result;
        }

        /// 
        /// Deserialize the JSON string into a proper object.
        /// 
        /// The HTTP response.
        /// Object type.
        /// Object representation of the JSON string.
        internal object Deserialize(RestResponse response, Type type)
        {
            if (type == typeof(byte[])) // return byte array
            {
                return response.RawBytes;
            }

            // TODO: ? if (type.IsAssignableFrom(typeof(Stream)))
            if (type == typeof(Stream))
            {
                var bytes = response.RawBytes;
                if (response.Headers != null)
                {
                    var filePath = string.IsNullOrEmpty(_configuration.TempFolderPath)
                        ? Path.GetTempPath()
                        : _configuration.TempFolderPath;
                    var regex = new Regex(@"Content-Disposition=.*filename=['""]?([^'""\s]+)['""]?$");
                    foreach (var header in response.Headers)
                    {
                        var match = regex.Match(header.ToString());
                        if (match.Success)
                        {
                            string fileName = filePath + ClientUtils.SanitizeFilename(match.Groups[1].Value.Replace("\"", "").Replace("'", ""));
                            File.WriteAllBytes(fileName, bytes);
                            return new FileStream(fileName, FileMode.Open);
                        }
                    }
                }
                var stream = new MemoryStream(bytes);
                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 Convert.ChangeType(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);
            }
        }

        public ISerializer Serializer => this;
        public IDeserializer Deserializer => this;

        public string[] AcceptedContentTypes => RestSharp.ContentType.JsonAccept;

        public SupportsContentType SupportsContentType => contentType =>
            contentType.Value.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) ||
            contentType.Value.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase);

        public ContentType ContentType { get; set; } = RestSharp.ContentType.Json;

        public DataFormat DataFormat => DataFormat.Json;
    }
    {{! NOTE: Any changes related to RestSharp should be done in this class. All other client classes are for extensibility by consumers.}}
    /// 
    /// Provides a default implementation of an Api client (both synchronous and asynchronous implementations),
    /// encapsulating general REST accessor use cases.
    /// 
    {{>visibility}} partial class ApiClient : ISynchronousClient{{#supportsAsync}}, IAsynchronousClient{{/supportsAsync}}
    {
        private readonly string _baseUrl;

        /// 
        /// Specifies the settings on a  object.
        /// These settings can be adjusted to accommodate custom serialization rules.
        /// 
        public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings
        {
            // OpenAPI generated types generally hide default constructors.
            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor,
            ContractResolver = new DefaultContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy
                {
                    OverrideSpecifiedNames = false
                }
            }
        };

        /// 
        /// Allows for extending request processing for  generated code.
        /// 
        /// The RestSharp request object
        partial void InterceptRequest(RestRequest request);

        /// 
        /// Allows for extending response processing for  generated code.
        /// 
        /// The RestSharp request object
        /// The RestSharp response object
        partial void InterceptResponse(RestRequest request, RestResponse response);

        /// 
        /// Initializes a new instance of the , defaulting to the global configurations' base url.
        /// 
        public ApiClient()
        {
            _baseUrl = {{packageName}}.Client.GlobalConfiguration.Instance.BasePath;
        }

        /// 
        /// Initializes a new instance of the 
        /// 
        /// The target service's base path in URL format.
        /// 
        public ApiClient(string basePath)
        {
            if (string.IsNullOrEmpty(basePath))
                throw new ArgumentException("basePath cannot be empty");

            _baseUrl = basePath;
        }

        /// 
        /// Constructs the RestSharp version of an http method
        /// 
        /// Swagger Client Custom HttpMethod
        /// RestSharp's HttpMethod instance.
        /// 
        private RestSharpMethod Method(HttpMethod method)
        {
            RestSharpMethod other;
            switch (method)
            {
                case HttpMethod.Get:
                    other = RestSharpMethod.Get;
                    break;
                case HttpMethod.Post:
                    other = RestSharpMethod.Post;
                    break;
                case HttpMethod.Put:
                    other = RestSharpMethod.Put;
                    break;
                case HttpMethod.Delete:
                    other = RestSharpMethod.Delete;
                    break;
                case HttpMethod.Head:
                    other = RestSharpMethod.Head;
                    break;
                case HttpMethod.Options:
                    other = RestSharpMethod.Options;
                    break;
                case HttpMethod.Patch:
                    other = RestSharpMethod.Patch;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("method", method, null);
            }

            return other;
        }

        /// 
        /// Provides all logic for constructing a new RestSharp .
        /// At this point, all information for querying the service is known. Here, it is simply
        /// mapped into the RestSharp request.
        /// 
        /// The http verb.
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// [private] A new RestRequest instance.
        /// 
        private RestRequest NewRequest(
            HttpMethod method,
            string path,
            RequestOptions options,
            IReadableConfiguration configuration)
        {
            if (path == null) throw new ArgumentNullException("path");
            if (options == null) throw new ArgumentNullException("options");
            if (configuration == null) throw new ArgumentNullException("configuration");

            RestRequest request = new RestRequest(path, Method(method));

            if (options.PathParameters != null)
            {
                foreach (var pathParam in options.PathParameters)
                {
                    request.AddParameter(pathParam.Key, pathParam.Value, ParameterType.UrlSegment);
                }
            }

            if (options.QueryParameters != null)
            {
                foreach (var queryParam in options.QueryParameters)
                {
                    foreach (var value in queryParam.Value)
                    {
                        request.AddQueryParameter(queryParam.Key, value);
                    }
                }
            }

            if (configuration.DefaultHeaders != null)
            {
                foreach (var headerParam in configuration.DefaultHeaders)
                {
                    request.AddHeader(headerParam.Key, headerParam.Value);
                }
            }

            if (options.HeaderParameters != null)
            {
                foreach (var headerParam in options.HeaderParameters)
                {
                    foreach (var value in headerParam.Value)
                    {
                        request.AddHeader(headerParam.Key, value);
                    }
                }
            }

            if (options.FormParameters != null)
            {
                foreach (var formParam in options.FormParameters)
                {
                    request.AddParameter(formParam.Key, formParam.Value);
                }
            }

            if (options.Data != null)
            {
                if (options.Data is Stream stream)
                {
                    var contentType = "application/octet-stream";
                    if (options.HeaderParameters != null)
                    {
                        var contentTypes = options.HeaderParameters["Content-Type"];
                        contentType = contentTypes[0];
                    }

                    var bytes = ClientUtils.ReadAsBytes(stream);
                    request.AddParameter(contentType, bytes, ParameterType.RequestBody);
                }
                else
                {
                    if (options.HeaderParameters != null)
                    {
                        var contentTypes = options.HeaderParameters["Content-Type"];
                        if (contentTypes == null || contentTypes.Any(header => header.Contains("application/json")))
                        {
                            request.RequestFormat = DataFormat.Json;
                        }
                        else
                        {
                            // TODO: Generated client user should add additional handlers. RestSharp only supports XML and JSON, with XML as default.
                        }
                    }
                    else
                    {
                        // Here, we'll assume JSON APIs are more common. XML can be forced by adding produces/consumes to openapi spec explicitly.
                        request.RequestFormat = DataFormat.Json;
                    }

                    request.AddJsonBody(options.Data);
                }
            }

            if (options.FileParameters != null)
            {
                foreach (var fileParam in options.FileParameters)
                {
                    foreach (var file in fileParam.Value)
                    {
                        var bytes = ClientUtils.ReadAsBytes(file);
                        var fileStream = file as FileStream;
                        if (fileStream != null)
                            request.AddFile(fileParam.Key, bytes, System.IO.Path.GetFileName(fileStream.Name));
                        else
                            request.AddFile(fileParam.Key, bytes, "no_file_name_provided");
                    }
                }
            }

            return request;
        }

        private ApiResponse ToApiResponse(RestResponse response)
        {
            T result = response.Data;
            string rawContent = response.Content;

            var transformed = new ApiResponse(response.StatusCode, new Multimap({{#caseInsensitiveResponseHeaders}}StringComparer.OrdinalIgnoreCase{{/caseInsensitiveResponseHeaders}}), result, rawContent)
            {
                ErrorText = response.ErrorMessage,
                Cookies = new List()
            };

            if (response.Headers != null)
            {
                foreach (var responseHeader in response.Headers)
                {
                    transformed.Headers.Add(responseHeader.Name, ClientUtils.ParameterToString(responseHeader.Value));
                }
            }

            if (response.ContentHeaders != null)
            {
                foreach (var responseHeader in response.ContentHeaders)
                {
                    transformed.Headers.Add(responseHeader.Name, ClientUtils.ParameterToString(responseHeader.Value));
                }
            }

            if (response.Cookies != null)
            {
                foreach (var responseCookies in response.Cookies.Cast())
                {
                    transformed.Cookies.Add(
                        new Cookie(
                            responseCookies.Name,
                            responseCookies.Value,
                            responseCookies.Path,
                            responseCookies.Domain)
                        );
                }
            }

            return transformed;
        }

        private ApiResponse Exec(RestRequest request, RequestOptions options, IReadableConfiguration configuration)
        {
            var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;

            var cookies = new CookieContainer();

            if (options.Cookies != null && options.Cookies.Count > 0)
            {
                foreach (var cookie in options.Cookies)
                {
                    cookies.Add(new Cookie(cookie.Name, cookie.Value));
                }
            }

            var clientOptions = new RestClientOptions(baseUrl)
            {
                ClientCertificates = configuration.ClientCertificates,
                CookieContainer = cookies,
                MaxTimeout = configuration.Timeout,
                Proxy = configuration.Proxy,
                UserAgent = configuration.UserAgent,
                UseDefaultCredentials = configuration.UseDefaultCredentials,
                RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
            };

            {{#hasOAuthMethods}}
            if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
                !string.IsNullOrEmpty(configuration.OAuthClientId) &&
                !string.IsNullOrEmpty(configuration.OAuthClientSecret) &&
                configuration.OAuthFlow != null)
            {
                clientOptions.Authenticator = new OAuthAuthenticator(
                    configuration.OAuthTokenUrl,
                    configuration.OAuthClientId,
                    configuration.OAuthClientSecret,
                    configuration.OAuthFlow,
                    SerializerSettings,
                    configuration);
            }

            {{/hasOAuthMethods}}
            using (RestClient client = new RestClient(clientOptions,
                configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration))))
            {
                InterceptRequest(request);

                RestResponse response;
                if (RetryConfiguration.RetryPolicy != null)
                {
                    var policy = RetryConfiguration.RetryPolicy;
                    var policyResult = policy.ExecuteAndCapture(() => client.Execute(request));
                    response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize(policyResult.Result) : new RestResponse(request)
                    {
                        ErrorException = policyResult.FinalException
                    };
                }
                else
                {
                    response = client.Execute(request);
                }

                // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
                if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
                {
                    try
                    {
                        response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
                    }
                    catch (Exception ex)
                    {
                        throw ex.InnerException != null ? ex.InnerException : ex;
                    }
                }
                else if (typeof(T).Name == "Stream") // for binary response
                {
                    response.Data = (T)(object)new MemoryStream(response.RawBytes);
                }
                else if (typeof(T).Name == "Byte[]") // for byte response
                {
                    response.Data = (T)(object)response.RawBytes;
                }
                else if (typeof(T).Name == "String") // for string response
                {
                    response.Data = (T)(object)response.Content;
                }

                InterceptResponse(request, response);

                var result = ToApiResponse(response);
                if (response.ErrorMessage != null)
                {
                    result.ErrorText = response.ErrorMessage;
                }

                if (response.Cookies != null && response.Cookies.Count > 0)
                {
                    if (result.Cookies == null) result.Cookies = new List();
                    foreach (var restResponseCookie in response.Cookies.Cast())
                    {
                        var cookie = new Cookie(
                            restResponseCookie.Name,
                            restResponseCookie.Value,
                            restResponseCookie.Path,
                            restResponseCookie.Domain
                        )
                        {
                            Comment = restResponseCookie.Comment,
                            CommentUri = restResponseCookie.CommentUri,
                            Discard = restResponseCookie.Discard,
                            Expired = restResponseCookie.Expired,
                            Expires = restResponseCookie.Expires,
                            HttpOnly = restResponseCookie.HttpOnly,
                            Port = restResponseCookie.Port,
                            Secure = restResponseCookie.Secure,
                            Version = restResponseCookie.Version
                        };

                        result.Cookies.Add(cookie);
                    }
                }
                return result;
            }
        }

        {{#supportsAsync}}
        private async Task> ExecAsync(RestRequest request, RequestOptions options, IReadableConfiguration configuration, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl;

            var clientOptions = new RestClientOptions(baseUrl)
            {
                ClientCertificates = configuration.ClientCertificates,
                MaxTimeout = configuration.Timeout,
                Proxy = configuration.Proxy,
                UserAgent = configuration.UserAgent,
                UseDefaultCredentials = configuration.UseDefaultCredentials,
                RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback
            };

            {{#hasOAuthMethods}}
            if (!string.IsNullOrEmpty(configuration.OAuthTokenUrl) &&
                !string.IsNullOrEmpty(configuration.OAuthClientId) &&
                !string.IsNullOrEmpty(configuration.OAuthClientSecret) &&
                configuration.OAuthFlow != null)
            {
                clientOptions.Authenticator = new OAuthAuthenticator(
                    configuration.OAuthTokenUrl,
                    configuration.OAuthClientId,
                    configuration.OAuthClientSecret,
                    configuration.OAuthFlow,
                    SerializerSettings,
                    configuration);
            }

            {{/hasOAuthMethods}}
            using (RestClient client = new RestClient(clientOptions,
                configureSerialization: serializerConfig => serializerConfig.UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration))))
            {
                InterceptRequest(request);

                RestResponse response;
            {{#supportsRetry}}
                if (RetryConfiguration.AsyncRetryPolicy != null)
                {
                    var policy = RetryConfiguration.AsyncRetryPolicy;
                    var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(request, ct), cancellationToken).ConfigureAwait(false);
                    response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize(policyResult.Result) : new RestResponse(request)
                    {
                        ErrorException = policyResult.FinalException
                    };
                }
                else
                {
            {{/supportsRetry}}
                    response = await client.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
            {{#supportsRetry}}
                }
            {{/supportsRetry}}

                // if the response type is oneOf/anyOf, call FromJSON to deserialize the data
                if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
                {
                    response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
                }
                else if (typeof(T).Name == "Stream") // for binary response
                {
                    response.Data = (T)(object)new MemoryStream(response.RawBytes);
                }
                else if (typeof(T).Name == "Byte[]") // for byte response
                {
                    response.Data = (T)(object)response.RawBytes;
                }

                InterceptResponse(request, response);

                var result = ToApiResponse(response);
                if (response.ErrorMessage != null)
                {
                    result.ErrorText = response.ErrorMessage;
                }

                if (response.Cookies != null && response.Cookies.Count > 0)
                {
                    if (result.Cookies == null) result.Cookies = new List();
                    foreach (var restResponseCookie in response.Cookies.Cast())
                    {
                        var cookie = new Cookie(
                            restResponseCookie.Name,
                            restResponseCookie.Value,
                            restResponseCookie.Path,
                            restResponseCookie.Domain
                        )
                        {
                            Comment = restResponseCookie.Comment,
                            CommentUri = restResponseCookie.CommentUri,
                            Discard = restResponseCookie.Discard,
                            Expired = restResponseCookie.Expired,
                            Expires = restResponseCookie.Expires,
                            HttpOnly = restResponseCookie.HttpOnly,
                            Port = restResponseCookie.Port,
                            Secure = restResponseCookie.Secure,
                            Version = restResponseCookie.Version
                        };

                        result.Cookies.Add(cookie);
                    }
                }
                return result;
            }
        }

        #region IAsynchronousClient
        /// 
        /// Make a HTTP GET request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> GetAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Get, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP POST request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> PostAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Post, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP PUT request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> PutAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Put, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP DELETE request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> DeleteAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Delete, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP HEAD request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> HeadAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Head, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP OPTION request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> OptionsAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Options, path, options, config), options, config, cancellationToken);
        }

        /// 
        /// Make a HTTP PATCH request (async).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// Token that enables callers to cancel the request.
        /// A Task containing ApiResponse
        public Task> PatchAsync(string path, RequestOptions options, IReadableConfiguration configuration = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return ExecAsync(NewRequest(HttpMethod.Patch, path, options, config), options, config, cancellationToken);
        }
        #endregion IAsynchronousClient
        {{/supportsAsync}}

        #region ISynchronousClient
        /// 
        /// Make a HTTP GET request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Get(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Get, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP POST request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Post(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Post, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP PUT request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Put(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Put, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP DELETE request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Delete(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Delete, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP HEAD request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Head(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Head, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP OPTION request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Options(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Options, path, options, config), options, config);
        }

        /// 
        /// Make a HTTP PATCH request (synchronous).
        /// 
        /// The target path (or resource).
        /// The additional request options.
        /// A per-request configuration object. It is assumed that any merge with
        /// GlobalConfiguration has been done before calling this method.
        /// A Task containing ApiResponse
        public ApiResponse Patch(string path, RequestOptions options, IReadableConfiguration configuration = null)
        {
            var config = configuration ?? GlobalConfiguration.Instance;
            return Exec(NewRequest(HttpMethod.Patch, path, options, config), options, config);
        }
        #endregion ISynchronousClient
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy