Skip to content

Commit 1b5b444

Browse files
committed
Modifying static OAuthApi
1 parent 2367ac9 commit 1b5b444

File tree

1 file changed

+141
-51
lines changed
  • cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/Api

1 file changed

+141
-51
lines changed

cybersource-rest-client-netstandard/cybersource-rest-client-netstandard/Api/OAuthApi.cs

Lines changed: 141 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using RestSharp;
66
using CyberSource.Client;
77
using CyberSource.Model;
8+
using NLog;
9+
using AuthenticationSdk.util;
810

911
namespace CyberSource.Api
1012
{
@@ -54,22 +56,29 @@ public interface IOAuthApi : IApiAccessor
5456
/// </summary>
5557
public partial class OAuthApi : IOAuthApi
5658
{
57-
private CyberSource.Client.ExceptionFactory _exceptionFactory = (name, response) => null;
59+
private static Logger logger;
60+
private ExceptionFactory _exceptionFactory = (name, response) => null;
61+
private int? _statusCode;
5862

5963
/// <summary>
6064
/// Initializes a new instance of the <see cref="OAuthApi"/> class.
6165
/// </summary>
6266
/// <returns></returns>
63-
public OAuthApi(String basePath)
67+
public OAuthApi(string basePath)
6468
{
65-
this.Configuration = new Configuration(new ApiClient(basePath));
69+
Configuration = new Configuration(new ApiClient(basePath));
6670

67-
ExceptionFactory = CyberSource.Client.Configuration.DefaultExceptionFactory;
71+
ExceptionFactory = Configuration.DefaultExceptionFactory;
6872

6973
// ensure API client has configuration ready
7074
if (Configuration.ApiClient.Configuration == null)
7175
{
72-
this.Configuration.ApiClient.Configuration = this.Configuration;
76+
Configuration.ApiClient.Configuration = Configuration;
77+
}
78+
79+
if (logger == null)
80+
{
81+
logger = LogManager.GetCurrentClassLogger();
7382
}
7483
}
7584

@@ -82,30 +91,35 @@ public OAuthApi(String basePath)
8291
public OAuthApi(Configuration configuration = null)
8392
{
8493
if (configuration == null) // use the default one in Configuration
85-
this.Configuration = Configuration.Default;
94+
Configuration = Configuration.Default;
8695
else
87-
this.Configuration = configuration;
96+
Configuration = configuration;
97+
98+
ExceptionFactory = Configuration.DefaultExceptionFactory;
8899

89-
ExceptionFactory = CyberSource.Client.Configuration.DefaultExceptionFactory;
100+
Configuration.ApiClient.Configuration = Configuration;
90101

91-
this.Configuration.ApiClient.Configuration = this.Configuration;
102+
if (logger == null)
103+
{
104+
logger = LogManager.GetCurrentClassLogger();
105+
}
92106
}
93107

94108
/// <summary>
95109
/// Gets the base path of the API client.
96110
/// </summary>
97111
/// <value>The base path</value>
98-
public String GetBasePath()
112+
public string GetBasePath()
99113
{
100-
return this.Configuration.ApiClient.RestClient.BaseUrl.ToString();
114+
return Configuration.ApiClient.RestClient.Options.BaseUrl.ToString();
101115
}
102116

103117
/// <summary>
104118
/// Sets the base path of the API client.
105119
/// </summary>
106120
/// <value>The base path</value>
107121
[Obsolete("SetBasePath is deprecated, please do 'Configuration.ApiClient = new ApiClient(\"http://new-path\")' instead.")]
108-
public void SetBasePath(String basePath)
122+
public void SetBasePath(string basePath)
109123
{
110124
// do nothing
111125
}
@@ -119,12 +133,13 @@ public void SetBasePath(String basePath)
119133
/// <summary>
120134
/// Provides a factory method hook for the creation of exceptions.
121135
/// </summary>
122-
public CyberSource.Client.ExceptionFactory ExceptionFactory
136+
public ExceptionFactory ExceptionFactory
123137
{
124138
get
125139
{
126140
if (_exceptionFactory != null && _exceptionFactory.GetInvocationList().Length > 1)
127141
{
142+
logger.Error("InvalidOperationException : Multicast delegate for ExceptionFactory is unsupported.");
128143
throw new InvalidOperationException("Multicast delegate for ExceptionFactory is unsupported.");
129144
}
130145
return _exceptionFactory;
@@ -137,9 +152,9 @@ public CyberSource.Client.ExceptionFactory ExceptionFactory
137152
/// </summary>
138153
/// <returns>Dictionary of HTTP header</returns>
139154
[Obsolete("DefaultHeader is deprecated, please use Configuration.DefaultHeader instead.")]
140-
public Dictionary<String, String> DefaultHeader()
155+
public Dictionary<string, string> DefaultHeader()
141156
{
142-
return this.Configuration.DefaultHeader;
157+
return Configuration.DefaultHeader;
143158
}
144159

145160
/// <summary>
@@ -151,51 +166,85 @@ public Dictionary<String, String> DefaultHeader()
151166
[Obsolete("AddDefaultHeader is deprecated, please use Configuration.AddDefaultHeader instead.")]
152167
public void AddDefaultHeader(string key, string value)
153168
{
154-
this.Configuration.AddDefaultHeader(key, value);
169+
Configuration.AddDefaultHeader(key, value);
155170
}
156171

157-
172+
/// <summary>
173+
/// Retrieves the status code being set for the most recently executed API request.
174+
/// </summary>
175+
/// <returns>Status Code of previous request</returns>
176+
public int GetStatusCode()
177+
{
178+
return this._statusCode == null ? 0 : (int) this._statusCode;
179+
}
180+
181+
/// <summary>
182+
/// Sets the value of status code for the most recently executed API request, in order to be retrieved later.
183+
/// </summary>
184+
/// <param name="statusCode">Status Code to be set</param>
185+
/// <returns></returns>
186+
public void SetStatusCode(int? statusCode)
187+
{
188+
this._statusCode = statusCode;
189+
}
190+
191+
/// <summary>
192+
/// Process a POST request to generate an access token.
193+
/// </summary>
158194
/// <exception cref="CyberSource.Client.ApiException">Thrown when fails to make API call</exception>
159195
/// <param name="createAccessTokenRequest"></param>
160196
/// <returns>AccessTokenResponse</returns>
161197
public AccessTokenResponse PostAccessTokenRequest(CreateAccessTokenRequest createAccessTokenRequest)
162198
{
199+
logger.Debug("CALLING API \"PostAccessTokenRequest\" STARTED");
200+
this.SetStatusCode(null);
163201
ApiResponse<AccessTokenResponse> localVarResponse = PostAccessTokenRequestWithHttpInfo(createAccessTokenRequest);
202+
logger.Debug("CALLING API \"PostAccessTokenRequest\" ENDED");
203+
this.SetStatusCode(localVarResponse.StatusCode);
164204
return localVarResponse.Data;
165205
}
166206

167-
207+
/// <summary>
208+
/// Process a POST request to generate an access token.
209+
/// </summary>
168210
/// <exception cref="CyberSource.Client.ApiException">Thrown when fails to make API call</exception>
169211
/// <param name="createAccessTokenRequest"></param>
170212
/// <returns>ApiResponse of AccessTokenResponse</returns>
171213
public ApiResponse<AccessTokenResponse> PostAccessTokenRequestWithHttpInfo(CreateAccessTokenRequest createAccessTokenRequest)
172-
{
214+
{
215+
LogUtility logUtility = new LogUtility();
216+
173217
// verify the required parameter 'createAccessTokenRequest' is set
174218
if (createAccessTokenRequest == null)
219+
{
220+
logger.Error("ApiException : Missing required parameter 'createAccessTokenRequest' when calling OAuthApi->PostAccessTokenRequest");
175221
throw new ApiException(400, "Missing required parameter 'createAccessTokenRequest' when calling OAuthApi->PostAccessTokenRequest");
222+
}
176223

177224
var localVarPath = $"/oauth2/v3/token";
178-
var localVarPathParams = new Dictionary<String, String>();
179-
var localVarQueryParams = new Dictionary<String, String>();
180-
var localVarHeaderParams = new Dictionary<String, String>(Configuration.DefaultHeader);
181-
var localVarFormParams = new Dictionary<String, String>();
182-
var localVarFileParams = new Dictionary<String, FileParameter>();
183-
Object localVarPostBody = null;
225+
var localVarPathParams = new Dictionary<string, string>();
226+
var localVarQueryParams = new Dictionary<string, string>();
227+
var localVarHeaderParams = new Dictionary<string, string>(Configuration.DefaultHeader);
228+
var localVarFormParams = new Dictionary<string, string>();
229+
var localVarFileParams = new Dictionary<string, FileParameter>();
230+
object localVarPostBody = null;
184231

185232
// to determine the Content-Type header
186-
String[] localVarHttpContentTypes = new String[] {
233+
string[] localVarHttpContentTypes = new string[] {
187234
"application/x-www-form-urlencoded"
188235
};
189-
String localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);
236+
string localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);
190237

191238
// to determine the Accept header
192-
String[] localVarHttpHeaderAccepts = new String[] {
239+
string[] localVarHttpHeaderAccepts = new string[] {
193240
"application/json"
194241
};
195-
String localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);
242+
string localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);
196243
if (localVarHttpHeaderAccept != null)
244+
{
197245
localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept);
198-
246+
}
247+
199248
if (createAccessTokenRequest != null && createAccessTokenRequest.GetType() != typeof(byte[]))
200249
{
201250
localVarPostBody = Configuration.ApiClient.Serialize(createAccessTokenRequest); // http body (model) parameter
@@ -205,67 +254,95 @@ public ApiResponse<AccessTokenResponse> PostAccessTokenRequestWithHttpInfo(Creat
205254
localVarPostBody = createAccessTokenRequest; // byte array
206255
}
207256

257+
if (logUtility.IsMaskingEnabled(logger))
258+
{
259+
logger.Debug($"HTTP Request Body :\n{logUtility.MaskSensitiveData(localVarPostBody.ToString())}");
260+
}
261+
else
262+
{
263+
logger.Debug($"HTTP Request Body :\n{localVarPostBody}");
264+
}
265+
208266

209267
// make the HTTP request
210-
IRestResponse localVarResponse = (IRestResponse)Configuration.ApiClient.CallApi(localVarPath,
211-
Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
268+
RestResponse localVarResponse = (RestResponse) Configuration.ApiClient.CallApi(localVarPath,
269+
Method.Post, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
212270
localVarPathParams, localVarHttpContentType);
213271

214-
int localVarStatusCode = (int)localVarResponse.StatusCode;
272+
int localVarStatusCode = (int) localVarResponse.StatusCode;
215273

216274
if (ExceptionFactory != null)
217275
{
218276
Exception exception = ExceptionFactory("PostAccessTokenRequest", localVarResponse);
219-
if (exception != null) throw exception;
277+
if (exception != null)
278+
{
279+
logger.Error($"Exception : {exception.Message}");
280+
throw exception;
281+
}
220282
}
221283

222284
return new ApiResponse<AccessTokenResponse>(localVarStatusCode,
223285
localVarResponse.Headers.ToDictionary(x => x.Name, x => x.Value.ToString()),
224286
(AccessTokenResponse)Configuration.ApiClient.Deserialize(localVarResponse, typeof(AccessTokenResponse))); // Return statement
225287
}
226-
288+
289+
/// <summary>
290+
/// Process a POST request to generate an access token.
291+
/// </summary>
227292
/// <exception cref="CyberSource.Client.ApiException">Thrown when fails to make API call</exception>
228293
/// <param name="createAccessTokenRequest"></param>
229294
/// <returns>Task of AccessTokenResponse</returns>
230295
public async System.Threading.Tasks.Task<AccessTokenResponse> PostAccessTokenRequestAsync(CreateAccessTokenRequest createAccessTokenRequest)
231296
{
297+
logger.Debug("CALLING API \"PostAccessTokenRequestAsync\" STARTED");
298+
this.SetStatusCode(null);
232299
ApiResponse<AccessTokenResponse> localVarResponse = await PostAccessTokenRequestAsyncWithHttpInfo(createAccessTokenRequest);
300+
logger.Debug("CALLING API \"PostAccessTokenRequestAsync\" ENDED");
301+
this.SetStatusCode(localVarResponse.StatusCode);
233302
return localVarResponse.Data;
234-
235303
}
236304

237-
305+
/// <summary>
306+
/// Process a POST request to generate an access token.
307+
/// </summary>
238308
/// <exception cref="CyberSource.Client.ApiException">Thrown when fails to make API call</exception>
239309
/// <param name="createAccessTokenRequest"></param>
240310
/// <returns>Task of ApiResponse (AccessTokenResponse)</returns>
241311
public async System.Threading.Tasks.Task<ApiResponse<AccessTokenResponse>> PostAccessTokenRequestAsyncWithHttpInfo(CreateAccessTokenRequest createAccessTokenRequest)
242312
{
313+
LogUtility logUtility = new LogUtility();
314+
243315
// verify the required parameter 'createAccessTokenRequest' is set
244316
if (createAccessTokenRequest == null)
317+
{
318+
logger.Error("ApiException : Missing required parameter 'createAccessTokenRequest' when calling OAuthApi->PostAccessTokenRequest");
245319
throw new ApiException(400, "Missing required parameter 'createAccessTokenRequest' when calling OAuthApi->PostAccessTokenRequest");
320+
}
246321

247322
var localVarPath = $"/oauth2/v3/token";
248-
var localVarPathParams = new Dictionary<String, String>();
249-
var localVarQueryParams = new Dictionary<String, String>();
250-
var localVarHeaderParams = new Dictionary<String, String>(Configuration.DefaultHeader);
251-
var localVarFormParams = new Dictionary<String, String>();
252-
var localVarFileParams = new Dictionary<String, FileParameter>();
253-
Object localVarPostBody = null;
323+
var localVarPathParams = new Dictionary<string, string>();
324+
var localVarQueryParams = new Dictionary<string, string>();
325+
var localVarHeaderParams = new Dictionary<string, string>(Configuration.DefaultHeader);
326+
var localVarFormParams = new Dictionary<string, string>();
327+
var localVarFileParams = new Dictionary<string, FileParameter>();
328+
object localVarPostBody = null;
254329

255330
// to determine the Content-Type header
256-
String[] localVarHttpContentTypes = new String[] {
331+
string[] localVarHttpContentTypes = new string[] {
257332
"application/x-www-form-urlencoded"
258333
};
259-
String localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);
334+
string localVarHttpContentType = Configuration.ApiClient.SelectHeaderContentType(localVarHttpContentTypes);
260335

261336
// to determine the Accept header
262-
String[] localVarHttpHeaderAccepts = new String[] {
337+
string[] localVarHttpHeaderAccepts = new string[] {
263338
"application/json"
264339
};
265-
String localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);
340+
string localVarHttpHeaderAccept = Configuration.ApiClient.SelectHeaderAccept(localVarHttpHeaderAccepts);
266341
if (localVarHttpHeaderAccept != null)
342+
{
267343
localVarHeaderParams.Add("Accept", localVarHttpHeaderAccept);
268-
344+
}
345+
269346
if (createAccessTokenRequest != null && createAccessTokenRequest.GetType() != typeof(byte[]))
270347
{
271348
localVarPostBody = Configuration.ApiClient.Serialize(createAccessTokenRequest); // http body (model) parameter
@@ -275,18 +352,31 @@ public async System.Threading.Tasks.Task<ApiResponse<AccessTokenResponse>> PostA
275352
localVarPostBody = createAccessTokenRequest; // byte array
276353
}
277354

355+
if (logUtility.IsMaskingEnabled(logger))
356+
{
357+
logger.Debug($"HTTP Request Body :\n{logUtility.MaskSensitiveData(localVarPostBody.ToString())}");
358+
}
359+
else
360+
{
361+
logger.Debug($"HTTP Request Body :\n{localVarPostBody}");
362+
}
363+
278364

279365
// make the HTTP request
280-
IRestResponse localVarResponse = (IRestResponse)await Configuration.ApiClient.CallApiAsync(localVarPath,
281-
Method.POST, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
366+
RestResponse localVarResponse = (RestResponse)await Configuration.ApiClient.CallApiAsync(localVarPath,
367+
Method.Post, localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarFileParams,
282368
localVarPathParams, localVarHttpContentType);
283369

284370
int localVarStatusCode = (int)localVarResponse.StatusCode;
285371

286372
if (ExceptionFactory != null)
287373
{
288374
Exception exception = ExceptionFactory("PostAccessTokenRequest", localVarResponse);
289-
if (exception != null) throw exception;
375+
if (exception != null)
376+
{
377+
logger.Error($"Exception : {exception.Message}");
378+
throw exception;
379+
}
290380
}
291381

292382
return new ApiResponse<AccessTokenResponse>(localVarStatusCode,

0 commit comments

Comments
 (0)