Skip to content

Commit 64934ae

Browse files
authored
Merge pull request #119 from abuzuhri/async-api-request
Async api request
2 parents 57e8f75 + 71e12aa commit 64934ae

File tree

2 files changed

+76
-61
lines changed

2 files changed

+76
-61
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ static async Task Main(string[] args)
3333

3434
var seller = await amazonConnection.Seller.GetMarketplaceParticipationsAsync();
3535

36-
var dataaa = amazonConnection.FbaInventory.GetInventorySummaries(new Parameter.FbaInventory.ParameterGetInventorySummaries
36+
var dataaa = await amazonConnection.FbaInventory.GetInventorySummariesAsync(new Parameter.FbaInventory.ParameterGetInventorySummaries
3737
{
3838
granularityType = AmazonSpApiSDK.Models.FbaInventory.Granularity.GranularityTypeEnum.Marketplace,
3939
granularityId = config.GetSection("FikaAmazonAPI:MarketPlaceID").Value
40-
}); ;
40+
});
4141

4242
ReportManager reportManager = new ReportManager(amazonConnection);
4343
var products = reportManager.GetProducts(); //GET_MERCHANT_LISTINGS_ALL_DATA
4444
var inventoryAging = reportManager.GetInventoryAging(); //GET_FBA_INVENTORY_AGED_DATA
45-
var ordersByDate = reportManager.GetOrdersByOrderDate(90); //GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL
46-
var ordersByLastUpdate = reportManager.GetOrdersByLastUpdate(90); //GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_GENERAL
45+
var ordersByDate = await reportManager.GetOrdersByOrderDateAsync(90); //GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL
46+
var ordersByLastUpdate = await reportManager.GetOrdersByLastUpdateAsync(90); //GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_GENERAL
4747
var settlementOrder = reportManager.GetSettlementOrder(90); //GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2
4848
var returnMFNOrder = reportManager.GetReturnMFNOrder(90); //GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE
4949
var returnFBAOrder = reportManager.GetReturnFBAOrder(90); //GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA

Source/FikaAmazonAPI/ReportGeneration/ReportManager.cs

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Threading.Tasks;
34
using static FikaAmazonAPI.Utils.Constants;
45

56
namespace FikaAmazonAPI.ReportGeneration
@@ -16,21 +17,23 @@ public ReportManager(AmazonConnection amazonConnection)
1617
}
1718

1819
#region feedback
19-
public List<FeedbackOrderRow> GetFeedbackFromDays(int days)
20+
public List<FeedbackOrderRow> GetFeedbackFromDays(int days) =>
21+
GetFeedbackFromDaysAsync(days).GetAwaiter().GetResult();
22+
public async Task<List<FeedbackOrderRow>> GetFeedbackFromDaysAsync(int days)
2023
{
2124
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
2225
DateTime toDate = DateTime.UtcNow;
23-
return GetFeedbackFromDate(fromDate,toDate);
26+
return await GetFeedbackFromDateAsync(fromDate, toDate);
2427
}
25-
public List<FeedbackOrderRow> GetFeedbackFromDate(DateTime fromDate, DateTime toDate)
28+
public async Task<List<FeedbackOrderRow>> GetFeedbackFromDateAsync(DateTime fromDate, DateTime toDate)
2629
{
27-
var path = GetFeedbackFromDate(_amazonConnection, fromDate, toDate);
30+
var path = await GetFeedbackFromDateAsync(_amazonConnection, fromDate, toDate);
2831
FeedbackOrderReport report = new FeedbackOrderReport(path, _amazonConnection.RefNumber);
2932
return report.Data;
3033
}
31-
private string GetFeedbackFromDate(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
34+
private async Task<string> GetFeedbackFromDateAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
3235
{
33-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_SELLER_FEEDBACK_DATA, fromDate);
36+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_SELLER_FEEDBACK_DATA, fromDate);
3437
}
3538

3639
#endregion
@@ -39,93 +42,97 @@ private string GetFeedbackFromDate(AmazonConnection amazonConnection, DateTime f
3942
#endregion
4043

4144
#region Reimbursement
42-
public IList<ReimbursementsOrderRow> GetReimbursementsOrder(int days)
45+
public IList<ReimbursementsOrderRow> GetReimbursementsOrder(int days) =>
46+
GetReimbursementsOrderAsync(days).GetAwaiter().GetResult();
47+
public async Task<IList<ReimbursementsOrderRow>> GetReimbursementsOrderAsync(int days)
4348
{
4449
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
4550
DateTime toDate = DateTime.UtcNow;
46-
return GetReimbursementsOrder(fromDate, toDate);
51+
return await GetReimbursementsOrderAsync(fromDate, toDate);
4752
}
48-
public IList<ReimbursementsOrderRow> GetReimbursementsOrder(DateTime fromDate, DateTime toDate)
53+
public async Task<IList<ReimbursementsOrderRow>> GetReimbursementsOrderAsync(DateTime fromDate, DateTime toDate)
4954
{
50-
var path = GetReimbursementsOrder(_amazonConnection, fromDate, toDate);
55+
var path = await GetReimbursementsOrderAsync(_amazonConnection, fromDate, toDate);
5156
ReimbursementsOrderReport report = new ReimbursementsOrderReport(path, _amazonConnection.RefNumber);
5257
return report.Data;
5358
}
5459

55-
private string GetReimbursementsOrder(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
60+
private async Task<string> GetReimbursementsOrderAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
5661
{
57-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FBA_REIMBURSEMENTS_DATA, fromDate, toDate);
62+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FBA_REIMBURSEMENTS_DATA, fromDate, toDate);
5863
}
59-
60-
6164
#endregion
6265

6366
#region ReturnFBAOrder
64-
public List<ReturnFBAOrderRow> GetReturnFBAOrder(int days)
67+
public List<ReturnFBAOrderRow> GetReturnFBAOrder(int days) =>
68+
GetReturnFBAOrderAsync(days).GetAwaiter().GetResult();
69+
public async Task<List<ReturnFBAOrderRow>> GetReturnFBAOrderAsync(int days)
6570
{
6671
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
6772
DateTime toDate = DateTime.UtcNow;
68-
return GetReturnFBAOrder(fromDate, toDate);
73+
return await GetReturnFBAOrderAsync(fromDate, toDate);
6974
}
70-
public List<ReturnFBAOrderRow> GetReturnFBAOrder(DateTime fromDate, DateTime toDate)
75+
public async Task<List<ReturnFBAOrderRow>> GetReturnFBAOrderAsync(DateTime fromDate, DateTime toDate)
7176
{
72-
var path = GetReturnFBAOrder(_amazonConnection, fromDate, toDate);
77+
var path = await GetReturnFBAOrderAsync(_amazonConnection, fromDate, toDate);
7378
ReturnFBAOrderReport report = new ReturnFBAOrderReport(path, _amazonConnection.RefNumber);
74-
79+
7580
return report.Data;
7681
}
7782

78-
private string GetReturnFBAOrder(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
83+
private async Task<string> GetReturnFBAOrderAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
7984
{
80-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA, fromDate, toDate);
85+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FBA_FULFILLMENT_CUSTOMER_RETURNS_DATA, fromDate, toDate);
8186
}
8287

8388

8489
#endregion
8590

8691
#region ReturnFBMOrder
87-
public List<ReturnFBMOrderRow> GetReturnMFNOrder(int days)
92+
public List<ReturnFBMOrderRow> GetReturnMFNOrder(int days) =>
93+
GetReturnMFNOrderAsync(days).GetAwaiter().GetResult();
94+
public async Task<List<ReturnFBMOrderRow>> GetReturnMFNOrderAsync(int days)
8895
{
8996
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
9097
DateTime toDate = DateTime.UtcNow;
91-
return GetReturnMFNOrder(fromDate, toDate);
98+
return await GetReturnMFNOrderAsync(fromDate, toDate);
9299
}
93-
public List<ReturnFBMOrderRow> GetReturnMFNOrder(DateTime fromDate, DateTime toDate)
100+
public async Task<List<ReturnFBMOrderRow>> GetReturnMFNOrderAsync(DateTime fromDate, DateTime toDate)
94101
{
95-
List<ReturnFBMOrderRow> list=new List<ReturnFBMOrderRow>();
102+
List<ReturnFBMOrderRow> list = new List<ReturnFBMOrderRow>();
96103
var dateList = ReportDateRange.GetDateRange(fromDate, toDate, DAY_60);
97104
foreach (var date in dateList)
98105
{
99-
var path = GetReturnMFNOrder(_amazonConnection, date.StartDate, date.EndDate);
106+
var path = await GetReturnMFNOrderAsync(_amazonConnection, date.StartDate, date.EndDate);
100107

101108
ReturnFBMOrderReport report = new ReturnFBMOrderReport(path, _amazonConnection.RefNumber);
102109
list.AddRange(report.Data);
103110
}
104111
return list;
105112
}
106-
public string GetReturnMFNOrder(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
113+
public async Task<string> GetReturnMFNOrderAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
107114
{
108-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE, fromDate, toDate);
115+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FLAT_FILE_RETURNS_DATA_BY_RETURN_DATE, fromDate, toDate);
109116
}
110-
111-
112117
#endregion
113118

114119
#region Settlement
115-
public List<SettlementOrderRow> GetSettlementOrder(int days)
120+
public List<SettlementOrderRow> GetSettlementOrder(int days) =>
121+
GetSettlementOrderAsync(days).GetAwaiter().GetResult();
122+
public async Task<List<SettlementOrderRow>> GetSettlementOrderAsync(int days)
116123
{
117124
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
118125
DateTime toDate = DateTime.UtcNow;
119-
return GetSettlementOrder(fromDate, toDate);
126+
return await GetSettlementOrderAsync(fromDate, toDate);
120127
}
121-
public List<SettlementOrderRow> GetSettlementOrder(DateTime fromDate, DateTime toDate)
128+
public async Task<List<SettlementOrderRow>> GetSettlementOrderAsync(DateTime fromDate, DateTime toDate)
122129
{
123130
List<SettlementOrderRow> list = new List<SettlementOrderRow>();
124131
var totalDays = (DateTime.UtcNow - fromDate).TotalDays;
125132
if (totalDays > 90)
126133
fromDate = DateTime.UtcNow.AddDays(-90);
127134

128-
var paths = GetSettlementOrder(_amazonConnection, fromDate, toDate);
135+
var paths = await GetSettlementOrderAsync(_amazonConnection, fromDate, toDate);
129136
foreach (var path in paths)
130137
{
131138
SettlementOrderReport report = new SettlementOrderReport(path, _amazonConnection.RefNumber);
@@ -134,9 +141,9 @@ public List<SettlementOrderRow> GetSettlementOrder(DateTime fromDate, DateTime t
134141

135142
return list;
136143
}
137-
private IList<string> GetSettlementOrder(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
144+
private async Task<IList<string>> GetSettlementOrderAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
138145
{
139-
return amazonConnection.Reports.DownloadExistingReportAndDownloadFile(ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2, fromDate, toDate);
146+
return await amazonConnection.Reports.DownloadExistingReportAndDownloadFileAsync(ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2, fromDate, toDate);
140147
}
141148
#endregion
142149

@@ -153,76 +160,84 @@ private string GetInventoryQty(AmazonConnection amazonConnection)
153160
#endregion
154161

155162
#region GetInventoryAging
156-
public List<InventoryAgingRow> GetInventoryAging()
163+
public List<InventoryAgingRow> GetInventoryAging() =>
164+
GetInventoryAgingAsync().GetAwaiter().GetResult();
165+
public async Task<List<InventoryAgingRow>> GetInventoryAgingAsync()
157166
{
158-
var path = GetInventoryAging(_amazonConnection);
167+
var path = await GetInventoryAgingAsync(_amazonConnection);
159168
InventoryAgingReport report = new InventoryAgingReport(path, _amazonConnection.RefNumber);
160169
return report.Data;
161170
}
162-
private string GetInventoryAging(AmazonConnection amazonConnection)
171+
private async Task<string> GetInventoryAgingAsync(AmazonConnection amazonConnection)
163172
{
164-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FBA_INVENTORY_AGED_DATA);
173+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FBA_INVENTORY_AGED_DATA);
165174
}
166175
#endregion
167176

168177
#region Products
169-
public List<ProductsRow> GetProducts()
178+
public List<ProductsRow> GetProducts() =>
179+
GetProductsAsync().GetAwaiter().GetResult();
180+
public async Task<List<ProductsRow>> GetProductsAsync()
170181
{
171-
var path = GetProducts(_amazonConnection);
182+
var path = await GetProductsAsync(_amazonConnection);
172183
ProductsReport report = new ProductsReport(path, _amazonConnection.RefNumber);
173184
return report.Data;
174185
}
175-
private string GetProducts(AmazonConnection amazonConnection)
186+
private async Task<string> GetProductsAsync(AmazonConnection amazonConnection)
176187
{
177-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_MERCHANT_LISTINGS_ALL_DATA);
188+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_MERCHANT_LISTINGS_ALL_DATA);
178189
}
179190
#endregion
180191

181192
#region Orders
182-
public List<OrdersRow> GetOrdersByLastUpdate(int days)
193+
public List<OrdersRow> GetOrdersByLastUpdate(int days) =>
194+
GetOrdersByLastUpdateAsync(days).GetAwaiter().GetResult();
195+
public async Task<List<OrdersRow>> GetOrdersByLastUpdateAsync(int days)
183196
{
184197
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
185198
DateTime toDate = DateTime.UtcNow;
186-
return GetOrdersByLastUpdate(fromDate, toDate);
199+
return await GetOrdersByLastUpdateAsync(fromDate, toDate);
187200
}
188-
public List<OrdersRow> GetOrdersByLastUpdate(DateTime fromDate, DateTime toDate)
201+
public async Task<List<OrdersRow>> GetOrdersByLastUpdateAsync(DateTime fromDate, DateTime toDate)
189202
{
190203
List<OrdersRow> list = new List<OrdersRow>();
191204
var dateList = ReportDateRange.GetDateRange(fromDate, toDate, DAY_30);
192205
foreach (var range in dateList)
193206
{
194-
var path = GetOrdersByLastUpdate(_amazonConnection, range.StartDate, range.EndDate);
207+
var path = await GetOrdersByLastUpdateAsync(_amazonConnection, range.StartDate, range.EndDate);
195208
OrdersReport report = new OrdersReport(path, _amazonConnection.RefNumber);
196209
list.AddRange(report.Data);
197210
}
198211
return list;
199212
}
200-
private string GetOrdersByLastUpdate(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
213+
private async Task<string> GetOrdersByLastUpdateAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
201214
{
202-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_GENERAL, fromDate, toDate);
215+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_LAST_UPDATE_GENERAL, fromDate, toDate);
203216
}
204217

205-
public List<OrdersRow> GetOrdersByOrderDate(int days)
218+
public List<OrdersRow> GetOrdersByOrderDate(int days) =>
219+
GetOrdersByOrderDateAsync(days).GetAwaiter().GetResult();
220+
public async Task<List<OrdersRow>> GetOrdersByOrderDateAsync(int days)
206221
{
207222
DateTime fromDate = DateTime.UtcNow.AddDays(-1 * days);
208223
DateTime toDate = DateTime.UtcNow;
209-
return GetOrdersByOrderDate(fromDate,toDate);
224+
return await GetOrdersByOrderDateAsync(fromDate, toDate);
210225
}
211-
public List<OrdersRow> GetOrdersByOrderDate(DateTime fromDate, DateTime toDate)
226+
public async Task<List<OrdersRow>> GetOrdersByOrderDateAsync(DateTime fromDate, DateTime toDate)
212227
{
213228
List<OrdersRow> list = new List<OrdersRow>();
214-
var dateList=ReportDateRange.GetDateRange(fromDate, toDate,DAY_30);
229+
var dateList = ReportDateRange.GetDateRange(fromDate, toDate, DAY_30);
215230
foreach (var range in dateList)
216231
{
217-
var path = GetOrdersByOrderDate(_amazonConnection, range.StartDate, range.EndDate);
232+
var path = await GetOrdersByOrderDateAsync(_amazonConnection, range.StartDate, range.EndDate);
218233
OrdersReport report = new OrdersReport(path, _amazonConnection.RefNumber);
219234
list.AddRange(report.Data);
220235
}
221236
return list;
222237
}
223-
private string GetOrdersByOrderDate(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
238+
private async Task<string> GetOrdersByOrderDateAsync(AmazonConnection amazonConnection, DateTime fromDate, DateTime toDate)
224239
{
225-
return amazonConnection.Reports.CreateReportAndDownloadFile(ReportTypes.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL, fromDate, toDate);
240+
return await amazonConnection.Reports.CreateReportAndDownloadFileAsync(ReportTypes.GET_FLAT_FILE_ALL_ORDERS_DATA_BY_ORDER_DATE_GENERAL, fromDate, toDate);
226241
}
227242
#endregion
228243

0 commit comments

Comments
 (0)