forked from CyberSource/cybersource-rest-client-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfiguration.cs
417 lines (364 loc) · 15 KB
/
Configuration.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* CyberSource Merged Spec
*
* All CyberSource API specs merged together. These are available at https://developer.cybersource.com/api/reference/api-reference.html
*
* OpenAPI spec version: 0.0.1
*
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Net;
namespace CyberSource.Client
{
/// <summary>
/// Represents a set of configuration settings
/// </summary>
public class Configuration
{
/// <summary>
/// Initializes a new instance of the Configuration class with different settings
/// </summary>
/// <param name="apiClient">Api client</param>
/// <param name="defaultHeader">Dictionary of default HTTP header</param>
/// <param name="username">Username</param>
/// <param name="password">Password</param>
/// <param name="accessToken">accessToken</param>
/// <param name="apiKey">Dictionary of API key</param>
/// <param name="apiKeyPrefix">Dictionary of API key prefix</param>
/// <param name="tempFolderPath">Temp folder path</param>
/// <param name="dateTimeFormat">DateTime format string</param>
/// <param name="timeout">HTTP connection timeout (in milliseconds)</param>
/// <param name="userAgent">HTTP user agent</param>
/// <param name="merchConfigDictObj">Dictiory Object for Merchant Config</param>
public Configuration(ApiClient apiClient = null,
Dictionary<string, string> defaultHeader = null,
string username = null,
string password = null,
string accessToken = null,
Dictionary<string, string> apiKey = null,
Dictionary<string, string> apiKeyPrefix = null,
string tempFolderPath = null,
string dateTimeFormat = null,
int timeout = 100000,
string userAgent = "Swagger-Codegen/1.0.0/csharp",
IReadOnlyDictionary<string, string> merchConfigDictObj = null
)
{
SetApiClientUsingDefault(apiClient);
Username = username;
Password = password;
AccessToken = accessToken;
UserAgent = userAgent;
ClientId = GetClientId();
if (merchConfigDictObj != null)
{
SolutionId = merchConfigDictObj.ContainsKey("solutionID") ? merchConfigDictObj["solutionID"] : null;
}
if (merchConfigDictObj != null)
{
if (merchConfigDictObj.ContainsKey("useProxy"))
{
if (bool.Parse(merchConfigDictObj["useProxy"]))
{
if (!string.IsNullOrWhiteSpace(merchConfigDictObj["proxyAddress"]) && int.TryParse(merchConfigDictObj["proxyPort"], out int proxyPortTest))
{
WebProxy proxy = new WebProxy(merchConfigDictObj["proxyAddress"], proxyPortTest);
if (!string.IsNullOrWhiteSpace(merchConfigDictObj["proxyUsername"]) && !string.IsNullOrWhiteSpace(merchConfigDictObj["proxyPassword"]))
{
proxy.Credentials = new NetworkCredential(merchConfigDictObj["proxyUsername"], merchConfigDictObj["proxyPassword"]);
}
AddWebProxy(proxy);
}
}
}
}
if (defaultHeader != null)
DefaultHeader = defaultHeader;
if (apiKey != null)
ApiKey = apiKey;
if (apiKeyPrefix != null)
ApiKeyPrefix = apiKeyPrefix;
TempFolderPath = tempFolderPath;
DateTimeFormat = dateTimeFormat;
Timeout = timeout;
MerchantConfigDictionaryObj = merchConfigDictObj;
}
private string GetClientId()
{
var assembly = typeof(Configuration).Assembly;
var assemblyVersion = AssemblyHelper.GetAssemblyAttribute<AssemblyFileVersionAttribute>(assembly).Version;
return "cybs-rest-sdk-dotnet-" + assemblyVersion;
}
/// <summary>
/// Initializes a new instance of the Configuration class.
/// </summary>
/// <param name="apiClient">Api client.</param>
public Configuration(ApiClient apiClient)
{
SetApiClientUsingDefault(apiClient);
}
/// <summary>
/// Version of the package.
/// </summary>
/// <value>Version of the package.</value>
public const string Version = "1.0.0";
/// <summary>
/// Gets or sets the default Configuration.
/// </summary>
/// <value>Configuration.</value>
public static Configuration Default = new Configuration();
/// <summary>
/// Default creation of exceptions for a given method name and response object
/// </summary>
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;
};
/// <summary>
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
/// </summary>
/// <value>Timeout.</value>
public int Timeout
{
get { return ApiClient.RestClient.Timeout; }
set
{
if (ApiClient != null)
ApiClient.RestClient.Timeout = value;
}
}
/// <summary>
/// Gets or sets the default API client for making HTTP calls.
/// </summary>
/// <value>The API client.</value>
public ApiClient ApiClient;
/// <summary>
/// Set the ApiClient using Default or ApiClient instance.
/// </summary>
/// <param name="apiClient">An instance of ApiClient.</param>
/// <returns></returns>
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;
// }
if (apiClient == null)
{
ApiClient = new ApiClient();
}
else
{
ApiClient = apiClient;
}
}
/// <summary>
/// Gets or sets the default header.
/// </summary>
public Dictionary<string, string> DefaultHeader { get; set; } = new Dictionary<string, string>();
/// <summary>
/// Add default header.
/// </summary>
/// <param name="key">Header field name.</param>
/// <param name="value">Header field value.</param>
/// <returns></returns>
public void AddDefaultHeader(string key, string value)
{
DefaultHeader[key] = value;
}
/// <summary>
/// Add Api Key Header.
/// </summary>
/// <param name="key">Api Key name.</param>
/// <param name="value">Api Key value.</param>
/// <returns></returns>
public void AddApiKey(string key, string value)
{
ApiKey[key] = value;
}
/// <summary>
/// Sets the API key prefix.
/// </summary>
/// <param name="key">Api Key name.</param>
/// <param name="value">Api Key value.</param>
public void AddApiKeyPrefix(string key, string value)
{
ApiKeyPrefix[key] = value;
}
/// <summary>
/// Gets or sets the web proxy for the request.
/// </summary>
/// <param name="proxy">The proxy object.</param>
public void AddWebProxy(WebProxy proxy)
{
Proxy = proxy;
}
/// <summary>
/// Gets or sets the Merchant Config Dictionary Object (key/value pairs).
/// </summary>
/// <value>Merchant Config Dictionary Object</value>
public IReadOnlyDictionary<string, string> MerchantConfigDictionaryObj { get; set; }
/// <summary>
/// Gets or sets the HTTP user agent.
/// </summary>
/// <value>Http user agent.</value>
public string UserAgent { get; set; }
/// <summary>
/// Gets or sets the username (HTTP basic authentication).
/// </summary>
/// <value>The username.</value>
public string Username { get; set; }
/// <summary>
/// Gets or sets the password (HTTP basic authentication).
/// </summary>
/// <value>The password.</value>
public string Password { get; set; }
/// <summary>
/// Gets or sets the access token for OAuth2 authentication.
/// </summary>
/// <value>The access token.</value>
public string AccessToken { get; set; }
/// <summary>
/// Gets or sets the API key based on the authentication name.
/// </summary>
/// <value>The API key.</value>
public Dictionary<string, string> ApiKey = new Dictionary<string, string>();
/// <summary>
/// Gets or sets the Client ID for SDK auditing and/or reporting.
/// </summary>
/// <value>The Client ID.</value>
public string ClientId { get; set; }
/// <summary>
/// Gets or sets the Solution ID for SDK auditing and/or reporting.
/// </summary>
/// <value>The Solution ID.</value>
public string SolutionId { get; set; }
/// <summary>
/// Gets or sets the prefix (e.g. Token) of the API key based on the authentication name.
/// </summary>
/// <value>The prefix of the API key.</value>
public Dictionary<string, string> ApiKeyPrefix = new Dictionary<string, string>();
/// <summary>
/// Gets or sets the proxy object to use for the requests.
/// </summary>
/// <value>The proxy object.</value>
public WebProxy Proxy { get; set; }
/// <summary>
/// Get the API key with prefix.
/// </summary>
/// <param name="apiKeyIdentifier">API key identifier (authentication scheme).</param>
/// <returns>API key with prefix.</returns>
public string GetApiKeyWithPrefix (string apiKeyIdentifier)
{
ApiKey.TryGetValue(apiKeyIdentifier, out string apiKeyValue);
if (ApiKeyPrefix.TryGetValue (apiKeyIdentifier, out string apiKeyPrefix))
return apiKeyPrefix + " " + apiKeyValue;
else
return apiKeyValue;
}
private string _tempFolderPath;
/// <summary>
/// Gets or sets the temporary folder path to store the files downloaded from the server.
/// </summary>
/// <value>Folder path.</value>
public string TempFolderPath
{
get
{
// default to Path.GetTempPath() if _tempFolderPath is not set
if (string.IsNullOrEmpty(_tempFolderPath))
{
_tempFolderPath = Path.GetTempPath();
}
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;
/// <summary>
/// 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
/// </summary>
/// <value>The DateTimeFormat string</value>
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;
}
}
/// <summary>
/// Returns a string with essential information for debugging.
/// </summary>
public static string ToDebugReport()
{
string report = "C# SDK (CyberSource) Debug Report:\n";
report += " OS: " + Environment.OSVersion + "\n";
report += " .NET Framework Version: " + Assembly
.GetExecutingAssembly()
.GetReferencedAssemblies()
.Where(x => x.Name == "System.Core").First().Version.ToString() + "\n";
report += " Version of the API: 0.0.1\n";
report += " SDK Package Version: 1.0.0\n";
return report;
}
}
public static class AssemblyHelper
{
public static T GetAssemblyAttribute<T>(this Assembly ass) where T : Attribute
{
object[] attributes = ass.GetCustomAttributes(typeof(T), false);
if (attributes == null || attributes.Length == 0)
return null;
return attributes.OfType<T>().SingleOrDefault();
}
}
}