Skip to content

Commit bd26251

Browse files
authored
Merge pull request #495 from Xriuk/main
CancellationToken complete implementation + Small fixes
2 parents ec4287b + 29ba0c8 commit bd26251

31 files changed

+554
-519
lines changed

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Orders/Order.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
using Newtonsoft.Json;
1212
using Newtonsoft.Json.Converters;
13+
using RestSharp;
1314
using System;
1415
using System.Collections.Generic;
1516
using System.ComponentModel.DataAnnotations;
@@ -344,7 +345,7 @@ public Order() { }
344345
#pragma warning disable 0618
345346
public DateTime PurchaseDateTime {
346347
get => DateTime.Parse(PurchaseDate);
347-
set => PurchaseDate = value.ToString("o");
348+
set => PurchaseDate = value.ToString(DateFormat.ISO_8601);
348349
}
349350
#pragma warning restore 0618
350351

@@ -358,7 +359,7 @@ public DateTime PurchaseDateTime {
358359
#pragma warning disable 0618
359360
public DateTime LastUpdateDateTime {
360361
get => DateTime.Parse(LastUpdateDate);
361-
set => LastUpdateDate = value.ToString("o");
362+
set => LastUpdateDate = value.ToString(DateFormat.ISO_8601);
362363
}
363364
#pragma warning restore 0618
364365

@@ -460,7 +461,7 @@ public DateTime LastUpdateDateTime {
460461
#pragma warning disable 0618
461462
public DateTime? EarliestShipDateTime {
462463
get => !string.IsNullOrEmpty(EarliestShipDate) ? (DateTime?)DateTime.Parse(EarliestShipDate) : null;
463-
set => EarliestShipDate = value != null ? value.Value.ToString("o") : null;
464+
set => EarliestShipDate = value != null ? value.Value.ToString(DateFormat.ISO_8601) : null;
464465
}
465466
#pragma warning restore 0618
466467

@@ -474,7 +475,7 @@ public DateTime? EarliestShipDateTime {
474475
#pragma warning disable 0618
475476
public DateTime? LatestShipDateTime {
476477
get => !string.IsNullOrEmpty(LatestShipDate) ? (DateTime?)DateTime.Parse(LatestShipDate) : null;
477-
set => LatestShipDate = value != null ? value.Value.ToString("o") : null;
478+
set => LatestShipDate = value != null ? value.Value.ToString(DateFormat.ISO_8601) : null;
478479
}
479480
#pragma warning restore 0618
480481

@@ -488,7 +489,7 @@ public DateTime? LatestShipDateTime {
488489
#pragma warning disable 0618
489490
public DateTime? EarliestDeliveryDateTime {
490491
get => !string.IsNullOrEmpty(EarliestDeliveryDate) ? (DateTime?)DateTime.Parse(EarliestDeliveryDate) : null;
491-
set => EarliestDeliveryDate = value != null ? value.Value.ToString("o") : null;
492+
set => EarliestDeliveryDate = value != null ? value.Value.ToString(DateFormat.ISO_8601) : null;
492493
}
493494
#pragma warning restore 0618
494495

@@ -502,7 +503,7 @@ public DateTime? EarliestDeliveryDateTime {
502503
#pragma warning disable 0618
503504
public DateTime? LatestDeliveryDateTime {
504505
get => !string.IsNullOrEmpty(LatestDeliveryDate) ? (DateTime?)DateTime.Parse(LatestDeliveryDate) : null;
505-
set => LatestDeliveryDate = value != null ? value.Value.ToString("o") : null;
506+
set => LatestDeliveryDate = value != null ? value.Value.ToString(DateFormat.ISO_8601) : null;
506507
}
507508
#pragma warning restore 0618
508509

@@ -558,7 +559,7 @@ public DateTime? LatestDeliveryDateTime {
558559
#pragma warning disable 0618
559560
public DateTime? PromiseResponseDueDateTime {
560561
get => !string.IsNullOrEmpty(PromiseResponseDueDate) ? (DateTime?)DateTime.Parse(PromiseResponseDueDate) : null;
561-
set => PromiseResponseDueDate = value != null ? value.Value.ToString("o") : null;
562+
set => PromiseResponseDueDate = value != null ? value.Value.ToString(DateFormat.ISO_8601) : null;
562563
}
563564
#pragma warning restore 0618
564565

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.Authorization;
22
using FikaAmazonAPI.AmazonSpApiSDK.Models.Token;
33
using FikaAmazonAPI.Parameter.Authorization;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using static FikaAmazonAPI.AmazonSpApiSDK.Models.Token.CacheTokenData;
67

@@ -14,35 +15,35 @@ public AuthorizationService(AmazonCredential amazonCredential) : base(amazonCred
1415
public string GetAuthorizationCode(ParameterAuthorizationCode parameterGetOrderMetrics) =>
1516
Task.Run(() => GetAuthorizationCodeAsync(parameterGetOrderMetrics)).ConfigureAwait(false).GetAwaiter().GetResult();
1617

17-
public async Task<string> GetAuthorizationCodeAsync(ParameterAuthorizationCode parameter)
18+
public async Task<string> GetAuthorizationCodeAsync(ParameterAuthorizationCode parameter, CancellationToken cancellationToken = default)
1819
{
1920
var param = parameter.getParameters();
20-
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.Get, param, tokenDataType: TokenDataType.Grantless);
21+
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.Get, param, tokenDataType: TokenDataType.Grantless, cancellationToken: cancellationToken);
2122

22-
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>(Utils.RateLimitType.Authorization_GetAuthorizationCode);
23+
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>(Utils.RateLimitType.Authorization_GetAuthorizationCode, cancellationToken);
2324
if (response != null && response.Payload != null)
2425
return response.Payload._AuthorizationCode;
2526
return null;
2627
}
2728

28-
public async Task<TokenResponse> GetAccessTokenFromCodeAsync(string code, string appRedirectUri)
29+
public async Task<TokenResponse> GetAccessTokenFromCodeAsync(string code, string appRedirectUri, CancellationToken cancellationToken = default)
2930
{
30-
return await TokenGeneration.GetAccessTokenFromCodeAsync(AmazonCredential.ClientId, AmazonCredential.ClientSecret, code, appRedirectUri);
31+
return await TokenGeneration.GetAccessTokenFromCodeAsync(AmazonCredential.ClientId, AmazonCredential.ClientSecret, code, appRedirectUri, cancellationToken: cancellationToken);
3132
}
3233

33-
public async Task<TokenResponse> GetRefreshTokenFromCodeAsync(string code, string appRedirectUri)
34+
public async Task<TokenResponse> GetRefreshTokenFromCodeAsync(string code, string appRedirectUri, CancellationToken cancellationToken = default)
3435
{
35-
return await TokenGeneration.GetAccessTokenFromCodeAsync(AmazonCredential.ClientId, AmazonCredential.ClientSecret, code, appRedirectUri, grant_type: "authorization_code");
36+
return await TokenGeneration.GetAccessTokenFromCodeAsync(AmazonCredential.ClientId, AmazonCredential.ClientSecret, code, appRedirectUri, grant_type: "authorization_code", cancellationToken: cancellationToken);
3637
}
3738

38-
public static async Task<TokenResponse> GetAccessTokenFromCodeAsync(string clientId, string clientSecret, string code, string appRedirectUri)
39+
public static async Task<TokenResponse> GetAccessTokenFromCodeAsync(string clientId, string clientSecret, string code, string appRedirectUri, CancellationToken cancellationToken = default)
3940
{
40-
return await TokenGeneration.GetAccessTokenFromCodeAsync(clientId, clientSecret, code, appRedirectUri);
41+
return await TokenGeneration.GetAccessTokenFromCodeAsync(clientId, clientSecret, code, appRedirectUri, cancellationToken: cancellationToken);
4142
}
4243

43-
public static async Task<TokenResponse> GetRefreshTokenFromCodeAsync(string clientId, string clientSecret, string code, string appRedirectUri)
44+
public static async Task<TokenResponse> GetRefreshTokenFromCodeAsync(string clientId, string clientSecret, string code, string appRedirectUri, CancellationToken cancellationToken = default)
4445
{
45-
return await TokenGeneration.GetAccessTokenFromCodeAsync(clientId, clientSecret, code, appRedirectUri, grant_type: "authorization_code");
46+
return await TokenGeneration.GetAccessTokenFromCodeAsync(clientId, clientSecret, code, appRedirectUri, grant_type: "authorization_code", cancellationToken: cancellationToken);
4647
}
4748
}
4849
}

Source/FikaAmazonAPI/Services/CatalogItemService.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.IO;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using Item = FikaAmazonAPI.AmazonSpApiSDK.Models.CatalogItems.Item;
1011

@@ -76,7 +77,7 @@ public async Task<Item> GetCatalogItemAsync(string asin)
7677
public IList<Categories> ListCatalogCategories(string ASIN, string SellerSKU = null, string MarketPlaceID = null) =>
7778
Task.Run(() => ListCatalogCategoriesAsync(ASIN, SellerSKU, MarketPlaceID)).ConfigureAwait(false).GetAwaiter().GetResult();
7879

79-
public async Task<IList<Categories>> ListCatalogCategoriesAsync(string ASIN, string SellerSKU = null, string MarketPlaceID = null)
80+
public async Task<IList<Categories>> ListCatalogCategoriesAsync(string ASIN, string SellerSKU = null, string MarketPlaceID = null, CancellationToken cancellationToken = default)
8081
{
8182
if (string.IsNullOrEmpty(ASIN))
8283
throw new InvalidDataException("ASIN is a required property and cannot be null or empty");
@@ -88,8 +89,8 @@ public async Task<IList<Categories>> ListCatalogCategoriesAsync(string ASIN, str
8889
if (!string.IsNullOrEmpty(SellerSKU))
8990
param.Add(new KeyValuePair<string, string>("SellerSKU", SellerSKU));
9091

91-
await CreateAuthorizedRequestAsync(CategoryApiUrls.ListCatalogCategories, RestSharp.Method.Get, param);
92-
var response = await ExecuteRequestAsync<ListCatalogCategoriesResponse>(RateLimitType.CatalogItems_ListCatalogCategories);
92+
await CreateAuthorizedRequestAsync(CategoryApiUrls.ListCatalogCategories, RestSharp.Method.Get, param, cancellationToken: cancellationToken);
93+
var response = await ExecuteRequestAsync<ListCatalogCategoriesResponse>(RateLimitType.CatalogItems_ListCatalogCategories, cancellationToken);
9394

9495
if (response != null && response.Payload != null)
9596
return response.Payload;
@@ -106,7 +107,7 @@ public AmazonSpApiSDK.Models.CatalogItems.V20220401.Item GetCatalogItem202204(Pa
106107
/// <summary>
107108
/// Retrieves details for an item in the Amazon catalog.
108109
/// </summary>
109-
public async Task<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item> GetCatalogItem202204Async(ParameterGetCatalogItem parameterGetCatalogItem)
110+
public async Task<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item> GetCatalogItem202204Async(ParameterGetCatalogItem parameterGetCatalogItem, CancellationToken cancellationToken = default)
110111
{
111112
if (string.IsNullOrEmpty(parameterGetCatalogItem.ASIN))
112113
throw new InvalidDataException("asin is a required property and cannot be null");
@@ -118,8 +119,8 @@ public AmazonSpApiSDK.Models.CatalogItems.V20220401.Item GetCatalogItem202204(Pa
118119

119120
var param = parameterGetCatalogItem.getParameters();
120121

121-
await CreateAuthorizedRequestAsync(CategoryApiUrls.GetCatalogItem202204(parameterGetCatalogItem.ASIN), RestSharp.Method.Get, param);
122-
var response = await ExecuteRequestAsync<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item>(RateLimitType.CatalogItems20220401_GetCatalogItem);
122+
await CreateAuthorizedRequestAsync(CategoryApiUrls.GetCatalogItem202204(parameterGetCatalogItem.ASIN), RestSharp.Method.Get, param, cancellationToken: cancellationToken);
123+
var response = await ExecuteRequestAsync<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item>(RateLimitType.CatalogItems20220401_GetCatalogItem, cancellationToken);
123124

124125
return response;
125126
}
@@ -128,7 +129,7 @@ public AmazonSpApiSDK.Models.CatalogItems.V20220401.Item GetCatalogItem202204(Pa
128129
public IList<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item> SearchCatalogItems202204(ParameterSearchCatalogItems202204 parameterSearchCatalogItems) =>
129130
Task.Run(() => SearchCatalogItems202204Async(parameterSearchCatalogItems)).ConfigureAwait(false).GetAwaiter().GetResult();
130131

131-
public async Task<IList<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item>> SearchCatalogItems202204Async(ParameterSearchCatalogItems202204 parameter)
132+
public async Task<IList<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item>> SearchCatalogItems202204Async(ParameterSearchCatalogItems202204 parameter, CancellationToken cancellationToken = default)
132133
{
133134
if (parameter.identifiers != null && parameter.identifiers.Count > 20)
134135
throw new InvalidDataException("identifiers max count 20");
@@ -145,17 +146,17 @@ public AmazonSpApiSDK.Models.CatalogItems.V20220401.Item GetCatalogItem202204(Pa
145146

146147
var param = parameter.getParameters();
147148

148-
await CreateAuthorizedRequestAsync(CategoryApiUrls.SearchCatalogItems202204, RestSharp.Method.Get, param);
149-
var response = await ExecuteRequestAsync<ItemSearchResults>(RateLimitType.CatalogItems20220401_SearchCatalogItems);
149+
await CreateAuthorizedRequestAsync(CategoryApiUrls.SearchCatalogItems202204, RestSharp.Method.Get, param, cancellationToken: cancellationToken);
150+
var response = await ExecuteRequestAsync<ItemSearchResults>(RateLimitType.CatalogItems20220401_SearchCatalogItems, cancellationToken);
150151
list.AddRange(response.Items);
151152
var totalPages = 1;
152153
if (response.Pagination != null && !string.IsNullOrEmpty(response.Pagination.NextToken))
153154
{
154155
var nextToken = response.Pagination.NextToken;
155156
while (!string.IsNullOrEmpty(nextToken) && (!parameter.maxPages.HasValue || totalPages < parameter.maxPages.Value))
156157
{
157-
parameter.pageToken = nextToken;
158-
var getItemNextPage = await SearchCatalogItemsByNextToken202204Async(parameter);
158+
parameter.pageToken = nextToken;
159+
var getItemNextPage = await SearchCatalogItemsByNextToken202204Async(parameter, cancellationToken);
159160
list.AddRange(getItemNextPage.Items);
160161
nextToken = getItemNextPage.Pagination?.NextToken;
161162
totalPages++;
@@ -164,15 +165,15 @@ public AmazonSpApiSDK.Models.CatalogItems.V20220401.Item GetCatalogItem202204(Pa
164165
return list;
165166
}
166167

167-
private async Task<ItemSearchResults> SearchCatalogItemsByNextToken202204Async(ParameterSearchCatalogItems202204 parameter)
168+
private async Task<ItemSearchResults> SearchCatalogItemsByNextToken202204Async(ParameterSearchCatalogItems202204 parameter, CancellationToken cancellationToken = default)
168169
{
169170

170171
List<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item> list = new List<AmazonSpApiSDK.Models.CatalogItems.V20220401.Item>();
171172

172173
var param = parameter.getParameters();
173174

174-
await CreateAuthorizedRequestAsync(CategoryApiUrls.SearchCatalogItems202204, RestSharp.Method.Get, param);
175-
return await ExecuteRequestAsync<ItemSearchResults>(RateLimitType.CatalogItems20220401_SearchCatalogItems);
175+
await CreateAuthorizedRequestAsync(CategoryApiUrls.SearchCatalogItems202204, RestSharp.Method.Get, param, cancellationToken: cancellationToken);
176+
return await ExecuteRequestAsync<ItemSearchResults>(RateLimitType.CatalogItems20220401_SearchCatalogItems, cancellationToken);
176177
}
177178
#endregion
178179
}

0 commit comments

Comments
 (0)