Skip to content

Commit 57e8f75

Browse files
authored
Merge pull request #118 from abuzuhri/async-api-request
Async api request
2 parents 4723259 + e04999a commit 57e8f75

27 files changed

+939
-507
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static async Task Main(string[] args)
3131
IsActiveLimitRate = true
3232
});
3333

34+
var seller = await amazonConnection.Seller.GetMarketplaceParticipationsAsync();
35+
3436
var dataaa = amazonConnection.FbaInventory.GetInventorySummaries(new Parameter.FbaInventory.ParameterGetInventorySummaries
3537
{
3638
granularityType = AmazonSpApiSDK.Models.FbaInventory.Granularity.GranularityTypeEnum.Marketplace,

Source/FikaAmazonAPI/AmazonSpApiSDK/Runtime/LWAClient.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using RestSharp;
44
using System;
55
using System.IO;
6+
using System.Threading.Tasks;
67

78
namespace FikaAmazonAPI.AmazonSpApiSDK.Runtime
89
{
@@ -30,17 +31,27 @@ public LWAClient(LWAAuthorizationCredentials lwaAuthorizationCredentials)
3031
/// <param name="lwaAccessTokenRequestMeta">LWA AccessTokenRequest metadata</param>
3132
/// <returns>LWA Access Token</returns>
3233
public virtual TokenResponse GetAccessToken()
34+
{
35+
return GetAccessTokenAsync().GetAwaiter().GetResult();
36+
}
37+
38+
/// <summary>
39+
/// Retrieves access token from LWA
40+
/// </summary>
41+
/// <param name="lwaAccessTokenRequestMeta">LWA AccessTokenRequest metadata</param>
42+
/// <returns>LWA Access Token</returns>
43+
public virtual async Task<TokenResponse> GetAccessTokenAsync()
3344
{
3445
LWAAccessTokenRequestMeta lwaAccessTokenRequestMeta = LWAAccessTokenRequestMetaBuilder.Build(LWAAuthorizationCredentials);
35-
var accessTokenRequest = new RestRequest(LWAAuthorizationCredentials.Endpoint.AbsolutePath,RestSharp.Method.POST);
46+
var accessTokenRequest = new RestRequest(LWAAuthorizationCredentials.Endpoint.AbsolutePath, RestSharp.Method.POST);
3647

3748
string jsonRequestBody = JsonConvert.SerializeObject(lwaAccessTokenRequestMeta);
3849

3950
accessTokenRequest.AddParameter(JsonMediaType, jsonRequestBody, ParameterType.RequestBody);
4051

4152
try
4253
{
43-
var response = RestClient.Execute(accessTokenRequest);
54+
var response = await RestClient.ExecuteAsync(accessTokenRequest);
4455

4556
if (!IsSuccessful(response))
4657
{

Source/FikaAmazonAPI/Services/CatalogItemService.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using FikaAmazonAPI.Utils;
44
using System.Collections.Generic;
55
using System.IO;
6+
using System.Threading.Tasks;
67

78
namespace FikaAmazonAPI.Services
89
{
@@ -14,7 +15,10 @@ public CatalogItemService(AmazonCredential amazonCredential) : base(amazonCreden
1415
}
1516

1617

17-
public IList<Item> ListCatalogItems(ParameterListCatalogItems parameterListCatalogItems)
18+
public IList<Item> ListCatalogItems(ParameterListCatalogItems parameterListCatalogItems) =>
19+
ListCatalogItemsAsync(parameterListCatalogItems).GetAwaiter().GetResult();
20+
21+
public async Task<IList<Item>> ListCatalogItemsAsync(ParameterListCatalogItems parameterListCatalogItems)
1822
{
1923
if (string.IsNullOrEmpty(parameterListCatalogItems.MarketplaceId))
2024
parameterListCatalogItems.MarketplaceId = AmazonCredential.MarketPlace.ID;
@@ -33,8 +37,8 @@ public IList<Item> ListCatalogItems(ParameterListCatalogItems parameterListCatal
3337

3438
var parameter = parameterListCatalogItems.getParameters();
3539

36-
CreateAuthorizedRequest(CategoryApiUrls.ListCatalogItems, RestSharp.Method.GET, parameter);
37-
var response = ExecuteRequest<ListCatalogItemsResponse>(RateLimitType.CatalogItems_ListCatalogItems);
40+
await CreateAuthorizedRequestAsync(CategoryApiUrls.ListCatalogItems, RestSharp.Method.GET, parameter);
41+
var response = await ExecuteRequestAsync<ListCatalogItemsResponse>(RateLimitType.CatalogItems_ListCatalogItems);
3842

3943
if (response != null && response.Payload != null && response.Payload.Items != null && response.Payload.Items.Count > 0)
4044
list.AddRange(response.Payload.Items);
@@ -43,7 +47,10 @@ public IList<Item> ListCatalogItems(ParameterListCatalogItems parameterListCatal
4347
}
4448

4549
//[Obsolete("This method is will be deprecated in June 2022. Please use GetCatalogItem(ParameterGetCatalogItem parameterListCatalogItem) instead.")]
46-
public Item GetCatalogItem(string asin)
50+
public Item GetCatalogItem(string asin) =>
51+
GetCatalogItemAsync(asin).GetAwaiter().GetResult();
52+
53+
public async Task<Item> GetCatalogItemAsync(string asin)
4754
{
4855

4956
if (string.IsNullOrEmpty(asin))
@@ -52,8 +59,8 @@ public Item GetCatalogItem(string asin)
5259
var param = new List<KeyValuePair<string, string>>();
5360
param.Add(new KeyValuePair<string, string>("MarketplaceId", AmazonCredential.MarketPlace.ID));
5461

55-
CreateAuthorizedRequest(CategoryApiUrls.GetCatalogItem(asin), RestSharp.Method.GET, param);
56-
var response = ExecuteRequest<GetCatalogItemResponse>(RateLimitType.CatalogItems_GetCatalogItem);
62+
await CreateAuthorizedRequestAsync(CategoryApiUrls.GetCatalogItem(asin), RestSharp.Method.GET, param);
63+
var response = await ExecuteRequestAsync<GetCatalogItemResponse>(RateLimitType.CatalogItems_GetCatalogItem);
5764

5865
if (response != null && response.Payload != null)
5966
return response.Payload;
@@ -74,13 +81,16 @@ public Item GetCatalogItem(string asin)
7481

7582
// var param = parameterListCatalogItem.getParameters();
7683

77-
// CreateAuthorizedRequest(CategoryApiUrls.GetCatalogItem202012(parameterListCatalogItem.ASIN), RestSharp.Method.GET, param);
78-
// var response = ExecuteRequest<Item>();
84+
// await CreateAuthorizedRequestAsync(CategoryApiUrls.GetCatalogItem202012(parameterListCatalogItem.ASIN), RestSharp.Method.GET, param);
85+
// var response = await ExecuteRequestAsync<Item>();
7986

8087
// return response;
8188
//}
8289

83-
public IList<Categories> ListCatalogCategories(string ASIN,string SellerSKU=null,string MarketPlaceID = null)
90+
public IList<Categories> ListCatalogCategories(string ASIN, string SellerSKU = null, string MarketPlaceID = null) =>
91+
ListCatalogCategoriesAsync(ASIN, SellerSKU, MarketPlaceID).GetAwaiter().GetResult();
92+
93+
public async Task<IList<Categories>> ListCatalogCategoriesAsync(string ASIN, string SellerSKU = null, string MarketPlaceID = null)
8494
{
8595
if (string.IsNullOrEmpty(ASIN))
8696
throw new InvalidDataException("ASIN is a required property and cannot be null or empty");
@@ -89,17 +99,16 @@ public IList<Categories> ListCatalogCategories(string ASIN,string SellerSKU=null
8999
var param = new List<KeyValuePair<string, string>>();
90100
param.Add(new KeyValuePair<string, string>("MarketplaceId", MarketPlaceID ?? AmazonCredential.MarketPlace.ID));
91101
param.Add(new KeyValuePair<string, string>("ASIN", ASIN));
92-
if(!string.IsNullOrEmpty(SellerSKU))
102+
if (!string.IsNullOrEmpty(SellerSKU))
93103
param.Add(new KeyValuePair<string, string>("SellerSKU", SellerSKU));
94104

95-
CreateAuthorizedRequest(CategoryApiUrls.ListCatalogCategories, RestSharp.Method.GET, param);
96-
var response = ExecuteRequest<ListCatalogCategoriesResponse>(RateLimitType.CatalogItems_ListCatalogCategories);
105+
await CreateAuthorizedRequestAsync(CategoryApiUrls.ListCatalogCategories, RestSharp.Method.GET, param);
106+
var response = await ExecuteRequestAsync<ListCatalogCategoriesResponse>(RateLimitType.CatalogItems_ListCatalogCategories);
97107

98108
if (response != null && response.Payload != null)
99109
return response.Payload;
100110

101111
return null;
102112
}
103-
104113
}
105114
}

Source/FikaAmazonAPI/Services/FbaInboundEligibilityService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.FbaInbound;
22
using FikaAmazonAPI.Parameter.FbaInboundEligibility;
33
using FikaAmazonAPI.Utils;
4+
using System.Threading.Tasks;
45

56
namespace FikaAmazonAPI.Services
67
{
@@ -11,11 +12,14 @@ public FbaInboundEligibilityService(AmazonCredential amazonCredential) : base(am
1112

1213
}
1314

14-
public ItemEligibilityPreview GetItemEligibilityPreview(ParameterGetItemEligibilityPreview parameterGetItemEligibilityPreview)
15+
public ItemEligibilityPreview GetItemEligibilityPreview(ParameterGetItemEligibilityPreview parameterGetItemEligibilityPreview) =>
16+
GetItemEligibilityPreviewAsync(parameterGetItemEligibilityPreview).GetAwaiter().GetResult();
17+
18+
public async Task<ItemEligibilityPreview> GetItemEligibilityPreviewAsync(ParameterGetItemEligibilityPreview parameterGetItemEligibilityPreview)
1519
{
1620
var parameter = parameterGetItemEligibilityPreview.getParameters();
17-
CreateAuthorizedRequest(FBAInboundEligibiltyApiUrls.GetItemEligibilityPreview, RestSharp.Method.GET, parameter);
18-
var response = ExecuteRequest<GetItemEligibilityPreviewResponse>(RateLimitType.FBAInboundEligibility_GetItemEligibilityPreview);
21+
await CreateAuthorizedRequestAsync(FBAInboundEligibiltyApiUrls.GetItemEligibilityPreview, RestSharp.Method.GET, parameter);
22+
var response = await ExecuteRequestAsync<GetItemEligibilityPreviewResponse>(RateLimitType.FBAInboundEligibility_GetItemEligibilityPreview);
1923
if (response != null && response.Payload != null)
2024
return response.Payload;
2125
return null;

Source/FikaAmazonAPI/Services/FbaInventoryService.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using FikaAmazonAPI.Parameter.FbaInventory;
33
using FikaAmazonAPI.Utils;
44
using System.Collections.Generic;
5+
using System.Threading.Tasks;
56

67
namespace FikaAmazonAPI.Services
78
{
@@ -14,7 +15,10 @@ public FbaInventoryService(AmazonCredential amazonCredential) : base(amazonCrede
1415
}
1516

1617

17-
public List<InventorySummaries> GetInventorySummaries(ParameterGetInventorySummaries parameter)
18+
public List<InventorySummaries> GetInventorySummaries(ParameterGetInventorySummaries parameter) =>
19+
GetInventorySummariesAsync(parameter).GetAwaiter().GetResult();
20+
21+
public async Task<List<InventorySummaries>> GetInventorySummariesAsync(ParameterGetInventorySummaries parameter)
1822
{
1923
if (parameter.marketplaceIds == null || parameter.marketplaceIds.Count == 0)
2024
{
@@ -25,8 +29,8 @@ public List<InventorySummaries> GetInventorySummaries(ParameterGetInventorySumma
2529
var list = new List<InventorySummaries>();
2630
var param = parameter.getParameters();
2731

28-
CreateAuthorizedRequest(FbaInventoriesApiUrls.GetInventorySummaries, RestSharp.Method.GET, param);
29-
var response = ExecuteRequest<GetInventorySummariesResponse>(RateLimitType.FbaInventory_GetInventorySummaries);
32+
await CreateAuthorizedRequestAsync(FbaInventoriesApiUrls.GetInventorySummaries, RestSharp.Method.GET, param);
33+
var response = await ExecuteRequestAsync<GetInventorySummariesResponse>(RateLimitType.FbaInventory_GetInventorySummaries);
3034
list.Add(response.Payload.InventorySummaries);
3135
if (response.Pagination != null && !string.IsNullOrEmpty(response.Pagination.NextToken))
3236
{
@@ -41,13 +45,16 @@ public List<InventorySummaries> GetInventorySummaries(ParameterGetInventorySumma
4145
return list;
4246
}
4347

44-
private GetInventorySummariesResponse GetInventorySummariesByNextToken(string nextToken, ParameterGetInventorySummaries parameterGetInventorySummaries)
48+
public GetInventorySummariesResponse GetInventorySummariesByNextToken(string nextToken, ParameterGetInventorySummaries parameterGetInventorySummaries) =>
49+
GetInventorySummariesByNextTokenAsync(nextToken, parameterGetInventorySummaries).GetAwaiter().GetResult();
50+
51+
public async Task<GetInventorySummariesResponse> GetInventorySummariesByNextTokenAsync(string nextToken, ParameterGetInventorySummaries parameterGetInventorySummaries)
4552
{
4653
parameterGetInventorySummaries.nextToken = nextToken;
4754
var param = parameterGetInventorySummaries.getParameters();
4855

49-
CreateAuthorizedRequest(FbaInventoriesApiUrls.GetInventorySummaries, RestSharp.Method.GET, param);
50-
var response = ExecuteRequest<GetInventorySummariesResponse>(RateLimitType.FbaInventory_GetInventorySummaries);
56+
await CreateAuthorizedRequestAsync(FbaInventoriesApiUrls.GetInventorySummaries, RestSharp.Method.GET, param);
57+
var response = await ExecuteRequestAsync<GetInventorySummariesResponse>(RateLimitType.FbaInventory_GetInventorySummaries);
5158

5259
return response;
5360
}

Source/FikaAmazonAPI/Services/FbaSmallandLightService.cs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.FbaSmallandLight;
22
using FikaAmazonAPI.Utils;
33
using System.Collections.Generic;
4+
using System.Threading.Tasks;
45

56
namespace FikaAmazonAPI.Services
67
{
@@ -13,67 +14,85 @@ public FbaSmallandLightService(AmazonCredential amazonCredential) : base(amazonC
1314
}
1415

1516

16-
public SmallAndLightEnrollment GetSmallAndLightEnrollmentBySellerSKU(string sellerSKU)
17+
public SmallAndLightEnrollment GetSmallAndLightEnrollmentBySellerSKU(string sellerSKU) =>
18+
GetSmallAndLightEnrollmentBySellerSKUAsync(sellerSKU).GetAwaiter().GetResult();
19+
20+
public async Task<SmallAndLightEnrollment> GetSmallAndLightEnrollmentBySellerSKUAsync(string sellerSKU)
1721
{
1822
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
1923
queryParameters.Add(new KeyValuePair<string, string>("marketplaceIds", AmazonCredential.MarketPlace.ID));
2024

21-
CreateAuthorizedRequest(FBASmallAndLightApiUrls.GetSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.GET);
25+
await CreateAuthorizedRequestAsync(FBASmallAndLightApiUrls.GetSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.GET);
2226

23-
var response = ExecuteRequest<SmallAndLightEnrollment>(RateLimitType.FbaSmallandLight_GetSmallAndLightEnrollmentBySellerSKU);
27+
var response = await ExecuteRequestAsync<SmallAndLightEnrollment>(RateLimitType.FbaSmallandLight_GetSmallAndLightEnrollmentBySellerSKU);
2428

2529
return response;
2630

2731
}
28-
public SmallAndLightEnrollment PutSmallAndLightEnrollmentBySellerSKU(string sellerSKU)
32+
33+
public SmallAndLightEnrollment PutSmallAndLightEnrollmentBySellerSKU(string sellerSKU) =>
34+
PutSmallAndLightEnrollmentBySellerSKUAsync(sellerSKU).GetAwaiter().GetResult();
35+
36+
public async Task<SmallAndLightEnrollment> PutSmallAndLightEnrollmentBySellerSKUAsync(string sellerSKU)
2937
{
3038
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
3139
queryParameters.Add(new KeyValuePair<string, string>("marketplaceIds", AmazonCredential.MarketPlace.ID));
3240

3341

34-
CreateAuthorizedRequest(FBASmallAndLightApiUrls.PutSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.PUT, queryParameters);
42+
await CreateAuthorizedRequestAsync(FBASmallAndLightApiUrls.PutSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.PUT, queryParameters);
3543

36-
var response = ExecuteRequest<SmallAndLightEnrollment>(RateLimitType.FbaSmallandLight_PutSmallAndLightEnrollmentBySellerSKU);
44+
var response = await ExecuteRequestAsync<SmallAndLightEnrollment>(RateLimitType.FbaSmallandLight_PutSmallAndLightEnrollmentBySellerSKU);
3745

3846
return response;
3947

4048
}
41-
public bool DeleteSmallAndLightEnrollmentBySellerSKU(string sellerSKU)
49+
50+
public bool DeleteSmallAndLightEnrollmentBySellerSKU(string sellerSKU) =>
51+
DeleteSmallAndLightEnrollmentBySellerSKUAsync(sellerSKU).GetAwaiter().GetResult();
52+
53+
public async Task<bool> DeleteSmallAndLightEnrollmentBySellerSKUAsync(string sellerSKU)
4254
{
4355
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
4456
queryParameters.Add(new KeyValuePair<string, string>("marketplaceIds", AmazonCredential.MarketPlace.ID));
4557

4658

47-
CreateAuthorizedRequest(FBASmallAndLightApiUrls.DeleteSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.DELETE, queryParameters);
59+
await CreateAuthorizedRequestAsync(FBASmallAndLightApiUrls.DeleteSmallAndLightEnrollmentBySellerSKU(sellerSKU), RestSharp.Method.DELETE, queryParameters);
4860

49-
var response = ExecuteRequest<object>(RateLimitType.FbaSmallandLight_DeleteSmallAndLightEnrollmentBySellerSKU);
61+
var response = await ExecuteRequestAsync<object>(RateLimitType.FbaSmallandLight_DeleteSmallAndLightEnrollmentBySellerSKU);
5062

5163
return true;
5264

5365
}
5466

55-
public SmallAndLightEligibility GetSmallAndLightEligibilityBySellerSKU(string sellerSKU)
67+
public SmallAndLightEligibility GetSmallAndLightEligibilityBySellerSKU(string sellerSKU) =>
68+
GetSmallAndLightEligibilityBySellerSKUAsync(sellerSKU).GetAwaiter().GetResult();
69+
70+
public async Task<SmallAndLightEligibility> GetSmallAndLightEligibilityBySellerSKUAsync(string sellerSKU)
5671
{
5772

5873
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
5974
queryParameters.Add(new KeyValuePair<string, string>("marketplaceIds", AmazonCredential.MarketPlace.ID));
6075

6176

62-
CreateAuthorizedRequest(FBASmallAndLightApiUrls.GetSmallAndLightEligibilityBySellerSKU(sellerSKU), RestSharp.Method.GET, queryParameters);
77+
await CreateAuthorizedRequestAsync(FBASmallAndLightApiUrls.GetSmallAndLightEligibilityBySellerSKU(sellerSKU), RestSharp.Method.GET, queryParameters);
6378

64-
var response = ExecuteRequest<SmallAndLightEligibility>(RateLimitType.FbaSmallandLight_GetSmallAndLightEligibilityBySellerSKU);
79+
var response = await ExecuteRequestAsync<SmallAndLightEligibility>(RateLimitType.FbaSmallandLight_GetSmallAndLightEligibilityBySellerSKU);
6580

6681
return response;
6782

6883
}
69-
public List<FeePreview> GetSmallAndLightFeePreview(SmallAndLightFeePreviewRequest smallAndLightFeePreviewRequest)
84+
85+
public List<FeePreview> GetSmallAndLightFeePreview(SmallAndLightFeePreviewRequest smallAndLightFeePreviewRequest) =>
86+
GetSmallAndLightFeePreviewAsync(smallAndLightFeePreviewRequest).GetAwaiter().GetResult();
87+
88+
public async Task<List<FeePreview>> GetSmallAndLightFeePreviewAsync(SmallAndLightFeePreviewRequest smallAndLightFeePreviewRequest)
7089
{
7190
List<KeyValuePair<string, string>> queryParameters = new List<KeyValuePair<string, string>>();
7291
queryParameters.Add(new KeyValuePair<string, string>("marketplaceIds", AmazonCredential.MarketPlace.ID));
7392

74-
CreateAuthorizedRequest(FBASmallAndLightApiUrls.GetSmallAndLightFeePreview, RestSharp.Method.POST,postJsonObj: smallAndLightFeePreviewRequest);
93+
await CreateAuthorizedRequestAsync(FBASmallAndLightApiUrls.GetSmallAndLightFeePreview, RestSharp.Method.POST, postJsonObj: smallAndLightFeePreviewRequest);
7594

76-
var response = ExecuteRequest<SmallAndLightFeePreviews>(RateLimitType.FbaSmallandLight_GetSmallAndLightFeePreview);
95+
var response = await ExecuteRequestAsync<SmallAndLightFeePreviews>(RateLimitType.FbaSmallandLight_GetSmallAndLightFeePreview);
7796
if (response != null && response.Data != null)
7897
return response.Data;
7998

0 commit comments

Comments
 (0)