From c3fb8d877614508a17d093eecfa146cb0ef1d563 Mon Sep 17 00:00:00 2001 From: vmalinovskiy Date: Thu, 12 Jan 2017 19:00:58 +0200 Subject: [PATCH 1/2] vmalinovskiy: added way to use api without dll config deployment --- .../Api/ProxyIntegrationTest.cs | 14 ++ .../CallfireApiClient.csproj | 1 + src/CallfireApiClient/CallfireClient.cs | 48 +++--- src/CallfireApiClient/ClientConstants.cs | 4 + src/CallfireApiClient/ProxyAuthenticator.cs | 24 +++ src/CallfireApiClient/RestApiClient.cs | 140 ++++++++++++++---- 6 files changed, 181 insertions(+), 50 deletions(-) create mode 100644 src/CallfireApiClient/ProxyAuthenticator.cs diff --git a/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs b/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs index 0ab788f..d535b4e 100644 --- a/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs +++ b/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs @@ -15,5 +15,19 @@ public void QueryCallfireThroughProxyWithBasicAuth() var account = Client.MeApi.GetAccount(); Console.WriteLine("account: " + account); } + + [Test] + public void QueryCallfireThroughProxyWithProxyAuth() + { + ProxyAuthenticator auth = new ProxyAuthenticator() + { + ProxyAddress = "localhost:3128", + ProxyCredentials = "proxyuser:proxypass" + }; + + CallfireClient Client = new CallfireClient(auth); + var account = Client.MeApi.GetAccount(); + Console.WriteLine("account: " + account); + } } } diff --git a/src/CallfireApiClient/CallfireApiClient.csproj b/src/CallfireApiClient/CallfireApiClient.csproj index e60c1d6..6cf7c31 100644 --- a/src/CallfireApiClient/CallfireApiClient.csproj +++ b/src/CallfireApiClient/CallfireApiClient.csproj @@ -179,6 +179,7 @@ + diff --git a/src/CallfireApiClient/CallfireClient.cs b/src/CallfireApiClient/CallfireClient.cs index f461f25..133e674 100644 --- a/src/CallfireApiClient/CallfireClient.cs +++ b/src/CallfireApiClient/CallfireClient.cs @@ -18,25 +18,25 @@ public class CallfireClient { public RestApiClient RestApiClient { get; set; } - readonly Lazy _MeApi; - readonly Lazy _OrdersApi; - readonly Lazy _BatchesApi; - readonly Lazy _CampaignSoundsApi; - readonly Lazy _ContactsApi; - readonly Lazy _ContactListsApi; - readonly Lazy _NumbersApi; - readonly Lazy _NumberLeasesApi; - readonly Lazy _KeywordsApi; - readonly Lazy _KeywordLeasesApi; - readonly Lazy _DncApi; - readonly Lazy _CallsApi; - readonly Lazy _TextsApi; - readonly Lazy _TextAutoRepliesApi; - readonly Lazy _TextBroadcastsApi; - readonly Lazy _CallBroadcastsApi; - readonly Lazy _MediaApi; - readonly Lazy _SubscriptionsApi; - readonly Lazy _WebhooksApi; + private Lazy _MeApi; + private Lazy _OrdersApi; + private Lazy _BatchesApi; + private Lazy _CampaignSoundsApi; + private Lazy _ContactsApi; + private Lazy _ContactListsApi; + private Lazy _NumbersApi; + private Lazy _NumberLeasesApi; + private Lazy _KeywordsApi; + private Lazy _KeywordLeasesApi; + private Lazy _DncApi; + private Lazy _CallsApi; + private Lazy _TextsApi; + private Lazy _TextAutoRepliesApi; + private Lazy _TextBroadcastsApi; + private Lazy _CallBroadcastsApi; + private Lazy _MediaApi; + private Lazy _SubscriptionsApi; + private Lazy _WebhooksApi; public MeApi MeApi { get { return _MeApi.Value; } } @@ -80,7 +80,17 @@ public class CallfireClient public CallfireClient(string username, string password) { RestApiClient = new RestApiClient(new HttpBasicAuthenticator(username, password)); + InitApis(); + } + public CallfireClient(ProxyAuthenticator proxyAuth) + { + RestApiClient = new RestApiClient(proxyAuth); + InitApis(); + } + + private void InitApis() + { _MeApi = new Lazy(() => new MeApi(RestApiClient)); _OrdersApi = new Lazy(() => new OrdersApi(RestApiClient)); _BatchesApi = new Lazy(() => new BatchesApi(RestApiClient)); diff --git a/src/CallfireApiClient/ClientConstants.cs b/src/CallfireApiClient/ClientConstants.cs index 4aa465e..0ff528b 100644 --- a/src/CallfireApiClient/ClientConstants.cs +++ b/src/CallfireApiClient/ClientConstants.cs @@ -13,6 +13,8 @@ public static class ClientConstants public const string LOG_FILE_LISTENER_NAME = "CallfireLogFile"; public const string CONFIG_API_BASE_PATH = "CallFireBasePath"; + public const string API_BASE_PATH_DEFAULT_VALUE = "https://api.callfire.com/v2"; + public const string CONFIG_CLIENT_NAME = "CallFireClientVersion"; public const string PROXY_ADDRESS_PROPERTY = "com.callfire.api.client.proxy.address"; @@ -24,5 +26,7 @@ public static class ClientConstants public const string GENERIC_HELP_LINK = "https://answers.callfire.com/hc/en-us"; public static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + public static string DEFAULT_FILE_CONTENT_TYPE = "application/octet-stream"; } } \ No newline at end of file diff --git a/src/CallfireApiClient/ProxyAuthenticator.cs b/src/CallfireApiClient/ProxyAuthenticator.cs new file mode 100644 index 0000000..4bbcad8 --- /dev/null +++ b/src/CallfireApiClient/ProxyAuthenticator.cs @@ -0,0 +1,24 @@ +using RestSharp.Authenticators; + +namespace CallfireApiClient +{ + /// + /// authenticator to include proxy configuration parameters + /// + public class ProxyAuthenticator + { + public string ProxyAddress { get; set; } + + public string ProxyCredentials { get; set; } + + public ProxyAuthenticator(string proxyAddress, string proxyCredentials) + { + ProxyAddress = proxyAddress; + ProxyCredentials = proxyCredentials; + } + + public ProxyAuthenticator() + { + } + } +} \ No newline at end of file diff --git a/src/CallfireApiClient/RestApiClient.cs b/src/CallfireApiClient/RestApiClient.cs index 4e52e22..28018c9 100644 --- a/src/CallfireApiClient/RestApiClient.cs +++ b/src/CallfireApiClient/RestApiClient.cs @@ -1,6 +1,5 @@ using System; using RestSharp; -using CallfireApiClient.Api.Common.Model.Request; using RestSharp.Authenticators; using System.Collections.Generic; using System.Configuration; @@ -10,8 +9,6 @@ using System.Collections; using System.Text; using System.IO; -using System.Reflection; -using System.Diagnostics; using System.Net; namespace CallfireApiClient @@ -21,11 +18,9 @@ namespace CallfireApiClient /// public class RestApiClient { - private const string DEFAULT_FILE_CONTENT_TYPE = "application/octet-stream"; - - private readonly Logger Logger = new Logger(); - private readonly ISerializer JsonSerializer; - private readonly IDeserializer JsonDeserializer; + private static ISerializer JsonSerializer; + private static IDeserializer JsonDeserializer; + private static Logger Logger = new Logger(); private static KeyValueConfigurationCollection ClientConfig; /// @@ -38,19 +33,44 @@ public class RestApiClient /// Returns base URL path for all Callfire's API 2.0 endpoints /// / /// string representation of base URL path - public string ApiBasePath { get; private set; } + public static string ApiBasePath { get; private set; } + + /// + /// Returns proxy adress for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy adress + public static string ProxyAddress { get; private set; } + + /// + /// Returns proxy port for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy port + public static int ProxyPort { get; private set; } + + /// + /// Returns proxy login for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy login + public static string ProxyLogin { get; private set; } + + /// + /// Returns proxy password for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy login + public static string ProxyPassword { get; private set; } /// /// Returns HTTP request filters associated with API client /// /// active filters. - public SortedSet Filters { get; } + public SortedSet Filters { get; private set; } /// /// loads client configuration /// static RestApiClient() { ClientConfig = LoadAppSettings(); + SetAppSettings(); } /// @@ -70,7 +90,39 @@ public static KeyValueConfigurationCollection getClientConfig() /// public RestApiClient(IAuthenticator authenticator) { - ApiBasePath = ClientConfig[ClientConstants.CONFIG_API_BASE_PATH].Value; + InitRestApiClient(authenticator); + SetUpRestClientProxy(); + } + + /// + /// REST API client constructor + /// / + /// + /// authentication API authentication method + /// + public RestApiClient(ProxyAuthenticator authenticator) + { + InitRestApiClient(new HttpBasicAuthenticator("", "")); + ConfigureProxyParameters(authenticator.ProxyAddress, authenticator.ProxyCredentials); + SetUpRestClientProxy(); + } + + /// + /// Loads client's app settings config section + /// + public static KeyValueConfigurationCollection LoadAppSettings() + { + var path = typeof(RestApiClient).Assembly.Location; + var config = ConfigurationManager.OpenExeConfiguration(path); + var appSettings = (AppSettingsSection)config.GetSection("appSettings"); + return appSettings.Settings; + } + + /// + /// Initialize main RestClient parameters + /// + private void InitRestApiClient(IAuthenticator authenticator) + { JsonSerializer = new CallfireJsonConverter(); JsonDeserializer = JsonSerializer as IDeserializer; @@ -79,10 +131,37 @@ public RestApiClient(IAuthenticator authenticator) RestClient.UserAgent = this.GetType().Assembly.GetName().Name + "-csharp-" + this.GetType().Assembly.GetName().Version; RestClient.AddHandler("application/json", JsonDeserializer); + Filters = new SortedSet(); + } + + /// + /// Set up client's app config parameters from app settings + /// + private static void SetAppSettings() + { + //basePath + var basePath = ClientConfig[ClientConstants.CONFIG_API_BASE_PATH]; - String proxyAddress = ClientConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value; - String proxyCredentials = ClientConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value; + if (basePath == null || string.IsNullOrWhiteSpace(basePath.Value)) + { + ApiBasePath = ClientConstants.API_BASE_PATH_DEFAULT_VALUE; + } + else + { + ApiBasePath = basePath.Value; + } + + //proxy + String proxyAddress = ClientConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value; + String proxyCredentials = ClientConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value; + ConfigureProxyParameters(proxyAddress, proxyCredentials); + } + /// + /// Configure app proxy parameters + /// + private static void ConfigureProxyParameters(String proxyAddress, String proxyCredentials) + { if (!String.IsNullOrEmpty(proxyAddress)) { Logger.Debug("Configuring proxy host for client: {} auth: {}", proxyAddress, proxyCredentials); @@ -90,40 +169,39 @@ public RestApiClient(IAuthenticator authenticator) String[] parsedAddress = proxyAddress.Split(delimiterChars); String[] parsedCredentials = (proxyCredentials == null ? "" : proxyCredentials).Split(delimiterChars); int portValue = parsedAddress.Length > 1 ? ClientUtils.StrToIntDef(parsedAddress[1], ClientConstants.DEFAULT_PROXY_PORT) : ClientConstants.DEFAULT_PROXY_PORT; - WebProxy proxy = new WebProxy(parsedAddress[0], portValue); - + if (!String.IsNullOrEmpty(proxyCredentials)) { if (parsedCredentials.Length > 1) { - proxy.Credentials = new NetworkCredential(parsedCredentials[0], parsedCredentials[1]); + ProxyAddress = parsedAddress[0]; + ProxyPort = portValue; + ProxyLogin = parsedCredentials[0]; + ProxyPassword = parsedCredentials[1]; } else { Logger.Debug("Proxy credentials have wrong format, must be username:password"); } } - RestClient.Proxy = proxy; } - - Filters = new SortedSet(); } /// - /// Loads client's app settings config section + /// Set up client's app proxy /// - public static KeyValueConfigurationCollection LoadAppSettings() + private void SetUpRestClientProxy() { - var path = typeof(RestApiClient).Assembly.Location; - var config = ConfigurationManager.OpenExeConfiguration(path); - var appSettings = (AppSettingsSection)config.GetSection("appSettings"); - var basePath = appSettings.Settings[ClientConstants.CONFIG_API_BASE_PATH]; - if (basePath == null || string.IsNullOrWhiteSpace(basePath.Value)) + if (!String.IsNullOrEmpty(ProxyAddress)) + { + WebProxy proxy = new WebProxy(ProxyAddress, ProxyPort); + proxy.Credentials = new NetworkCredential(ProxyLogin, ProxyPassword); + RestClient.Proxy = proxy; + } + else { - throw new CallfireClientException("Cannot read " + ClientConstants.CONFIG_API_BASE_PATH + - " property from configuration file at: " + path + ".config"); + Logger.Debug("Proxy wasn't configured, please check input parameters"); } - return appSettings.Settings; } /// @@ -266,7 +344,7 @@ public Stream GetFileData(string path, IEnumerable> var queryParams = ClientUtils.BuildQueryParams("name", fileName); var restRequest = CreateRestRequest(path, Method.POST, queryParams); restRequest.AddHeader("Content-Type", "multipart/form-data"); - restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), contentType != null ? contentType : DEFAULT_FILE_CONTENT_TYPE); + restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), contentType != null ? contentType : ClientConstants.DEFAULT_FILE_CONTENT_TYPE); restRequest.AddParameter("name", fileName); return DoRequest(restRequest); } @@ -294,7 +372,7 @@ public Stream GetFileData(string path, IEnumerable> var restRequest = CreateRestRequest(path, Method.POST, queryParams); restRequest.AddHeader("Content-Type", "multipart/form-data"); - restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), DEFAULT_FILE_CONTENT_TYPE); + restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), ClientConstants.DEFAULT_FILE_CONTENT_TYPE); restRequest.AddParameter("name", fileName); return DoRequest(restRequest); } From 68be7f246e48e704805e8c7acef9f81379f0f48c Mon Sep 17 00:00:00 2001 From: vmalinovskiy Date: Mon, 16 Jan 2017 18:33:06 +0200 Subject: [PATCH 2/2] vmalinovskiy: added way to use api without dll config deployment(refactoring) --- .../Api/ProxyIntegrationTest.cs | 21 +-- .../CallfireApiClient.csproj | 2 +- src/CallfireApiClient/CallfireClient.cs | 54 ++++---- src/CallfireApiClient/ClientConfig.cs | 39 ++++++ src/CallfireApiClient/ProxyAuthenticator.cs | 24 ---- src/CallfireApiClient/RestApiClient.cs | 129 +++++++----------- 6 files changed, 126 insertions(+), 143 deletions(-) create mode 100644 src/CallfireApiClient/ClientConfig.cs delete mode 100644 src/CallfireApiClient/ProxyAuthenticator.cs diff --git a/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs b/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs index d535b4e..32c1a70 100644 --- a/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs +++ b/src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs @@ -9,23 +9,26 @@ public class ProxyIntegrationTest [Test] public void QueryCallfireThroughProxyWithBasicAuth() { - RestApiClient.getClientConfig().Add(ClientConstants.PROXY_ADDRESS_PROPERTY, "localhost:3128"); - RestApiClient.getClientConfig().Add(ClientConstants.PROXY_CREDENTIALS_PROPERTY, "proxyuser:proxypass"); + RestApiClient.getApplicationConfig().Add(ClientConstants.PROXY_ADDRESS_PROPERTY, "localhost:3128"); + RestApiClient.getApplicationConfig().Add(ClientConstants.PROXY_CREDENTIALS_PROPERTY, "proxyuser:proxypass"); CallfireClient Client = new CallfireClient("", ""); var account = Client.MeApi.GetAccount(); Console.WriteLine("account: " + account); } [Test] - public void QueryCallfireThroughProxyWithProxyAuth() + public void QueryCallfireThroughProxyWithProxy() { - ProxyAuthenticator auth = new ProxyAuthenticator() - { - ProxyAddress = "localhost:3128", - ProxyCredentials = "proxyuser:proxypass" - }; + CallfireClient Client = new CallfireClient("", ""); + + Client.SetClientConfig(new ClientConfig(){ + ApiBasePath = "https://api.callfire.com/v2", + ProxyAddress = "localhost", + ProxyPort = 3128, + ProxyLogin = "proxyuser", + ProxyPassword = "proxypass" + }); - CallfireClient Client = new CallfireClient(auth); var account = Client.MeApi.GetAccount(); Console.WriteLine("account: " + account); } diff --git a/src/CallfireApiClient/CallfireApiClient.csproj b/src/CallfireApiClient/CallfireApiClient.csproj index 6cf7c31..697aa96 100644 --- a/src/CallfireApiClient/CallfireApiClient.csproj +++ b/src/CallfireApiClient/CallfireApiClient.csproj @@ -179,7 +179,7 @@ - + diff --git a/src/CallfireApiClient/CallfireClient.cs b/src/CallfireApiClient/CallfireClient.cs index 133e674..c1f23b2 100644 --- a/src/CallfireApiClient/CallfireClient.cs +++ b/src/CallfireApiClient/CallfireClient.cs @@ -16,27 +16,33 @@ namespace CallfireApiClient /// public class CallfireClient { + + public void SetClientConfig(ClientConfig config) + { + RestApiClient.ClientConfig = config; + } + public RestApiClient RestApiClient { get; set; } - private Lazy _MeApi; - private Lazy _OrdersApi; - private Lazy _BatchesApi; - private Lazy _CampaignSoundsApi; - private Lazy _ContactsApi; - private Lazy _ContactListsApi; - private Lazy _NumbersApi; - private Lazy _NumberLeasesApi; - private Lazy _KeywordsApi; - private Lazy _KeywordLeasesApi; - private Lazy _DncApi; - private Lazy _CallsApi; - private Lazy _TextsApi; - private Lazy _TextAutoRepliesApi; - private Lazy _TextBroadcastsApi; - private Lazy _CallBroadcastsApi; - private Lazy _MediaApi; - private Lazy _SubscriptionsApi; - private Lazy _WebhooksApi; + readonly Lazy _MeApi; + readonly Lazy _OrdersApi; + readonly Lazy _BatchesApi; + readonly Lazy _CampaignSoundsApi; + readonly Lazy _ContactsApi; + readonly Lazy _ContactListsApi; + readonly Lazy _NumbersApi; + readonly Lazy _NumberLeasesApi; + readonly Lazy _KeywordsApi; + readonly Lazy _KeywordLeasesApi; + readonly Lazy _DncApi; + readonly Lazy _CallsApi; + readonly Lazy _TextsApi; + readonly Lazy _TextAutoRepliesApi; + readonly Lazy _TextBroadcastsApi; + readonly Lazy _CallBroadcastsApi; + readonly Lazy _MediaApi; + readonly Lazy _SubscriptionsApi; + readonly Lazy _WebhooksApi; public MeApi MeApi { get { return _MeApi.Value; } } @@ -80,17 +86,7 @@ public class CallfireClient public CallfireClient(string username, string password) { RestApiClient = new RestApiClient(new HttpBasicAuthenticator(username, password)); - InitApis(); - } - public CallfireClient(ProxyAuthenticator proxyAuth) - { - RestApiClient = new RestApiClient(proxyAuth); - InitApis(); - } - - private void InitApis() - { _MeApi = new Lazy(() => new MeApi(RestApiClient)); _OrdersApi = new Lazy(() => new OrdersApi(RestApiClient)); _BatchesApi = new Lazy(() => new BatchesApi(RestApiClient)); diff --git a/src/CallfireApiClient/ClientConfig.cs b/src/CallfireApiClient/ClientConfig.cs new file mode 100644 index 0000000..bc4babf --- /dev/null +++ b/src/CallfireApiClient/ClientConfig.cs @@ -0,0 +1,39 @@ + +namespace CallfireApiClient +{ + /// + /// object to include client configuration parameters + /// + public class ClientConfig + { + /// + /// Returns base URL path for all Callfire's API 2.0 endpoints + /// / + /// string representation of base URL path + public string ApiBasePath { get; set; } + + /// + /// Returns proxy adress for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy adress + public string ProxyAddress { get; set; } + + /// + /// Returns proxy port for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy port + public int ProxyPort { get; set; } + + /// + /// Returns proxy login for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy login + public string ProxyLogin { get; set; } + + /// + /// Returns proxy password for all Callfire's API 2.0 endpoints + /// / + /// string representation of proxy login + public string ProxyPassword { get; set; } + } +} \ No newline at end of file diff --git a/src/CallfireApiClient/ProxyAuthenticator.cs b/src/CallfireApiClient/ProxyAuthenticator.cs deleted file mode 100644 index 4bbcad8..0000000 --- a/src/CallfireApiClient/ProxyAuthenticator.cs +++ /dev/null @@ -1,24 +0,0 @@ -using RestSharp.Authenticators; - -namespace CallfireApiClient -{ - /// - /// authenticator to include proxy configuration parameters - /// - public class ProxyAuthenticator - { - public string ProxyAddress { get; set; } - - public string ProxyCredentials { get; set; } - - public ProxyAuthenticator(string proxyAddress, string proxyCredentials) - { - ProxyAddress = proxyAddress; - ProxyCredentials = proxyCredentials; - } - - public ProxyAuthenticator() - { - } - } -} \ No newline at end of file diff --git a/src/CallfireApiClient/RestApiClient.cs b/src/CallfireApiClient/RestApiClient.cs index 28018c9..36972f4 100644 --- a/src/CallfireApiClient/RestApiClient.cs +++ b/src/CallfireApiClient/RestApiClient.cs @@ -18,68 +18,55 @@ namespace CallfireApiClient /// public class RestApiClient { - private static ISerializer JsonSerializer; - private static IDeserializer JsonDeserializer; + private readonly ISerializer JsonSerializer; + private readonly IDeserializer JsonDeserializer; private static Logger Logger = new Logger(); - private static KeyValueConfigurationCollection ClientConfig; - /// - /// RestSharp client configured to query Callfire API - /// / - /// RestSharp client interface - public IRestClient RestClient { get; set; } - - /// - /// Returns base URL path for all Callfire's API 2.0 endpoints - /// / - /// string representation of base URL path - public static string ApiBasePath { get; private set; } + private static KeyValueConfigurationCollection ApplicationConfig; - /// - /// Returns proxy adress for all Callfire's API 2.0 endpoints - /// / - /// string representation of proxy adress - public static string ProxyAddress { get; private set; } + private ClientConfig _ClientConfig = new ClientConfig(); - /// - /// Returns proxy port for all Callfire's API 2.0 endpoints - /// / - /// string representation of proxy port - public static int ProxyPort { get; private set; } + public ClientConfig ClientConfig + { + get + { + return _ClientConfig; + } - /// - /// Returns proxy login for all Callfire's API 2.0 endpoints - /// / - /// string representation of proxy login - public static string ProxyLogin { get; private set; } + set + { + _ClientConfig = value; + RestClient.BaseUrl = !string.IsNullOrWhiteSpace(value.ApiBasePath) ? new Uri(value.ApiBasePath) : RestClient.BaseUrl; + SetUpRestClientProxy(); + } + } /// - /// Returns proxy password for all Callfire's API 2.0 endpoints + /// RestSharp client configured to query Callfire API /// / - /// string representation of proxy login - public static string ProxyPassword { get; private set; } + /// RestSharp client interface + public IRestClient RestClient { get; set; } /// /// Returns HTTP request filters associated with API client /// /// active filters. - public SortedSet Filters { get; private set; } + public SortedSet Filters { get; } /// /// loads client configuration /// static RestApiClient() { - ClientConfig = LoadAppSettings(); - SetAppSettings(); + ApplicationConfig = LoadAppSettings(); } /// /// Get client configuration /// /// configuration properties collection - public static KeyValueConfigurationCollection getClientConfig() + public static KeyValueConfigurationCollection getApplicationConfig() { - return ClientConfig; + return ApplicationConfig; } /// @@ -90,20 +77,18 @@ public static KeyValueConfigurationCollection getClientConfig() /// public RestApiClient(IAuthenticator authenticator) { - InitRestApiClient(authenticator); - SetUpRestClientProxy(); - } + SetAppSettings(); + + JsonSerializer = new CallfireJsonConverter(); + JsonDeserializer = JsonSerializer as IDeserializer; + + RestClient = new RestClient(_ClientConfig.ApiBasePath); + RestClient.Authenticator = authenticator; + RestClient.UserAgent = this.GetType().Assembly.GetName().Name + "-csharp-" + this.GetType().Assembly.GetName().Version; + RestClient.AddHandler("application/json", JsonDeserializer); + + Filters = new SortedSet(); - /// - /// REST API client constructor - /// / - /// - /// authentication API authentication method - /// - public RestApiClient(ProxyAuthenticator authenticator) - { - InitRestApiClient(new HttpBasicAuthenticator("", "")); - ConfigureProxyParameters(authenticator.ProxyAddress, authenticator.ProxyCredentials); SetUpRestClientProxy(); } @@ -118,49 +103,33 @@ public static KeyValueConfigurationCollection LoadAppSettings() return appSettings.Settings; } - /// - /// Initialize main RestClient parameters - /// - private void InitRestApiClient(IAuthenticator authenticator) - { - JsonSerializer = new CallfireJsonConverter(); - JsonDeserializer = JsonSerializer as IDeserializer; - - RestClient = new RestClient(ApiBasePath); - RestClient.Authenticator = authenticator; - RestClient.UserAgent = this.GetType().Assembly.GetName().Name + "-csharp-" + this.GetType().Assembly.GetName().Version; - RestClient.AddHandler("application/json", JsonDeserializer); - - Filters = new SortedSet(); - } - /// /// Set up client's app config parameters from app settings /// - private static void SetAppSettings() + private void SetAppSettings() { //basePath - var basePath = ClientConfig[ClientConstants.CONFIG_API_BASE_PATH]; + var basePath = ApplicationConfig[ClientConstants.CONFIG_API_BASE_PATH]; if (basePath == null || string.IsNullOrWhiteSpace(basePath.Value)) { - ApiBasePath = ClientConstants.API_BASE_PATH_DEFAULT_VALUE; + _ClientConfig.ApiBasePath = ClientConstants.API_BASE_PATH_DEFAULT_VALUE; } else { - ApiBasePath = basePath.Value; + _ClientConfig.ApiBasePath = basePath.Value; } //proxy - String proxyAddress = ClientConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value; - String proxyCredentials = ClientConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value; + String proxyAddress = ApplicationConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value; + String proxyCredentials = ApplicationConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value; ConfigureProxyParameters(proxyAddress, proxyCredentials); } /// /// Configure app proxy parameters /// - private static void ConfigureProxyParameters(String proxyAddress, String proxyCredentials) + private void ConfigureProxyParameters(String proxyAddress, String proxyCredentials) { if (!String.IsNullOrEmpty(proxyAddress)) { @@ -174,10 +143,10 @@ private static void ConfigureProxyParameters(String proxyAddress, String proxyCr { if (parsedCredentials.Length > 1) { - ProxyAddress = parsedAddress[0]; - ProxyPort = portValue; - ProxyLogin = parsedCredentials[0]; - ProxyPassword = parsedCredentials[1]; + _ClientConfig.ProxyAddress = parsedAddress[0]; + _ClientConfig.ProxyPort = portValue; + _ClientConfig.ProxyLogin = parsedCredentials[0]; + _ClientConfig.ProxyPassword = parsedCredentials[1]; } else { @@ -192,10 +161,10 @@ private static void ConfigureProxyParameters(String proxyAddress, String proxyCr /// private void SetUpRestClientProxy() { - if (!String.IsNullOrEmpty(ProxyAddress)) + if (!String.IsNullOrEmpty(_ClientConfig.ProxyAddress)) { - WebProxy proxy = new WebProxy(ProxyAddress, ProxyPort); - proxy.Credentials = new NetworkCredential(ProxyLogin, ProxyPassword); + WebProxy proxy = new WebProxy(_ClientConfig.ProxyAddress, _ClientConfig.ProxyPort); + proxy.Credentials = new NetworkCredential(_ClientConfig.ProxyLogin, _ClientConfig.ProxyPassword); RestClient.Proxy = proxy; } else