-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathRequestService.cs
414 lines (366 loc) · 16.8 KB
/
RequestService.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
using FikaAmazonAPI.AmazonSpApiSDK.Models.Exceptions;
using FikaAmazonAPI.AmazonSpApiSDK.Models.Filters;
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
using FikaAmazonAPI.AmazonSpApiSDK.Services;
using FikaAmazonAPI.Search;
using FikaAmazonAPI.Utils;
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Serializers.NewtonsoftJson;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
using static FikaAmazonAPI.Utils.Constants;
namespace FikaAmazonAPI.Services
{
public class RequestService : ApiUrls
{
public static readonly string AccessTokenHeaderName = "x-amz-access-token";
public static readonly string SecurityTokenHeaderName = "x-amz-security-token";
public static readonly string ShippingBusinessIdHeaderName = "x-amzn-shipping-business-id";
protected RestClient RequestClient { get; set; }
protected RestRequest Request { get; set; }
protected AmazonCredential AmazonCredential { get; set; }
protected string AmazonSandboxUrl { get; set; }
protected string AmazonProductionUrl { get; set; }
protected string AccessToken { get; set; }
protected IList<KeyValuePair<string, string>> LastHeaders { get; set; }
private IRateLimitingHandler RateLimitingHandler { get; }
protected string ApiBaseUrl
{
get
{
return AmazonCredential.Environment == Environments.Sandbox ? AmazonSandboxUrl : AmazonProductionUrl;
}
}
private ILogger<RequestService>? _logger = null;
/// <summary>
/// Creates request base service
/// </summary>
/// <param name="amazonCredential">A credential containing the API user's information and cached token values</param>
/// <param name="rateLimitingHandler">A singleton designed to handle concurrent requests based on the rate limiting policy</param>
public RequestService(AmazonCredential amazonCredential,ILoggerFactory? loggerFactory, IRateLimitingHandler rateLimitingHandler = null)
{
RateLimitingHandler = rateLimitingHandler ?? new RateLimitingHandler();
_logger = loggerFactory?.CreateLogger<RequestService>();
AmazonCredential = amazonCredential;
AmazonSandboxUrl = amazonCredential.MarketPlace.Region.SandboxHostUrl;
AmazonProductionUrl = amazonCredential.MarketPlace.Region.HostUrl;
}
private void CreateRequest(string url, RestSharp.Method method)
{
if (AmazonCredential.Proxy == null)
{
var options = new RestClientOptions(ApiBaseUrl);
RequestClient = new RestClient(options,
configureSerialization: s => s.UseNewtonsoftJson());
}
else
{
var options = new RestClientOptions(ApiBaseUrl)
{
Proxy = AmazonCredential.Proxy
};
RequestClient = new RestClient(options,
configureSerialization: s => s.UseNewtonsoftJson());
}
Request = new RestRequest(url, method);
}
protected async Task CreateAuthorizedRequestAsync(string url, RestSharp.Method method,
List<KeyValuePair<string, string>> queryParameters = null, object postJsonObj = null,
TokenDataType tokenDataType = TokenDataType.Normal, object parameter = null,
CancellationToken cancellationToken = default)
{
var PiiObject = parameter as IParameterBasedPII;
if (PiiObject != null && PiiObject.IsNeedRestrictedDataToken)
await RefreshTokenAsync(TokenDataType.PII, PiiObject.RestrictedDataTokenRequest, cancellationToken);
else
await RefreshTokenAsync(tokenDataType, cancellationToken: cancellationToken);
CreateRequest(url, method);
if (postJsonObj != null)
AddJsonBody(postJsonObj);
if (queryParameters != null)
AddQueryParameters(queryParameters);
}
protected void CreateAuthorizedPagedRequest(AmazonFilter filter, string url, RestSharp.Method method)
{
RefreshToken();
if (filter.NextPage != null)
CreateRequest(filter.NextPage, method);
else
{
CreateRequest(url, method);
AddLimitHeader(filter.Limit);
}
AddAccessToken();
}
/// <summary>
/// Executes the request
/// </summary>
/// <typeparam name="T">Type to parse response to</typeparam>
/// <returns>Returns data of T type</returns>
protected async Task<T> ExecuteRequestTry<T>(
RateLimitType rateLimitType = RateLimitType.UNSET,
CancellationToken cancellationToken = default) where T : new()
{
RestHeader();
AddAccessToken();
AddShippingBusinessId();
//Remove AWS authorization
//Request = await TokenGeneration.SignWithSTSKeysAndSecurityTokenAsync(Request, RequestClient.Options.BaseUrl.Host, AmazonCredential, cancellationToken);
var response = await RateLimitingHandler.SafelyExecuteRequestAsync<T>(
RequestClient,
Request,
AmazonCredential,
rateLimitType,
responseCallback: response =>
{
LogRequest(Request, response);
SaveLastRequestHeader(response.Headers);
},
cancellationToken: cancellationToken);
ParseResponse(response);
if (response.StatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(response.Content) &&
response.Data == null)
{
response.Data = JsonConvert.DeserializeObject<T>(response.Content);
}
return response.Data;
}
private void SaveLastRequestHeader(IReadOnlyCollection<RestSharp.HeaderParameter> parameters)
{
LastHeaders = new List<KeyValuePair<string, string>>();
foreach (RestSharp.Parameter parameter in parameters ?? Enumerable.Empty<HeaderParameter>())
{
if (parameter != null && parameter.Name != null && parameter.Value != null)
{
LastHeaders.Add(new KeyValuePair<string, string>(parameter.Name.ToString(),
parameter.Value.ToString()));
}
}
}
private void LogRequest(RestRequest request, RestResponse response)
{
var requestToLog = new
{
resource = request.Resource,
parameters = request.Parameters.Select(parameter => new
{
name = parameter.Name,
value = parameter.Value,
type = parameter.Type.ToString()
}).ToList(),
// ToString() here to have the method as a nice string otherwise it will just show the enum value
method = request.Method.ToString(),
// This will generate the actual Uri used in the request
//uri = request. _restClient.BuildUri(request),
};
//remove the access token from the headers
requestToLog.parameters.RemoveAll(p => p.name == "x-amz-access-token");
var responseToLog = new
{
statusCode = response.StatusCode,
content = response.Content,
headers = response.Headers.Select(h => new
{
name = h.Name,
value = h.Value
}),
// The Uri that actually responded (could be different from the requestUri if a redirection occurred)
responseUri = response.ResponseUri,
errorMessage = response.ErrorMessage,
};
//There are PII considerations here
_logger?.LogInformation("Request completed, \nRequest: {@request} \n\nResponse: {@response}", requestToLog, responseToLog);
}
private void RestHeader()
{
lock (Request)
{
//Request?.Parameters?.RemoveParameter(AWSSignerHelper.XAmzDateHeaderName);
//Request?.Parameters?.RemoveParameter(AWSSignerHelper.AuthorizationHeaderName);
Request?.Parameters?.RemoveParameter(AccessTokenHeaderName);
Request?.Parameters?.RemoveParameter(SecurityTokenHeaderName);
Request?.Parameters?.RemoveParameter(ShippingBusinessIdHeaderName);
}
}
//public T ExecuteRequest<T>(RateLimitType rateLimitType = RateLimitType.UNSET) where T : new()
//{
// return this.ExecuteRequestAsync<T>(rateLimitType)).ConfigureAwait(false).GetAwaiter().GetResult();
//}
public async Task<T> ExecuteRequestAsync<T>(RateLimitType rateLimitType = RateLimitType.UNSET,
CancellationToken cancellationToken = default) where T : new()
{
var tryCount = 0;
while (true)
{
try
{
return await ExecuteRequestTry<T>(rateLimitType, cancellationToken);
}
catch (AmazonQuotaExceededException ex)
{
if (tryCount >= AmazonCredential.MaxThrottledRetryCount)
{
_logger?.LogWarning("Throttle max try count reached");
throw;
}
cancellationToken.ThrowIfCancellationRequested();
await RateLimitingHandler.WaitForLimitTypeAsync(AmazonCredential, rateLimitType, cancellationToken);
tryCount++;
}
}
}
protected void ParseResponse(RestResponse response)
{
if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Accepted ||
response.StatusCode == HttpStatusCode.Created || response.StatusCode == HttpStatusCode.NoContent)
return;
else if (response.StatusCode == HttpStatusCode.NotFound)
{
throw new AmazonNotFoundException("Resource that you are looking for is not found", response);
}
else
{
_logger?.LogWarning("Amazon Api didn't respond with Okay, see exception for more details: {content}", response.Content);
var errorResponse = response.Content.ConvertToErrorResponse();
if (errorResponse != null)
{
var error = errorResponse.Errors.FirstOrDefault();
switch (error.Code)
{
case "Unauthorized":
throw new AmazonUnauthorizedException(error.Message, response);
case "InvalidSignature":
throw new AmazonInvalidSignatureException(error.Message, response);
case "InvalidInput":
throw new AmazonInvalidInputException(error.Message, error.Details, response);
case "QuotaExceeded":
throw new AmazonQuotaExceededException(error.Message, response);
case "InternalFailure":
throw new AmazonInternalErrorException(error.Message, response);
}
}
}
if (response.StatusCode == HttpStatusCode.BadRequest)
{
throw new AmazonBadRequestException(
"BadRequest see https://developer-docs.amazon.com/sp-api/changelog/api-request-validation-for-400-errors-with-html-response for advice",
response);
}
throw new AmazonException("Amazon Api didn't respond with Okay, see exception for more details", response);
}
protected void AddQueryParameters(List<KeyValuePair<string, string>> queryParameters)
{
if (queryParameters != null)
queryParameters.ForEach(qp => Request.AddQueryParameter(qp.Key, qp.Value));
}
protected void AddLimitHeader(int limit)
{
Request.AddQueryParameter("limit", limit.ToString());
}
protected void AddJsonBody(object jsonData)
{
var json = JsonConvert.SerializeObject(jsonData);
Request.AddJsonBody(json);
}
protected void AddAccessToken()
{
lock (Request)
{
Request.AddOrUpdateHeader(AccessTokenHeaderName, AccessToken);
}
}
protected void AddShippingBusinessId()
{
if (AmazonCredential.ShippingBusiness.HasValue)
Request.AddOrUpdateHeader(ShippingBusinessIdHeaderName,
AmazonCredential.ShippingBusiness.Value.GetEnumMemberValue());
}
protected async Task RefreshToken(TokenDataType tokenDataType = TokenDataType.Normal,
CreateRestrictedDataTokenRequest requestPII = null)
{
var token = AmazonCredential.GetToken(tokenDataType);
if (token == null)
{
if (tokenDataType == TokenDataType.PII)
{
var pii = await CreateRestrictedDataTokenAsync(requestPII);
if (pii != null)
{
token = new TokenResponse()
{
access_token = pii.RestrictedDataToken,
expires_in = pii.ExpiresIn
};
}
else
{
throw new ArgumentNullException(nameof(pii));
}
}
else
{
token = await TokenGeneration.RefreshAccessTokenAsync(AmazonCredential, tokenDataType);
}
AmazonCredential.SetToken(tokenDataType, token);
}
AccessToken = token.access_token;
}
protected async Task RefreshTokenAsync(TokenDataType tokenDataType = TokenDataType.Normal,
CreateRestrictedDataTokenRequest requestPII = null, CancellationToken cancellationToken = default)
{
var token = AmazonCredential.GetToken(tokenDataType);
if (token == null)
{
if (tokenDataType == TokenDataType.PII)
{
var pii = await CreateRestrictedDataTokenAsync(requestPII, cancellationToken);
if (pii != null)
{
token = new TokenResponse()
{
access_token = pii.RestrictedDataToken,
expires_in = pii.ExpiresIn
};
}
else
{
throw new ArgumentNullException(nameof(pii));
}
}
else
{
token = await TokenGeneration.RefreshAccessTokenAsync(AmazonCredential, tokenDataType,
cancellationToken);
}
AmazonCredential.SetToken(tokenDataType, token);
}
AccessToken = token.access_token;
}
public IList<KeyValuePair<string, string>> LastResponseHeader => LastHeaders;
public CreateRestrictedDataTokenResponse CreateRestrictedDataToken(
CreateRestrictedDataTokenRequest createRestrictedDataTokenRequest)
{
return Task.Run(() => CreateRestrictedDataTokenAsync(createRestrictedDataTokenRequest))
.ConfigureAwait(false).GetAwaiter().GetResult();
}
public async Task<CreateRestrictedDataTokenResponse> CreateRestrictedDataTokenAsync(
CreateRestrictedDataTokenRequest createRestrictedDataTokenRequest,
CancellationToken cancellationToken = default)
{
await CreateAuthorizedRequestAsync(TokenApiUrls.RestrictedDataToken, RestSharp.Method.Post,
postJsonObj: createRestrictedDataTokenRequest, cancellationToken: cancellationToken);
var response = await ExecuteRequestAsync<CreateRestrictedDataTokenResponse>(
RateLimitType.Token_CreateRestrictedDataToken, cancellationToken: cancellationToken);
return response;
}
}
}