Skip to content

Commit 74b19db

Browse files
committed
Modifications for rewrite of RestClient
1 parent 6be209a commit 74b19db

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/Client/ApiClient.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,13 @@ public class RestClientFactory
3939
{
4040
private static readonly ConcurrentDictionary<int, Lazy<RestClient>> _restClientInstances = new ConcurrentDictionary<int, Lazy<RestClient>>();
4141

42-
public static RestClient GetRestClient(Configuration configuration, RestClientOptions clientOptions)
42+
public static RestClient GetRestClient(MerchantConfig merchantConfig, RestClientOptions clientOptions)
4343
{
4444
int MaxServicePointIdleTime;
4545
int DefaultConnectionLimit;
4646

47-
if (configuration.MerchantConfigDictionaryObj != null)
47+
if (merchantConfig != null)
4848
{
49-
var merchantConfig = new MerchantConfig(configuration.MerchantConfigDictionaryObj);
50-
5149
DefaultConnectionLimit = int.Parse(merchantConfig.MaxConnectionPoolSize);
5250
MaxServicePointIdleTime = int.Parse(merchantConfig.KeepAliveTime);
5351
}
@@ -64,6 +62,7 @@ public static RestClient GetRestClient(Configuration configuration, RestClientOp
6462
};
6563

6664
var httpClient = new HttpClient(handler);
65+
httpClient.Timeout = TimeSpan.FromMilliseconds(int.Parse(merchantConfig.TimeOut));
6766

6867
int hash = GetHashOfRestClientOptions(clientOptions);
6968

@@ -356,11 +355,13 @@ public object CallApi(
356355
path, method, queryParams, postBody, headerParams, formParams, fileParams,
357356
pathParams, contentType);
358357

359-
var newRestClientOptions = GetRestClientOptions(Configuration);
358+
MerchantConfig merchantConfig = new MerchantConfig(merchantConfigDictionary: Configuration.MerchantConfigDictionaryObj, mapToControlMLEonAPI: Configuration.MapToControlMLEonAPI);
359+
360+
var newRestClientOptions = GetRestClientOptions(merchantConfig, Configuration.UserAgent, TimeSpan.FromMilliseconds(Configuration.Timeout));
360361

361362
// RestClient.ClearHandlers();
362363

363-
var actualRestClient = RestClientFactory.GetRestClient(Configuration, newRestClientOptions);
364+
var actualRestClient = RestClientFactory.GetRestClient(merchantConfig, newRestClientOptions);
364365

365366
// Logging Request Headers
366367
var headerPrintOutput = new StringBuilder();
@@ -403,18 +404,16 @@ public object CallApi(
403404
return response;
404405
}
405406

406-
private RestClientOptions GetRestClientOptions(Configuration configuration)
407+
private RestClientOptions GetRestClientOptions(MerchantConfig merchantConfig, string userAgent, TimeSpan timeout)
407408
{
408409
RestClientOptions clientOptions = new RestClientOptions();
409410

410-
clientOptions.UserAgent = configuration.UserAgent;
411-
clientOptions.Timeout = TimeSpan.FromMilliseconds(configuration.Timeout);
411+
clientOptions.UserAgent = userAgent;
412+
clientOptions.Timeout = timeout;
412413

413-
var merchantConfig = configuration.MerchantConfigDictionaryObj != null
414-
? new MerchantConfig(configuration.MerchantConfigDictionaryObj)
415-
: new MerchantConfig();
414+
IWebProxy webProxy = null;
416415

417-
if (configuration.Proxy == null && merchantConfig.UseProxy != null)
416+
if (merchantConfig.UseProxy != null)
418417
{
419418
if (bool.Parse(merchantConfig.UseProxy))
420419
{
@@ -427,14 +426,14 @@ private RestClientOptions GetRestClientOptions(Configuration configuration)
427426
proxy.Credentials = new NetworkCredential(merchantConfig.ProxyUsername, merchantConfig.ProxyPassword);
428427
}
429428

430-
configuration.AddWebProxy(proxy);
429+
webProxy = proxy;
431430
}
432431
}
433432
}
434433

435-
if (configuration.Proxy != null)
434+
if (webProxy != null)
436435
{
437-
clientOptions.Proxy = configuration.Proxy;
436+
clientOptions.Proxy = webProxy;
438437
}
439438

440439
if (Equals(bool.Parse(merchantConfig.EnableClientCert), true))
@@ -518,9 +517,11 @@ public async System.Threading.Tasks.Task<object> CallApiAsync(
518517

519518
logger.Debug($"HTTP Request Headers :\n{logUtility.MaskSensitiveData(headerPrintOutput.ToString())}");
520519

521-
var newRestClientOptions = GetRestClientOptions(Configuration);
520+
MerchantConfig merchantConfig = new MerchantConfig(merchantConfigDictionary: Configuration.MerchantConfigDictionaryObj, mapToControlMLEonAPI: Configuration.MapToControlMLEonAPI);
521+
522+
var newRestClientOptions = GetRestClientOptions(merchantConfig, Configuration.UserAgent, TimeSpan.FromMilliseconds(Configuration.Timeout));
522523

523-
var actualRestClient = RestClientFactory.GetRestClient(Configuration, newRestClientOptions);
524+
var actualRestClient = RestClientFactory.GetRestClient(merchantConfig, newRestClientOptions);
524525

525526
InterceptRequest(request);
526527
var response = await actualRestClient.ExecuteAsync(request);

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/Client/Configuration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public Configuration(ApiClient apiClient = null,
8686
}
8787
}
8888
}
89+
90+
if (merchConfigDictObj.ContainsKey("timeout"))
91+
{
92+
Timeout = int.Parse(merchConfigDictObj["timeout"]);
93+
}
94+
else
95+
{
96+
Timeout = timeout;
97+
}
8998
}
9099

91100
if (defaultHeader != null)
@@ -97,7 +106,6 @@ public Configuration(ApiClient apiClient = null,
97106

98107
TempFolderPath = tempFolderPath;
99108
DateTimeFormat = dateTimeFormat;
100-
Timeout = timeout;
101109
MerchantConfigDictionaryObj = merchConfigDictObj;
102110
MapToControlMLEonAPI = mapToControlMLEonAPI;
103111

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/generator/cybersource-csharp-template/ApiClient.mustache

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ namespace {{packageName}}.Client
4040
{
4141
private static readonly ConcurrentDictionary<int, Lazy<RestClient>> _restClientInstances = new ConcurrentDictionary<int, Lazy<RestClient>>();
4242
43-
public static RestClient GetRestClient(Configuration configuration, RestClientOptions clientOptions)
43+
public static RestClient GetRestClient(MerchantConfig merchantConfig, RestClientOptions clientOptions)
4444
{
4545
int MaxServicePointIdleTime;
4646
int DefaultConnectionLimit;
4747
48-
if (configuration.MerchantConfigDictionaryObj != null)
48+
if (merchantConfig != null)
4949
{
50-
var merchantConfig = new MerchantConfig(configuration.MerchantConfigDictionaryObj);
51-
5250
DefaultConnectionLimit = int.Parse(merchantConfig.MaxConnectionPoolSize);
5351
MaxServicePointIdleTime = int.Parse(merchantConfig.KeepAliveTime);
5452
}
@@ -65,6 +63,7 @@ namespace {{packageName}}.Client
6563
};
6664

6765
var httpClient = new HttpClient(handler);
66+
httpClient.Timeout = TimeSpan.FromMilliseconds(int.Parse(merchantConfig.TimeOut));
6867

6968
int hash = GetHashOfRestClientOptions(clientOptions);
7069

@@ -398,11 +397,13 @@ namespace {{packageName}}.Client
398397
path, method, queryParams, postBody, headerParams, formParams, fileParams,
399398
pathParams, contentType);
400399

401-
var newRestClientOptions = GetRestClientOptions(Configuration);
400+
MerchantConfig merchantConfig = new MerchantConfig(merchantConfigDictionary: Configuration.MerchantConfigDictionaryObj, mapToControlMLEonAPI: Configuration.MapToControlMLEonAPI);
401+
402+
var newRestClientOptions = GetRestClientOptions(merchantConfig, Configuration.UserAgent, TimeSpan.FromMilliseconds(Configuration.Timeout));
402403

403404
// RestClient.ClearHandlers();
404405

405-
var actualRestClient = RestClientFactory.GetRestClient(Configuration, newRestClientOptions);
406+
var actualRestClient = RestClientFactory.GetRestClient(merchantConfig, newRestClientOptions);
406407

407408
// Logging Request Headers
408409
var headerPrintOutput = new StringBuilder();
@@ -456,18 +457,16 @@ namespace {{packageName}}.Client
456457
return response;
457458
}
458459

459-
private RestClientOptions GetRestClientOptions(Configuration configuration)
460+
private RestClientOptions GetRestClientOptions(MerchantConfig merchantConfig, string userAgent, TimeSpan timeout)
460461
{
461462
RestClientOptions clientOptions = new RestClientOptions();
462463
463-
clientOptions.UserAgent = configuration.UserAgent;
464-
clientOptions.Timeout = TimeSpan.FromMilliseconds(configuration.Timeout);
464+
clientOptions.UserAgent = userAgent;
465+
clientOptions.Timeout = timeout;
465466
466-
var merchantConfig = configuration.MerchantConfigDictionaryObj != null
467-
? new MerchantConfig(configuration.MerchantConfigDictionaryObj)
468-
: new MerchantConfig();
467+
IWebProxy webProxy = null;
469468
470-
if (configuration.Proxy == null && merchantConfig.UseProxy != null)
469+
if (merchantConfig.UseProxy != null)
471470
{
472471
if (bool.Parse(merchantConfig.UseProxy))
473472
{
@@ -480,14 +479,14 @@ namespace {{packageName}}.Client
480479
proxy.Credentials = new NetworkCredential(merchantConfig.ProxyUsername, merchantConfig.ProxyPassword);
481480
}
482481

483-
configuration.AddWebProxy(proxy);
482+
webProxy = proxy;
484483
}
485484
}
486485
}
487486

488-
if (configuration.Proxy != null)
487+
if (webProxy != null)
489488
{
490-
clientOptions.Proxy = configuration.Proxy;
489+
clientOptions.Proxy = webProxy;
491490
}
492491

493492
if (Equals(bool.Parse(merchantConfig.EnableClientCert), true))
@@ -572,9 +571,11 @@ namespace {{packageName}}.Client
572571

573572
logger.Debug($"HTTP Request Headers :\n{logUtility.MaskSensitiveData(headerPrintOutput.ToString())}");
574573

575-
var newRestClientOptions = GetRestClientOptions(Configuration);
574+
MerchantConfig merchantConfig = new MerchantConfig(merchantConfigDictionary: Configuration.MerchantConfigDictionaryObj, mapToControlMLEonAPI: Configuration.MapToControlMLEonAPI);
575+
576+
var newRestClientOptions = GetRestClientOptions(merchantConfig, Configuration.UserAgent, TimeSpan.FromMilliseconds(Configuration.Timeout));
576577

577-
var actualRestClient = RestClientFactory.GetRestClient(Configuration, newRestClientOptions);
578+
var actualRestClient = RestClientFactory.GetRestClient(merchantConfig, newRestClientOptions);
578579

579580
InterceptRequest(request);
580581
var response = await actualRestClient.Execute{{^netStandard}}Async{{/netStandard}}(request);

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/generator/cybersource-csharp-template/Configuration.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ namespace {{packageName}}.Client
7777
}
7878
}
7979
}
80+
81+
if (merchConfigDictObj.ContainsKey("timeout"))
82+
{
83+
Timeout = int.Parse(merchConfigDictObj["timeout"]);
84+
}
85+
else
86+
{
87+
Timeout = timeout;
88+
}
8089
}
8190

8291
if (defaultHeader != null)

0 commit comments

Comments
 (0)