Skip to content

Commit e7a980f

Browse files
authored
Merge pull request #43 from CallFire/develop
Develop
2 parents 37a87f8 + 68be7f2 commit e7a980f

File tree

6 files changed

+154
-40
lines changed

6 files changed

+154
-40
lines changed

src/CallfireApiClient.IntegrationTests/Api/ProxyIntegrationTest.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@ public class ProxyIntegrationTest
99
[Test]
1010
public void QueryCallfireThroughProxyWithBasicAuth()
1111
{
12-
RestApiClient.getClientConfig().Add(ClientConstants.PROXY_ADDRESS_PROPERTY, "localhost:3128");
13-
RestApiClient.getClientConfig().Add(ClientConstants.PROXY_CREDENTIALS_PROPERTY, "proxyuser:proxypass");
12+
RestApiClient.getApplicationConfig().Add(ClientConstants.PROXY_ADDRESS_PROPERTY, "localhost:3128");
13+
RestApiClient.getApplicationConfig().Add(ClientConstants.PROXY_CREDENTIALS_PROPERTY, "proxyuser:proxypass");
1414
CallfireClient Client = new CallfireClient("", "");
1515
var account = Client.MeApi.GetAccount();
1616
Console.WriteLine("account: " + account);
1717
}
18+
19+
[Test]
20+
public void QueryCallfireThroughProxyWithProxy()
21+
{
22+
CallfireClient Client = new CallfireClient("", "");
23+
24+
Client.SetClientConfig(new ClientConfig(){
25+
ApiBasePath = "https://api.callfire.com/v2",
26+
ProxyAddress = "localhost",
27+
ProxyPort = 3128,
28+
ProxyLogin = "proxyuser",
29+
ProxyPassword = "proxypass"
30+
});
31+
32+
var account = Client.MeApi.GetAccount();
33+
Console.WriteLine("account: " + account);
34+
}
1835
}
1936
}

src/CallfireApiClient/CallfireApiClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<Compile Include="InternalServerErrorException.cs" />
180180
<Compile Include="Logger.cs" />
181181
<Compile Include="Properties\AssemblyInfo.cs" />
182+
<Compile Include="ClientConfig.cs" />
182183
<Compile Include="RequestFilter.cs" />
183184
<Compile Include="ResourceNotFoundException.cs" />
184185
<Compile Include="RestApiClient.cs" />

src/CallfireApiClient/CallfireClient.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ namespace CallfireApiClient
1616
/// </summary>
1717
public class CallfireClient
1818
{
19+
20+
public void SetClientConfig(ClientConfig config)
21+
{
22+
RestApiClient.ClientConfig = config;
23+
}
24+
1925
public RestApiClient RestApiClient { get; set; }
2026

2127
readonly Lazy<MeApi> _MeApi;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
namespace CallfireApiClient
3+
{
4+
/// <summary>
5+
/// object to include client configuration parameters
6+
/// <summary>
7+
public class ClientConfig
8+
{
9+
/// <summary>
10+
/// Returns base URL path for all Callfire's API 2.0 endpoints
11+
/// <summary>/
12+
/// <returns>string representation of base URL path</returns>
13+
public string ApiBasePath { get; set; }
14+
15+
/// <summary>
16+
/// Returns proxy adress for all Callfire's API 2.0 endpoints
17+
/// <summary>/
18+
/// <returns>string representation of proxy adress</returns>
19+
public string ProxyAddress { get; set; }
20+
21+
/// <summary>
22+
/// Returns proxy port for all Callfire's API 2.0 endpoints
23+
/// <summary>/
24+
/// <returns>string representation of proxy port</returns>
25+
public int ProxyPort { get; set; }
26+
27+
/// <summary>
28+
/// Returns proxy login for all Callfire's API 2.0 endpoints
29+
/// <summary>/
30+
/// <returns>string representation of proxy login</returns>
31+
public string ProxyLogin { get; set; }
32+
33+
/// <summary>
34+
/// Returns proxy password for all Callfire's API 2.0 endpoints
35+
/// <summary>/
36+
/// <returns>string representation of proxy login</returns>
37+
public string ProxyPassword { get; set; }
38+
}
39+
}

src/CallfireApiClient/ClientConstants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public static class ClientConstants
1313
public const string LOG_FILE_LISTENER_NAME = "CallfireLogFile";
1414

1515
public const string CONFIG_API_BASE_PATH = "CallFireBasePath";
16+
public const string API_BASE_PATH_DEFAULT_VALUE = "https://api.callfire.com/v2";
17+
1618
public const string CONFIG_CLIENT_NAME = "CallFireClientVersion";
1719

1820
public const string PROXY_ADDRESS_PROPERTY = "com.callfire.api.client.proxy.address";
@@ -24,5 +26,7 @@ public static class ClientConstants
2426
public const string GENERIC_HELP_LINK = "https://answers.callfire.com/hc/en-us";
2527

2628
public static readonly DateTime EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
29+
30+
public static string DEFAULT_FILE_CONTENT_TYPE = "application/octet-stream";
2731
}
2832
}

src/CallfireApiClient/RestApiClient.cs

Lines changed: 85 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using RestSharp;
3-
using CallfireApiClient.Api.Common.Model.Request;
43
using RestSharp.Authenticators;
54
using System.Collections.Generic;
65
using System.Configuration;
@@ -10,8 +9,6 @@
109
using System.Collections;
1110
using System.Text;
1211
using System.IO;
13-
using System.Reflection;
14-
using System.Diagnostics;
1512
using System.Net;
1613

1714
namespace CallfireApiClient
@@ -21,25 +18,35 @@ namespace CallfireApiClient
2118
/// </summary>
2219
public class RestApiClient
2320
{
24-
private const string DEFAULT_FILE_CONTENT_TYPE = "application/octet-stream";
25-
26-
private readonly Logger Logger = new Logger();
2721
private readonly ISerializer JsonSerializer;
2822
private readonly IDeserializer JsonDeserializer;
29-
private static KeyValueConfigurationCollection ClientConfig;
23+
private static Logger Logger = new Logger();
24+
25+
private static KeyValueConfigurationCollection ApplicationConfig;
26+
27+
private ClientConfig _ClientConfig = new ClientConfig();
28+
29+
public ClientConfig ClientConfig
30+
{
31+
get
32+
{
33+
return _ClientConfig;
34+
}
35+
36+
set
37+
{
38+
_ClientConfig = value;
39+
RestClient.BaseUrl = !string.IsNullOrWhiteSpace(value.ApiBasePath) ? new Uri(value.ApiBasePath) : RestClient.BaseUrl;
40+
SetUpRestClientProxy();
41+
}
42+
}
3043

3144
/// <summary>
3245
/// RestSharp client configured to query Callfire API
3346
/// <summary>/
3447
/// <returns>RestSharp client interface</returns>
3548
public IRestClient RestClient { get; set; }
3649

37-
/// <summary>
38-
/// Returns base URL path for all Callfire's API 2.0 endpoints
39-
/// <summary>/
40-
/// <returns>string representation of base URL path</returns>
41-
public string ApiBasePath { get; private set; }
42-
4350
/// <summary>
4451
/// Returns HTTP request filters associated with API client
4552
/// </summary>
@@ -50,16 +57,16 @@ public class RestApiClient
5057
/// loads client configuration
5158
/// </summary>
5259
static RestApiClient() {
53-
ClientConfig = LoadAppSettings();
60+
ApplicationConfig = LoadAppSettings();
5461
}
5562

5663
/// <summary>
5764
/// Get client configuration
5865
/// </summary>
5966
/// <value>configuration properties collection</value>
60-
public static KeyValueConfigurationCollection getClientConfig()
67+
public static KeyValueConfigurationCollection getApplicationConfig()
6168
{
62-
return ClientConfig;
69+
return ApplicationConfig;
6370
}
6471

6572
/// <summary>
@@ -70,60 +77,100 @@ public static KeyValueConfigurationCollection getClientConfig()
7077
/// </param>
7178
public RestApiClient(IAuthenticator authenticator)
7279
{
73-
ApiBasePath = ClientConfig[ClientConstants.CONFIG_API_BASE_PATH].Value;
80+
SetAppSettings();
81+
7482
JsonSerializer = new CallfireJsonConverter();
7583
JsonDeserializer = JsonSerializer as IDeserializer;
7684

77-
RestClient = new RestClient(ApiBasePath);
85+
RestClient = new RestClient(_ClientConfig.ApiBasePath);
7886
RestClient.Authenticator = authenticator;
7987
RestClient.UserAgent = this.GetType().Assembly.GetName().Name + "-csharp-" + this.GetType().Assembly.GetName().Version;
8088
RestClient.AddHandler("application/json", JsonDeserializer);
8189

90+
Filters = new SortedSet<RequestFilter>();
91+
92+
SetUpRestClientProxy();
93+
}
94+
95+
/// <summary>
96+
/// Loads client's app settings config section
97+
/// </summary>
98+
public static KeyValueConfigurationCollection LoadAppSettings()
99+
{
100+
var path = typeof(RestApiClient).Assembly.Location;
101+
var config = ConfigurationManager.OpenExeConfiguration(path);
102+
var appSettings = (AppSettingsSection)config.GetSection("appSettings");
103+
return appSettings.Settings;
104+
}
105+
106+
/// <summary>
107+
/// Set up client's app config parameters from app settings
108+
/// </summary>
109+
private void SetAppSettings()
110+
{
111+
//basePath
112+
var basePath = ApplicationConfig[ClientConstants.CONFIG_API_BASE_PATH];
82113

83-
String proxyAddress = ClientConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value;
84-
String proxyCredentials = ClientConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value;
114+
if (basePath == null || string.IsNullOrWhiteSpace(basePath.Value))
115+
{
116+
_ClientConfig.ApiBasePath = ClientConstants.API_BASE_PATH_DEFAULT_VALUE;
117+
}
118+
else
119+
{
120+
_ClientConfig.ApiBasePath = basePath.Value;
121+
}
122+
123+
//proxy
124+
String proxyAddress = ApplicationConfig[ClientConstants.PROXY_ADDRESS_PROPERTY]?.Value;
125+
String proxyCredentials = ApplicationConfig[ClientConstants.PROXY_CREDENTIALS_PROPERTY]?.Value;
126+
ConfigureProxyParameters(proxyAddress, proxyCredentials);
127+
}
85128

129+
/// <summary>
130+
/// Configure app proxy parameters
131+
/// </summary>
132+
private void ConfigureProxyParameters(String proxyAddress, String proxyCredentials)
133+
{
86134
if (!String.IsNullOrEmpty(proxyAddress))
87135
{
88136
Logger.Debug("Configuring proxy host for client: {} auth: {}", proxyAddress, proxyCredentials);
89137
char[] delimiterChars = { ':' };
90138
String[] parsedAddress = proxyAddress.Split(delimiterChars);
91139
String[] parsedCredentials = (proxyCredentials == null ? "" : proxyCredentials).Split(delimiterChars);
92140
int portValue = parsedAddress.Length > 1 ? ClientUtils.StrToIntDef(parsedAddress[1], ClientConstants.DEFAULT_PROXY_PORT) : ClientConstants.DEFAULT_PROXY_PORT;
93-
WebProxy proxy = new WebProxy(parsedAddress[0], portValue);
94-
141+
95142
if (!String.IsNullOrEmpty(proxyCredentials))
96143
{
97144
if (parsedCredentials.Length > 1)
98145
{
99-
proxy.Credentials = new NetworkCredential(parsedCredentials[0], parsedCredentials[1]);
146+
_ClientConfig.ProxyAddress = parsedAddress[0];
147+
_ClientConfig.ProxyPort = portValue;
148+
_ClientConfig.ProxyLogin = parsedCredentials[0];
149+
_ClientConfig.ProxyPassword = parsedCredentials[1];
100150
}
101151
else
102152
{
103153
Logger.Debug("Proxy credentials have wrong format, must be username:password");
104154
}
105155
}
106-
RestClient.Proxy = proxy;
107156
}
108-
109-
Filters = new SortedSet<RequestFilter>();
110157
}
111158

112159
/// <summary>
113-
/// Loads client's app settings config section
160+
/// Set up client's app proxy
114161
/// </summary>
115-
public static KeyValueConfigurationCollection LoadAppSettings()
162+
private void SetUpRestClientProxy()
116163
{
117-
var path = typeof(RestApiClient).Assembly.Location;
118-
var config = ConfigurationManager.OpenExeConfiguration(path);
119-
var appSettings = (AppSettingsSection)config.GetSection("appSettings");
120-
var basePath = appSettings.Settings[ClientConstants.CONFIG_API_BASE_PATH];
121-
if (basePath == null || string.IsNullOrWhiteSpace(basePath.Value))
164+
if (!String.IsNullOrEmpty(_ClientConfig.ProxyAddress))
165+
{
166+
WebProxy proxy = new WebProxy(_ClientConfig.ProxyAddress, _ClientConfig.ProxyPort);
167+
proxy.Credentials = new NetworkCredential(_ClientConfig.ProxyLogin, _ClientConfig.ProxyPassword);
168+
RestClient.Proxy = proxy;
169+
}
170+
else
122171
{
123-
throw new CallfireClientException("Cannot read " + ClientConstants.CONFIG_API_BASE_PATH +
124-
" property from configuration file at: " + path + ".config");
172+
Logger.Debug("Proxy wasn't configured, please check input parameters");
125173
}
126-
return appSettings.Settings;
127174
}
128175

129176
/// <summary>
@@ -266,7 +313,7 @@ public Stream GetFileData(string path, IEnumerable<KeyValuePair<string, object>>
266313
var queryParams = ClientUtils.BuildQueryParams("name", fileName);
267314
var restRequest = CreateRestRequest(path, Method.POST, queryParams);
268315
restRequest.AddHeader("Content-Type", "multipart/form-data");
269-
restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), contentType != null ? contentType : DEFAULT_FILE_CONTENT_TYPE);
316+
restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), contentType != null ? contentType : ClientConstants.DEFAULT_FILE_CONTENT_TYPE);
270317
restRequest.AddParameter("name", fileName);
271318
return DoRequest<T>(restRequest);
272319
}
@@ -294,7 +341,7 @@ public Stream GetFileData(string path, IEnumerable<KeyValuePair<string, object>>
294341

295342
var restRequest = CreateRestRequest(path, Method.POST, queryParams);
296343
restRequest.AddHeader("Content-Type", "multipart/form-data");
297-
restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), DEFAULT_FILE_CONTENT_TYPE);
344+
restRequest.AddFileBytes("file", File.ReadAllBytes(filePath), Path.GetFileName(filePath), ClientConstants.DEFAULT_FILE_CONTENT_TYPE);
298345
restRequest.AddParameter("name", fileName);
299346
return DoRequest<T>(restRequest);
300347
}

0 commit comments

Comments
 (0)