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

csharp.Configuration.mustache Maven / Gradle / Ivy

The newest version!
{{>partial_header}}
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace {{packageName}}.Client
{
    /// 
    /// Represents a set of configuration settings
    /// 
    public class Configuration
    {
        /// 
        /// Initializes a new instance of the Configuration class with different settings
        /// 
        /// Api client
        /// Dictionary of default HTTP header
        /// Username
        /// Password
        /// accessToken
        /// Dictionary of API key
        /// Dictionary of API key prefix
        /// Temp folder path
        /// DateTime format string
        /// HTTP connection timeout (in milliseconds)
        /// HTTP user agent
        public Configuration(ApiClient apiClient = null,
                             Dictionary defaultHeader = null,
                             string username = null,
                             string password = null,
                             string accessToken = null,
                             Dictionary apiKey = null,
                             Dictionary apiKeyPrefix = null,
                             string tempFolderPath = null,
                             string dateTimeFormat = null,
                             int timeout = 100000,
                             string userAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{packageVersion}}/csharp{{/httpUserAgent}}"
                            )
        {
            setApiClientUsingDefault(apiClient);

            Username = username;
            Password = password;
            AccessToken = accessToken;
            UserAgent = userAgent;

            if (defaultHeader != null)
                DefaultHeader = defaultHeader;
            if (apiKey != null)
                ApiKey = apiKey;
            if (apiKeyPrefix != null)
                ApiKeyPrefix = apiKeyPrefix;

            TempFolderPath = tempFolderPath;
            DateTimeFormat = dateTimeFormat;
            Timeout = timeout;
        }

        /// 
        /// Initializes a new instance of the Configuration class.
        /// 
        /// Api client.
        public Configuration(ApiClient apiClient)
        {
            setApiClientUsingDefault(apiClient);
        }

        /// 
        /// Version of the package.
        /// 
        /// Version of the package.
        public const string Version = "{{packageVersion}}";

        /// 
        /// Gets or sets the default Configuration.
        /// 
        /// Configuration.
        public static Configuration Default = new Configuration();

        /// 
        /// Default creation of exceptions for a given method name and response object
        /// 
        public static readonly ExceptionFactory DefaultExceptionFactory = (methodName, response) =>
        {
            int status = (int) response.StatusCode;
            if (status >= 400) return new ApiException(status, String.Format("Error calling {0}: {1}", methodName, response.Content), response.Content);
            if (status == 0) return new ApiException(status, String.Format("Error calling {0}: {1}", methodName, response.ErrorMessage), response.ErrorMessage);
            return null;
        };

        /// 
        /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
        /// 
        /// Timeout.
        public int Timeout
        {
            get { return ApiClient.RestClient.Timeout; }

            set
            {
                if (ApiClient != null)
                    ApiClient.RestClient.Timeout = value;
            }
        }

        /// 
        /// Gets or sets the default API client for making HTTP calls.
        /// 
        /// The API client.
        public ApiClient ApiClient;

        /// 
        /// Set the ApiClient using Default or ApiClient instance.
        /// 
        /// An instance of ApiClient.
        /// 
        public void setApiClientUsingDefault (ApiClient apiClient = null)
        {
            if (apiClient == null)
            {
                if (Default != null && Default.ApiClient == null)
                    Default.ApiClient = new ApiClient();

                ApiClient = Default != null ? Default.ApiClient : new ApiClient();
            }
            else
            {
                if (Default != null && Default.ApiClient == null)
                    Default.ApiClient = apiClient;

                ApiClient = apiClient;
            }
        }

        private Dictionary _defaultHeaderMap = new Dictionary();

        /// 
        /// Gets or sets the default header.
        /// 
        public Dictionary DefaultHeader
        {
            get { return _defaultHeaderMap; }

            set
            {
                _defaultHeaderMap = value;
            }
        }

        /// 
        /// Add default header.
        /// 
        /// Header field name.
        /// Header field value.
        /// 
        public void AddDefaultHeader(string key, string value)
        {
            _defaultHeaderMap[key] = value;
        }

        /// 
        /// Add Api Key Header.
        /// 
        /// Api Key name.
        /// Api Key value.
        /// 
        public void AddApiKey(string key, string value)
        {
            ApiKey[key] = value;
        }

        /// 
        /// Sets the API key prefix.
        /// 
        /// Api Key name.
        /// Api Key value.
        public void AddApiKeyPrefix(string key, string value)
        {
            ApiKeyPrefix[key] = value;
        }

        /// 
        /// Gets or sets the HTTP user agent.
        /// 
        /// Http user agent.
        public String UserAgent { get; set; }

        /// 
        /// Gets or sets the username (HTTP basic authentication).
        /// 
        /// The username.
        public String Username { get; set; }

        /// 
        /// Gets or sets the password (HTTP basic authentication).
        /// 
        /// The password.
        public String Password { get; set; }

        /// 
        /// Gets or sets the access token for OAuth2 authentication.
        /// 
        /// The access token.
        public String AccessToken { get; set; }

        /// 
        /// Gets or sets the API key based on the authentication name.
        /// 
        /// The API key.
        public Dictionary ApiKey = new Dictionary();

        /// 
        /// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
        /// 
        /// The prefix of the API key.
        public Dictionary ApiKeyPrefix = new Dictionary();

        /// 
        /// Get the API key with prefix.
        /// 
        /// API key identifier (authentication scheme).
        /// API key with prefix.
        public string GetApiKeyWithPrefix (string apiKeyIdentifier)
        {
            var apiKeyValue = "";
            ApiKey.TryGetValue (apiKeyIdentifier, out apiKeyValue);
            var apiKeyPrefix = "";
            if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out apiKeyPrefix))
                return apiKeyPrefix + " " + apiKeyValue;
            else
                return apiKeyValue;
        }

        private string _tempFolderPath = Path.GetTempPath();

        /// 
        /// Gets or sets the temporary folder path to store the files downloaded from the server.
        /// 
        /// Folder path.
        public String TempFolderPath
        {
            get { return _tempFolderPath; }

            set
            {
                if (String.IsNullOrEmpty(value))
                {
                    _tempFolderPath = value;
                    return;
                }

                // create the directory if it does not exist
                if (!Directory.Exists(value))
                    Directory.CreateDirectory(value);

                // check if the path contains directory separator at the end
                if (value[value.Length - 1] == Path.DirectorySeparatorChar)
                    _tempFolderPath = value;
                else
                    _tempFolderPath = value  + Path.DirectorySeparatorChar;
            }
        }

        private const string ISO8601_DATETIME_FORMAT = "o";

        private string _dateTimeFormat = ISO8601_DATETIME_FORMAT;

        /// 
        /// Gets or sets the the date time format used when serializing in the ApiClient
        /// By default, it's set to ISO 8601 - "o", for others see:
        /// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx
        /// and https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
        /// No validation is done to ensure that the string you're providing is valid
        /// 
        /// The DateTimeFormat string
        public String DateTimeFormat
        {
            get
            {
                return _dateTimeFormat;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    // Never allow a blank or null string, go back to the default
                    _dateTimeFormat = ISO8601_DATETIME_FORMAT;
                    return;
                }

                // Caution, no validation when you choose date time format other than ISO 8601
                // Take a look at the above links
                _dateTimeFormat = value;
            }
        }

        /// 
        /// Returns a string with essential information for debugging.
        /// 
        public static String ToDebugReport()
        {
            String report = "C# SDK ({{{packageName}}}) Debug Report:\n";
            {{^supportsUWP}}
            report += "    OS: " + Environment.OSVersion + "\n";
            report += "    .NET Framework Version: " + Assembly
                     .GetExecutingAssembly()
                     .GetReferencedAssemblies()
                     .Where(x => x.Name == "System.Core").First().Version.ToString()  + "\n";
            {{/supportsUWP}}
            report += "    Version of the API: {{{version}}}\n";
            report += "    SDK Package Version: {{{packageVersion}}}\n";

            return report;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy