Skip to content

Commit a5d3b80

Browse files
committed
add authorization
1 parent b0f2dd7 commit a5d3b80

File tree

10 files changed

+92
-38
lines changed

10 files changed

+92
-38
lines changed

Source/FikaAmazonAPI.SampleCode/Program.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static async Task Main(string[] args)
1919
.AddUserSecrets<Program>()
2020
.Build();
2121

22+
2223
AmazonConnection amazonConnection = new AmazonConnection(new AmazonCredential()
2324
{
2425
AccessKey = config.GetSection("FikaAmazonAPI:AccessKey").Value,
@@ -31,14 +32,25 @@ static async Task Main(string[] args)
3132
IsActiveLimitRate = true
3233
});
3334

35+
var sss = amazonConnection.Authorization.GetAuthorizationCode(new Parameter.Authorization.ParameterAuthorizationCode()
36+
{
37+
developerId = "673740111638",
38+
mwsAuthToken = "amzn.mws.f0b83c90-ac85-07fc-f35b-9b9021fcbcf3",
39+
sellingPartnerId = "A3J37AJU4O9RHK"
40+
});
41+
42+
ReportManager reportManager2 = new ReportManager(amazonConnection);
43+
var products2 = await reportManager2.GetProductsAsync(); //GET_MERCHANT_LISTINGS_ALL_DATA
44+
45+
3446
ParameterOrderList serachOrderList = new ParameterOrderList();
3547
serachOrderList.CreatedAfter = DateTime.UtcNow.AddDays(-100);
3648

3749
serachOrderList.OrderStatuses = new List<OrderStatuses>();
3850
serachOrderList.OrderStatuses.Add(OrderStatuses.Shipped);
3951

4052

41-
var orders = amazonConnection.Orders.GetOrders(serachOrderList);
53+
var orders = await amazonConnection.Orders.GetOrdersAsync(serachOrderList);
4254

4355

4456
//var parameterOrderList = new ParameterOrderList

Source/FikaAmazonAPI/AmazonConnection.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using FikaAmazonAPI.Services;
2-
using FikaAmazonAPI.Utils;
32
using System;
4-
using System.Collections.Generic;
53

64
namespace FikaAmazonAPI
75
{
@@ -40,7 +38,7 @@ public class AmazonConnection
4038
public VendorDirectFulfillmentOrderService VendorDirectFulfillmentOrders => this._VendorDirectFulfillmentOrders ?? throw _NoCredentials;
4139

4240

43-
41+
4442
private OrderService _Orders { get; set; }
4543
private ReportService _Reports { get; set; }
4644
private SolicitationService _Solicitations { get; set; }
@@ -75,7 +73,7 @@ public class AmazonConnection
7573
private UnauthorizedAccessException _NoCredentials = new UnauthorizedAccessException($"Error, you cannot make calls to Amazon without credentials!");
7674

7775
public string RefNumber { get; set; }
78-
public AmazonConnection(AmazonCredential Credentials,string RefNumber=null)
76+
public AmazonConnection(AmazonCredential Credentials, string RefNumber = null)
7977
{
8078
this.Authenticate(Credentials);
8179
this.RefNumber = RefNumber;
@@ -93,6 +91,7 @@ private void Init(AmazonCredential Credentials)
9391
{
9492
this.Credentials = Credentials;
9593

94+
this._Authorization = new AuthorizationService(this.Credentials);
9695
this._Orders = new OrderService(this.Credentials);
9796
this._Reports = new ReportService(this.Credentials);
9897
this._Solicitations = new SolicitationService(this.Credentials);
@@ -109,18 +108,18 @@ private void Init(AmazonCredential Credentials)
109108
this._Feed = new FeedService(this.Credentials);
110109
this._ListingsItem = new ListingsItemService(this.Credentials);
111110
this._MerchantFulfillment = new MerchantFulfillmentService(this.Credentials);
112-
this._Messaging= new MessagingService(this.Credentials);
113-
this._Notification= new NotificationService(this.Credentials);
114-
this._ProductFee= new ProductFeeService(this.Credentials);
115-
this._Sales= new SalesService(this.Credentials);
116-
this._Seller= new SellerService(this.Credentials);
117-
this._Services= new ServicesService(this.Credentials);
118-
this._ShipmentInvoicing= new ShipmentInvoicingService(this.Credentials);
119-
this._Shipping= new ShippingService(this.Credentials);
120-
this._Upload= new UploadService(this.Credentials);
121-
this._Tokens= new TokenService(this.Credentials);
122-
this._FulFillmentInbound= new FulFillmentInboundService(this.Credentials);
123-
this._FulFillmentOutbound= new FulFillmentOutboundService(this.Credentials);
111+
this._Messaging = new MessagingService(this.Credentials);
112+
this._Notification = new NotificationService(this.Credentials);
113+
this._ProductFee = new ProductFeeService(this.Credentials);
114+
this._Sales = new SalesService(this.Credentials);
115+
this._Seller = new SellerService(this.Credentials);
116+
this._Services = new ServicesService(this.Credentials);
117+
this._ShipmentInvoicing = new ShipmentInvoicingService(this.Credentials);
118+
this._Shipping = new ShippingService(this.Credentials);
119+
this._Upload = new UploadService(this.Credentials);
120+
this._Tokens = new TokenService(this.Credentials);
121+
this._FulFillmentInbound = new FulFillmentInboundService(this.Credentials);
122+
this._FulFillmentOutbound = new FulFillmentOutboundService(this.Credentials);
124123
this._VendorDirectFulfillmentOrders = new VendorDirectFulfillmentOrderService(this.Credentials);
125124
}
126125
}

Source/FikaAmazonAPI/AmazonSpApiSDK/Models/Authorization/GetAuthorizationCodeResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public partial class GetAuthorizationCodeResponse : IEquatable<GetAuthorizationC
2828
/// </summary>
2929
/// <param name="Payload">Payload.</param>
3030
/// <param name="Errors">Errors.</param>
31+
public GetAuthorizationCodeResponse()
32+
{
33+
this.Payload = default(AuthorizationCode);
34+
this.Errors = default(ErrorList);
35+
}
3136
public GetAuthorizationCodeResponse(AuthorizationCode Payload = default(AuthorizationCode), ErrorList Errors = default(ErrorList))
3237
{
3338
this.Payload = Payload;

Source/FikaAmazonAPI/FikaAmazonAPI.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@
4444
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
4545
<PackageReference Include="System.Reflection" Version="4.3.0" />
4646
</ItemGroup>
47-
48-
<ItemGroup>
49-
<Folder Include="Parameter\Authorization\" />
50-
</ItemGroup>
5147
<ItemGroup>
5248
<None Include="..\..\Others\icon\icon.jpg">
5349
<Pack>True</Pack>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using FikaAmazonAPI.Search;
2+
3+
namespace FikaAmazonAPI.Parameter.Authorization
4+
{
5+
public class ParameterAuthorizationCode : ParameterBased
6+
{
7+
public string sellingPartnerId { get; set; }
8+
public string developerId { get; set; }
9+
public string mwsAuthToken { get; set; }
10+
11+
}
12+
}

Source/FikaAmazonAPI/Services/ApiUrls.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Net;
32

43
namespace FikaAmazonAPI.AmazonSpApiSDK.Services
54
{
@@ -232,6 +231,14 @@ public static string GetOrderMetrics
232231
get => $"{_resourceBaseUrl}/orderMetrics";
233232
}
234233
}
234+
protected class AuthorizationsApiUrls
235+
{
236+
private readonly static string _resourceBaseUrl = "/authorization/v1";
237+
public static string GetAuthorizationCode
238+
{
239+
get => $"{_resourceBaseUrl}/authorizationCode";
240+
}
241+
}
235242
protected class SellersApiUrls
236243
{
237244
private readonly static string _resourceBaseUrl = "/sellers/v1";
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
namespace FikaAmazonAPI.Services
1+
using FikaAmazonAPI.AmazonSpApiSDK.Models.Authorization;
2+
using FikaAmazonAPI.Parameter.Authorization;
3+
using System.Threading.Tasks;
4+
5+
namespace FikaAmazonAPI.Services
26
{
37
public class AuthorizationService : RequestService
48
{
59
public AuthorizationService(AmazonCredential amazonCredential) : base(amazonCredential)
610
{
11+
}
12+
public string GetAuthorizationCode(ParameterAuthorizationCode parameterGetOrderMetrics) =>
13+
Task.Run(() => GetAuthorizationCodeAsync(parameterGetOrderMetrics)).ConfigureAwait(false).GetAwaiter().GetResult();
714

15+
public async Task<string> GetAuthorizationCodeAsync(ParameterAuthorizationCode parameter)
16+
{
17+
var param = parameter.getParameters();
18+
await CreateAuthorizedRequestAsync(AuthorizationsApiUrls.GetAuthorizationCode, RestSharp.Method.GET, param);
19+
var response = await ExecuteRequestAsync<GetAuthorizationCodeResponse>();
20+
if (response != null && response.Payload != null)
21+
return response.Payload._AuthorizationCode;
22+
return null;
823
}
924
}
1025
}

Source/FikaAmazonAPI/Services/ReportService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public async Task<string> CreateReportAndDownloadFileAsync(ReportTypes reportTyp
237237
if (dataEndTime.HasValue)
238238
parameters.dataEndTime = dataEndTime;
239239

240-
var reportId = CreateReport(parameters);
240+
var reportId = await CreateReportAsync(parameters);
241241
var filePath = string.Empty;
242242
string ReportDocumentId = string.Empty;
243243

@@ -246,7 +246,7 @@ public async Task<string> CreateReportAndDownloadFileAsync(ReportTypes reportTyp
246246
var reportData = await GetReportAsync(reportId);
247247
if (!string.IsNullOrEmpty(reportData.ReportDocumentId))
248248
{
249-
filePath = GetReportFile(reportData.ReportDocumentId);
249+
filePath = await GetReportFileAsync(reportData.ReportDocumentId);
250250
break;
251251
}
252252
if (reportData.ProcessingStatus == Report.ProcessingStatusEnum.FATAL)

Source/FikaAmazonAPI/Services/RequestService.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ private void CreateRequest(string url, RestSharp.Method method)
5555
RequestClient = new RestClient(ApiBaseUrl);
5656
Request = new RestRequest(url, method);
5757
}
58+
protected async Task CreateUnAuthorizedRequestAsync(string url, RestSharp.Method method, List<KeyValuePair<string, string>> queryParameters = null, object postJsonObj = null)
59+
{
60+
CreateRequest(url, method);
61+
if (postJsonObj != null)
62+
AddJsonBody(postJsonObj);
63+
if (queryParameters != null)
64+
AddQueryParameters(queryParameters);
65+
}
5866

5967
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)
6068
{
@@ -185,16 +193,17 @@ private void SleepForRateLimit(IList<RestSharp.Parameter> headers, RateLimitType
185193
}
186194
}
187195

188-
/// <summary>
189-
/// Executes the request
190-
/// </summary>
191-
/// <typeparam name="T">Type to parse response to</typeparam>
192-
/// <returns>Returns raw response</returns>
193-
protected IRestResponse ExecuteRequest()
196+
197+
protected async Task<T> ExecuteUnAuthorizedRequest<T>() where T : new()
194198
{
195-
var response = RequestClient.Execute(Request);
199+
var response = await RequestClient.ExecuteAsync<T>(Request);
196200
ParseResponse(response);
197-
return response;
201+
202+
if (response.StatusCode == HttpStatusCode.OK && !string.IsNullOrEmpty(response.Content) && response.Data == null)
203+
{
204+
response.Data = JsonConvert.DeserializeObject<T>(response.Content);
205+
}
206+
return response.Data;
198207
}
199208

200209
protected void ParseResponse(IRestResponse response)

Source/FikaAmazonAPI/Services/SalesService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using FikaAmazonAPI.AmazonSpApiSDK.Models.Sales;
22
using FikaAmazonAPI.Parameter.Sales;
3-
using FikaAmazonAPI.Utils;
43
using System.Collections.Generic;
54
using System.Threading.Tasks;
65

@@ -14,13 +13,13 @@ public SalesService(AmazonCredential amazonCredential) : base(amazonCredential)
1413
}
1514

1615

17-
public List<OrderMetricsInterval> GetOrderMetricsAsync(ParameterGetOrderMetrics parameterGetOrderMetrics) =>
18-
Task.Run(() => GetOrderMetrics(parameterGetOrderMetrics)).ConfigureAwait(false).GetAwaiter().GetResult();
19-
public async Task<List<OrderMetricsInterval>> GetOrderMetrics(ParameterGetOrderMetrics parameterGetOrderMetrics)
16+
public List<OrderMetricsInterval> GetOrderMetrics(ParameterGetOrderMetrics parameterGetOrderMetrics) =>
17+
Task.Run(() => GetOrderMetricsAsync(parameterGetOrderMetrics)).ConfigureAwait(false).GetAwaiter().GetResult();
18+
public async Task<List<OrderMetricsInterval>> GetOrderMetricsAsync(ParameterGetOrderMetrics parameterGetOrderMetrics)
2019
{
2120
var param = parameterGetOrderMetrics.getParameters();
2221
await CreateAuthorizedRequestAsync(SalesApiUrls.GetOrderMetrics, RestSharp.Method.GET, param);
23-
var response = await ExecuteRequestAsync<GetOrderMetricsResponse>(RateLimitType.Sales_GetOrderMetrics);
22+
var response = await ExecuteUnAuthorizedRequest<GetOrderMetricsResponse>();
2423
if (response != null && response.Payload != null)
2524
return response.Payload;
2625
return null;

0 commit comments

Comments
 (0)