From 803451ec28c2e6d74e9bf98f1a200f225ac2acc4 Mon Sep 17 00:00:00 2001 From: Tareq Abuzuhri Date: Wed, 23 Nov 2022 20:50:43 +0100 Subject: [PATCH] Improve get PII --- .../FikaAmazonAPI.SampleCode/OrdersSample.cs | 30 ++++++++-- Source/FikaAmazonAPI.SampleCode/Program.cs | 9 ++- Source/FikaAmazonAPI/FikaAmazonAPI.csproj | 6 +- .../Services/MerchantFulfillmentService.cs | 58 ++++++++++++++++++- Source/FikaAmazonAPI/Services/OrderService.cs | 50 +++++++++++++++- .../FikaAmazonAPI/Services/RequestService.cs | 4 +- 6 files changed, 141 insertions(+), 16 deletions(-) diff --git a/Source/FikaAmazonAPI.SampleCode/OrdersSample.cs b/Source/FikaAmazonAPI.SampleCode/OrdersSample.cs index 36f48d40..fc8f648d 100644 --- a/Source/FikaAmazonAPI.SampleCode/OrdersSample.cs +++ b/Source/FikaAmazonAPI.SampleCode/OrdersSample.cs @@ -56,16 +56,26 @@ public void GetOrders() /// /// You must have valid PII developer to be able to call this method /// - public void GetOrdersPII() + public void GetOrdersPIISimple() { ParameterOrderList serachOrderList = new ParameterOrderList(); serachOrderList.CreatedAfter = DateTime.UtcNow.AddMinutes(-600000); serachOrderList.OrderStatuses = new List(); - serachOrderList.OrderStatuses.Add(OrderStatuses.Pending); + serachOrderList.OrderStatuses.Add(OrderStatuses.Unshipped); - serachOrderList.AmazonOrderIds = new List(); - serachOrderList.AmazonOrderIds.Add("405-0426616-1636335"); + //You must have valid PII developer to be able to call this + serachOrderList.IsNeedRestrictedDataToken = true; + + var orders = amazonConnection.Orders.GetOrders(serachOrderList); + } + public void GetOrdersPIIAdvance() + { + ParameterOrderList serachOrderList = new ParameterOrderList(); + serachOrderList.CreatedAfter = DateTime.UtcNow.AddMinutes(-600000); + + serachOrderList.OrderStatuses = new List(); + serachOrderList.OrderStatuses.Add(OrderStatuses.Unshipped); //You must have valid PII developer to be able to call this var restrictedResource = new RestrictedResource(); @@ -100,6 +110,18 @@ public void GetOrderItems() { var Items = amazonConnection.Orders.GetOrderItems("402-0467973-4229120"); } + + /// + /// You must have valid PII developer to be able to call this method + /// + public void GetOrderItemsPIISimple() + { + ParameterBasedPII parameterBasedPII = new ParameterBasedPII(); + parameterBasedPII.IsNeedRestrictedDataToken = true; + + var order = amazonConnection.Orders.GetOrderItems("405-0426616-1636335", parameterBasedPII); + } + /// /// You must have valid PII developer to be able to call this method /// diff --git a/Source/FikaAmazonAPI.SampleCode/Program.cs b/Source/FikaAmazonAPI.SampleCode/Program.cs index 8373ebf9..481899fd 100644 --- a/Source/FikaAmazonAPI.SampleCode/Program.cs +++ b/Source/FikaAmazonAPI.SampleCode/Program.cs @@ -36,9 +36,13 @@ static async Task Main(string[] args) ClientSecret = config.GetSection("FikaAmazonAPI:ClientSecret").Value, RefreshToken = config.GetSection("FikaAmazonAPI:RefreshToken").Value, MarketPlace = MarketPlace.GetMarketPlaceByID(config.GetSection("FikaAmazonAPI:MarketPlaceID").Value), - IsDebugMode=true + IsDebugMode = true }); + OrdersSample ordersSample1 = new OrdersSample(amazonConnection); + ordersSample1.GetOrdersPIIAdvance(); + ordersSample1.GetOrdersPIISimple(); + var offfers = amazonConnection.ProductPricing.GetItemOffers(new Parameter.ProductPricing.ParameterGetItemOffers { @@ -51,8 +55,7 @@ static async Task Main(string[] args) var dddd = amazonConnection.Seller.GetMarketplaceParticipations(); FeedsSample feedsSample = new FeedsSample(amazonConnection); - OrdersSample ordersSample1 = new OrdersSample(amazonConnection); - ordersSample1.GetOrdersPII(); + ReportManager reportManageree = new ReportManager(amazonConnection); diff --git a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj index 9ff45934..4c7cb7ce 100644 --- a/Source/FikaAmazonAPI/FikaAmazonAPI.csproj +++ b/Source/FikaAmazonAPI/FikaAmazonAPI.csproj @@ -7,9 +7,9 @@ CSharp Amazon Sp API true 8.0 - 1.5.16 - 1.5.16 - 1.5.16 + 1.5.17 + 1.5.17 + 1.5.17 true https://github.com/abuzuhri/Amazon-SP-API-CSharp MIT diff --git a/Source/FikaAmazonAPI/Services/MerchantFulfillmentService.cs b/Source/FikaAmazonAPI/Services/MerchantFulfillmentService.cs index 9db5c928..c2bb0839 100644 --- a/Source/FikaAmazonAPI/Services/MerchantFulfillmentService.cs +++ b/Source/FikaAmazonAPI/Services/MerchantFulfillmentService.cs @@ -1,6 +1,8 @@ using FikaAmazonAPI.AmazonSpApiSDK.Models.MerchantFulfillment; +using FikaAmazonAPI.AmazonSpApiSDK.Models.Token; using FikaAmazonAPI.Search; using FikaAmazonAPI.Utils; +using System.Collections.Generic; using System.Threading.Tasks; namespace FikaAmazonAPI.Services @@ -43,6 +45,19 @@ public Shipment GetShipment(string shipmentId, IParameterBasedPII ParameterBased Task.Run(() => GetShipmentAsync(shipmentId, ParameterBasedPII = null)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task GetShipmentAsync(string shipmentId, IParameterBasedPII ParameterBasedPII = null) { + if (ParameterBasedPII != null && ParameterBasedPII.IsNeedRestrictedDataToken && ParameterBasedPII.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.GET.ToString(); + restrictedResource.path = MerchantFulfillmentApiUrls.GetShipment(shipmentId); + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + ParameterBasedPII.RestrictedDataTokenRequest = createRDT; + } + await CreateAuthorizedRequestAsync(MerchantFulfillmentApiUrls.GetShipment(shipmentId), RestSharp.Method.Get, parameter: ParameterBasedPII); var response = await ExecuteRequestAsync(RateLimitType.MerchantFulFillment_GetShipment); @@ -54,6 +69,19 @@ public Shipment CancelShipment(string shipmentId, IParameterBasedPII parameterBa Task.Run(() => CancelShipmentAsync(shipmentId, parameterBasedPII)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task CancelShipmentAsync(string shipmentId, IParameterBasedPII parameterBasedPII = null) { + if (parameterBasedPII != null && parameterBasedPII.IsNeedRestrictedDataToken && parameterBasedPII.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.DELETE.ToString(); + restrictedResource.path = MerchantFulfillmentApiUrls.GetShipment(shipmentId); + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + parameterBasedPII.RestrictedDataTokenRequest = createRDT; + } + await CreateAuthorizedRequestAsync(MerchantFulfillmentApiUrls.GetShipment(shipmentId), RestSharp.Method.Delete, parameter: parameterBasedPII); var response = await ExecuteRequestAsync(RateLimitType.MerchantFulFillment_CancelShipment); @@ -64,9 +92,22 @@ public async Task CancelShipmentAsync(string shipmentId, IParameterBas public Shipment CancelShipmentOld(string shipmentId, IParameterBasedPII parameterBasedPII = null) => Task.Run(() => CancelShipmentOldAsync(shipmentId, parameterBasedPII)).ConfigureAwait(false).GetAwaiter().GetResult(); - public async Task CancelShipmentOldAsync(string shipmentId, IParameterBasedPII ParameterBasedPII = null) + public async Task CancelShipmentOldAsync(string shipmentId, IParameterBasedPII parameterBasedPII = null) { - await CreateAuthorizedRequestAsync(MerchantFulfillmentApiUrls.CancelShipmentOld(shipmentId), RestSharp.Method.Put, parameter: ParameterBasedPII); + if (parameterBasedPII != null && parameterBasedPII.IsNeedRestrictedDataToken && parameterBasedPII.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.PUT.ToString(); + restrictedResource.path = MerchantFulfillmentApiUrls.CancelShipmentOld(shipmentId); + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + parameterBasedPII.RestrictedDataTokenRequest = createRDT; + } + + await CreateAuthorizedRequestAsync(MerchantFulfillmentApiUrls.CancelShipmentOld(shipmentId), RestSharp.Method.Put, parameter: parameterBasedPII); var response = await ExecuteRequestAsync(RateLimitType.MerchantFulFillment_CancelShipmentOld); if (response != null && response.Payload != null) @@ -78,6 +119,19 @@ public Shipment CreateShipment(CreateShipmentRequest createShipmentRequest, IPar Task.Run(() => CreateShipmentAsync(createShipmentRequest, parameterBasedPII)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task CreateShipmentAsync(CreateShipmentRequest createShipmentRequest, IParameterBasedPII parameterBasedPII = null) { + if (parameterBasedPII != null && parameterBasedPII.IsNeedRestrictedDataToken && parameterBasedPII.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.POST.ToString(); + restrictedResource.path = MerchantFulfillmentApiUrls.CreateShipment; + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + parameterBasedPII.RestrictedDataTokenRequest = createRDT; + } + await CreateAuthorizedRequestAsync(MerchantFulfillmentApiUrls.CreateShipment, RestSharp.Method.Post, postJsonObj: createShipmentRequest, parameter: parameterBasedPII); var response = await ExecuteRequestAsync(RateLimitType.MerchantFulFillment_CreateShipment); diff --git a/Source/FikaAmazonAPI/Services/OrderService.cs b/Source/FikaAmazonAPI/Services/OrderService.cs index 6f75c278..d954e2c6 100644 --- a/Source/FikaAmazonAPI/Services/OrderService.cs +++ b/Source/FikaAmazonAPI/Services/OrderService.cs @@ -1,4 +1,5 @@ using FikaAmazonAPI.AmazonSpApiSDK.Models.Orders; +using FikaAmazonAPI.AmazonSpApiSDK.Models.Token; using FikaAmazonAPI.Parameter.Order; using FikaAmazonAPI.Search; using System.Collections.Generic; @@ -19,6 +20,7 @@ public OrderList GetOrders(ParameterOrderList searchOrderList) => Task.Run(() => GetOrdersAsync(searchOrderList)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task GetOrdersAsync(ParameterOrderList searchOrderList) { + var orderList = new OrderList(); if (searchOrderList.MarketplaceIds == null || searchOrderList.MarketplaceIds.Count == 0) @@ -26,6 +28,22 @@ public async Task GetOrdersAsync(ParameterOrderList searchOrderList) searchOrderList.MarketplaceIds = new List(); searchOrderList.MarketplaceIds.Add(AmazonCredential.MarketPlace.ID); } + + if (searchOrderList.IsNeedRestrictedDataToken && searchOrderList.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.GET.ToString(); + restrictedResource.path = OrdersApiUrls.Orders; + restrictedResource.dataElements = new List { "buyerInfo", "shippingAddress" }; + + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + searchOrderList.RestrictedDataTokenRequest = createRDT; + } + var queryParameters = searchOrderList.getParameters(); await CreateAuthorizedRequestAsync(OrdersApiUrls.Orders, RestSharp.Method.Get, queryParameters, parameter: searchOrderList); @@ -76,19 +94,47 @@ public Order GetOrder(ParameterGetOrder parameter) => Task.Run(() => GetOrderAsync(parameter)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task GetOrderAsync(ParameterGetOrder parameter) { + if (parameter.IsNeedRestrictedDataToken && parameter.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.GET.ToString(); + restrictedResource.path = OrdersApiUrls.Order(parameter.OrderId); + restrictedResource.dataElements = new List { "buyerInfo", "shippingAddress" }; + + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + parameter.RestrictedDataTokenRequest = createRDT; + } + await CreateAuthorizedRequestAsync(OrdersApiUrls.Order(parameter.OrderId), RestSharp.Method.Get, parameter: parameter); var response = await ExecuteRequestAsync(Utils.RateLimitType.Order_GetOrder); if (response != null && response.Payload != null) return response.Payload; else return null; } - - public OrderItemList GetOrderItems(string orderId, IParameterBasedPII parameterBasedPII = null) => Task.Run(() => GetOrderItemsAsync(orderId, parameterBasedPII)).ConfigureAwait(false).GetAwaiter().GetResult(); public async Task GetOrderItemsAsync(string orderId, IParameterBasedPII ParameterBasedPII = null) { var orderItemList = new OrderItemList(); + + if (ParameterBasedPII != null && ParameterBasedPII.IsNeedRestrictedDataToken && ParameterBasedPII.RestrictedDataTokenRequest == null) + { + var restrictedResource = new RestrictedResource(); + restrictedResource.method = Method.GET.ToString(); + restrictedResource.path = OrdersApiUrls.OrderItems(orderId); + restrictedResource.dataElements = new List { "buyerInfo", "shippingAddress" }; + + var createRDT = new CreateRestrictedDataTokenRequest() + { + restrictedResources = new List { restrictedResource } + }; + ParameterBasedPII.RestrictedDataTokenRequest = createRDT; + } + await CreateAuthorizedRequestAsync(OrdersApiUrls.OrderItems(orderId), RestSharp.Method.Get, parameter: ParameterBasedPII); var response = await ExecuteRequestAsync(Utils.RateLimitType.Order_GetOrderItems); var nextToken = response.Payload.NextToken; diff --git a/Source/FikaAmazonAPI/Services/RequestService.cs b/Source/FikaAmazonAPI/Services/RequestService.cs index 6565b93a..e9d1392e 100644 --- a/Source/FikaAmazonAPI/Services/RequestService.cs +++ b/Source/FikaAmazonAPI/Services/RequestService.cs @@ -153,8 +153,8 @@ private void LogRequest(RestRequest request, RestResponse response) responseUri = response.ResponseUri, errorMessage = response.ErrorMessage, }; - - Console.WriteLine(string.Format("Request completed, Request: {0}, Response: {1}", + Console.WriteLine("\n\n"); + Console.WriteLine(string.Format("Request completed, \nRequest: {0} \n\nResponse: {1}", JsonConvert.SerializeObject(requestToLog), JsonConvert.SerializeObject(responseToLog))); }