From 0926699563a810468a45283837abfa777a58ed78 Mon Sep 17 00:00:00 2001 From: wufan Date: Thu, 24 Oct 2024 11:16:41 +0800 Subject: [PATCH] =?UTF-8?q?:fire:=20AMAZON=E6=B5=8B=E8=AF=95=E6=A1=88?= =?UTF-8?q?=E4=BE=8B=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SampleShippingV2Tests.cs | 38 +++++++++---------- .../SampleTestsBase.cs | 3 +- .../AmazonCredential.cs | 2 + .../Misc/LinqHelper.cs | 34 +++++++++++++++++ .../Services/RequestService.cs | 8 ++++ 5 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 Amazon.SellingPartnerApiSDK/Misc/LinqHelper.cs diff --git a/Amazon.SellingPartnerApi.Tests/SampleShippingV2Tests.cs b/Amazon.SellingPartnerApi.Tests/SampleShippingV2Tests.cs index d6aa52b..40b93eb 100644 --- a/Amazon.SellingPartnerApi.Tests/SampleShippingV2Tests.cs +++ b/Amazon.SellingPartnerApi.Tests/SampleShippingV2Tests.cs @@ -23,20 +23,20 @@ public class SampleShippingV2Tests : SampleTestsBase { ShipTo = new Address { - Name = "Maria Mata", - AddressLine1 = "2760 W Yowlumne Ave, Ste A", - StateOrRegion = "CA", - City = "Porterville", + Name = "Courtesy Chakma", + AddressLine1 = "14420 97TH AVE BSMT DOOR JAMAICA", + StateOrRegion = "NY", + City = "JAMAICA", CountryCode = "US", - PostalCode = "93257", + PostalCode = "11435-4424", }, - ShipDate = new DateTime(2024,10,30), + ShipDate = new DateTime(2024,10,25,15,30,0), ShipFrom = new Address { - Name = "EDDIE", + Name = "bosonshop", AddressLine1 = "11190 White Birch Dr Suite 100", StateOrRegion = "CA", - City = "Rancho Cucamonga", + City = "RanchoCucamonga", CountryCode = "US", PostalCode = "91730", }, @@ -61,28 +61,28 @@ public class SampleShippingV2Tests : SampleTestsBase Value = 200, Unit = "USD" }, - PackageClientReferenceId = "111-0844329-0173888", + PackageClientReferenceId = "111-0844329-0DAWDAV", IsHazmat = false, Items = new ItemList { new Item { - // ItemValue = new Currency - // { - // Value = 200, - // Unit = "USD" - // }, + ItemValue = new Currency + { + Value = 200, + Unit = "USD" + }, Quantity = 1, Weight = new Weight { Unit = WeightUnitEnum.POUND, Value = 20 }, - // LiquidVolume = new LiquidVolume - // { - // Unit = LiquidVolume.UnitEnum.ML, - // Value = 2000 - // }, + LiquidVolume = new LiquidVolume + { + Unit = LiquidVolumeUnitEnum.ML, + Value = 2000 + }, } } } diff --git a/Amazon.SellingPartnerApi.Tests/SampleTestsBase.cs b/Amazon.SellingPartnerApi.Tests/SampleTestsBase.cs index 31fc742..2021613 100644 --- a/Amazon.SellingPartnerApi.Tests/SampleTestsBase.cs +++ b/Amazon.SellingPartnerApi.Tests/SampleTestsBase.cs @@ -22,7 +22,8 @@ public class SampleTestsBase MarketPlace.US.Id ) { - IsDebugMode = true + IsDebugMode = true, + ShippingBusiness = Constants.ShippingBusiness.AmazonShipping_US }; return credential; diff --git a/Amazon.SellingPartnerApiSDK/AmazonCredential.cs b/Amazon.SellingPartnerApiSDK/AmazonCredential.cs index 4672dd8..2671d0a 100644 --- a/Amazon.SellingPartnerApiSDK/AmazonCredential.cs +++ b/Amazon.SellingPartnerApiSDK/AmazonCredential.cs @@ -79,6 +79,8 @@ namespace Amazon.SellingPartnerApiSDK /// 最大重试次数 /// public int MaxThrottledRetryCount { get; set; } = 3; + + public ShippingBusiness? ShippingBusiness { get; set; } /// /// 是否启用调试模式 diff --git a/Amazon.SellingPartnerApiSDK/Misc/LinqHelper.cs b/Amazon.SellingPartnerApiSDK/Misc/LinqHelper.cs new file mode 100644 index 0000000..30424f4 --- /dev/null +++ b/Amazon.SellingPartnerApiSDK/Misc/LinqHelper.cs @@ -0,0 +1,34 @@ +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization; +using System.Xml.Serialization; + +namespace Amazon.SellingPartnerApiSDK.Misc +{ + public static class LinqHelper + { + public static string GetEnumMemberValue(this T value) where T : Enum + { + return typeof(T) + .GetTypeInfo() + .DeclaredMembers + .SingleOrDefault(x => x.Name == value.ToString()) + ?.GetCustomAttribute(false) + ?.Value; + } + + + public static string SerializeObject(this T toSerialize) + { + XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType()); + + using (StringWriter textWriter = new StringWriter()) + { + xmlSerializer.Serialize(textWriter, toSerialize); + return textWriter.ToString(); + } + } + } +} \ No newline at end of file diff --git a/Amazon.SellingPartnerApiSDK/Services/RequestService.cs b/Amazon.SellingPartnerApiSDK/Services/RequestService.cs index 9be656c..d964fba 100644 --- a/Amazon.SellingPartnerApiSDK/Services/RequestService.cs +++ b/Amazon.SellingPartnerApiSDK/Services/RequestService.cs @@ -97,6 +97,7 @@ namespace Amazon.SellingPartnerApiSDK.Services { RestHeader(); AddAccessToken(); + AddShippingBusinessId(); var response = await RequestClient.ExecuteAsync(Request, cancellationToken); LogRequest(Request, response); SaveLastRequestHeader(response.Headers); @@ -283,6 +284,13 @@ namespace Amazon.SellingPartnerApiSDK.Services Request.AddOrUpdateHeader(AccessTokenHeaderName, AccessToken); } } + + private void AddShippingBusinessId() + { + if (AmazonCredential.ShippingBusiness.HasValue) + Request.AddOrUpdateHeader(ShippingBusinessIdHeaderName, + AmazonCredential.ShippingBusiness.Value.GetEnumMemberValue()); + } private async Task RefreshTokenAsync(CacheTokenData.TokenDataType tokenDataType = CacheTokenData.TokenDataType.Normal, CreateRestrictedDataTokenRequest requestPii = null, CancellationToken cancellationToken = default)