-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OneClickShipmentAsync and enhance rate provider
Updated `Program.cs` to implement shipment creation using `OneClickShipmentAsync`, including detailed shipment information. Changed `ShipDate` type in `AmazonShippingApi.cs` from `DateTimeOffset` to `string`. Modified `GetRatesAsync` in `AmazonShippingRateProvider.cs` to construct a `GetRatesRequest` with new shipment details and improved error handling. Cleaned up `EasyKeys.Shipping.Amazon.Rates.csproj` by removing unnecessary folder references. Added `AddRestApiAmazonRateProvider` method in `AmazonRatesServiceCollectionExtensions.cs` to register services for the Amazon shipping rate provider.
- Loading branch information
Brandon Moffett
committed
Dec 16, 2024
1 parent
b4f766a
commit 59779fa
Showing
5 changed files
with
206 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
| ||
using System.Collections.ObjectModel; | ||
using System.Text.Json; | ||
|
||
using EasyKeys.Shipping.Amazon.Abstractions.OpenApis.V2.Shipping; | ||
|
@@ -102,6 +102,97 @@ | |
|
||
var shippingApi = new AmazonShippingApi(new HttpClient()); | ||
var result = await shippingApi.GetRatesAsync(accessToken.access_token, XAmznShippingBusinessId.AmazonShipping_US, rateRequest); | ||
var shipment = await shippingApi.OneClickShipmentAsync | ||
( | ||
accessToken.access_token, | ||
XAmznShippingBusinessId3.AmazonShipping_US, | ||
new () | ||
{ | ||
ShipDate = DateTimeOffset.Now.AddDays(2).ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"), | ||
ShipTo = new Address() | ||
{ | ||
Name = "test moffett", | ||
AddressLine1 = "22322 Roxford St", | ||
StateOrRegion = "Michigan", | ||
City = "Detroit", | ||
CountryCode = "United States", | ||
PostalCode = "48219-2381", | ||
Email = "[email protected]" | ||
}, | ||
ShipFrom = new Address() | ||
{ | ||
Name = "test moffett", | ||
AddressLine1 = "662 Broadway 3fl", | ||
StateOrRegion = "New York", | ||
City = "Brooklyn", | ||
CountryCode = "United States", | ||
PostalCode = "11206-4410", | ||
Email = "[email protected]" | ||
}, | ||
Packages = new() | ||
{ | ||
new() | ||
{ | ||
Dimensions = new() | ||
{ | ||
Unit = DimensionsUnit.INCH, | ||
Length = 1, | ||
Width = 1, | ||
Height = 1 | ||
}, | ||
Weight = new() | ||
{ | ||
Unit = WeightUnit.OUNCE, | ||
Value = 1 | ||
}, | ||
InsuredValue = new() | ||
{ | ||
Value = 1, | ||
Unit = "USD" | ||
}, | ||
PackageClientReferenceId = "packageClientReferenceId", | ||
Items = new() | ||
{ | ||
new() | ||
{ | ||
Weight = new() | ||
{ | ||
Unit = WeightUnit.GRAM | ||
}, | ||
LiquidVolume = new() | ||
{ | ||
Unit = LiquidVolumeUnit.ML | ||
}, | ||
Description = "asdf", | ||
Quantity = 1 | ||
} | ||
} | ||
} | ||
}, | ||
ChannelDetails = new() | ||
{ | ||
ChannelType = ChannelType.EXTERNAL | ||
}, | ||
ServiceSelection = new() | ||
{ | ||
ServiceId = new() { "std-us-swa-mfn" }, | ||
}, | ||
LabelSpecifications = new() | ||
{ | ||
Format = DocumentFormat.PNG, | ||
Size = new() | ||
{ | ||
Length = 60, | ||
Width = 4, | ||
Unit = DocumentSizeUnit.INCH | ||
}, | ||
Dpi = 300, | ||
PageLayout = "DEFAULT", | ||
NeedFileJoining = false, | ||
RequestedDocumentTypes = new Collection<DocumentType> { DocumentType.LABEL } | ||
} | ||
} | ||
); | ||
} | ||
catch (ApiException<ErrorList> ex) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,93 +19,111 @@ public AmazonShippingRateProvider( | |
|
||
public async Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken cancellationToken = default) | ||
{ | ||
var rateRequest = new GetRatesRequest() | ||
try | ||
{ | ||
ShipDate = shipment.Options.ShippingDate.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"), | ||
ShipTo = new Abstractions.OpenApis.V2.Shipping.Address() | ||
var rateRequest = new GetRatesRequest() | ||
{ | ||
Name = "unknown name", | ||
AddressLine1 = shipment.DestinationAddress.StreetLine, | ||
AddressLine2 = shipment.DestinationAddress.StreetLine2, | ||
StateOrRegion = shipment.DestinationAddress.StateOrProvince, | ||
City = shipment.DestinationAddress.City, | ||
CountryCode = shipment.DestinationAddress.CountryCode, | ||
PostalCode = shipment.DestinationAddress.PostalCode, | ||
Email = "unknown name", | ||
PhoneNumber = "unknown phone number" | ||
}, | ||
ShipFrom = new Abstractions.OpenApis.V2.Shipping.Address() | ||
{ | ||
AddressLine1 = shipment.OriginAddress.StreetLine, | ||
AddressLine2 = shipment.OriginAddress.StreetLine2, | ||
StateOrRegion = shipment.OriginAddress.StateOrProvince, | ||
City = shipment.OriginAddress.City, | ||
CountryCode = shipment.OriginAddress.CountryCode, | ||
PostalCode = shipment.OriginAddress.PostalCode, | ||
Email = "[email protected]", | ||
CompanyName = "EasyKeys", | ||
PhoneNumber = "unknown phone number" | ||
}, | ||
Packages = new () | ||
{ | ||
new () | ||
{ | ||
Dimensions = new () | ||
ShipDate = shipment.Options.ShippingDate.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"), | ||
ShipTo = new Abstractions.OpenApis.V2.Shipping.Address() | ||
{ | ||
Unit = DimensionsUnit.INCH, | ||
Length = 1, | ||
Width = 1, | ||
Height = 1 | ||
Name = "unknown name", | ||
AddressLine1 = shipment.DestinationAddress.StreetLine, | ||
AddressLine2 = shipment.DestinationAddress.StreetLine2, | ||
StateOrRegion = shipment.DestinationAddress.StateOrProvince, | ||
City = shipment.DestinationAddress.City, | ||
CountryCode = shipment.DestinationAddress.CountryCode, | ||
PostalCode = shipment.DestinationAddress.PostalCode, | ||
Email = "unknown name", | ||
PhoneNumber = "unknown phone number" | ||
}, | ||
Weight = new () | ||
ShipFrom = new Abstractions.OpenApis.V2.Shipping.Address() | ||
{ | ||
Unit = WeightUnit.POUND, | ||
Value = (double)shipment.Packages.Sum(x => x.RoundedWeight) | ||
AddressLine1 = shipment.OriginAddress.StreetLine, | ||
AddressLine2 = shipment.OriginAddress.StreetLine2, | ||
StateOrRegion = shipment.OriginAddress.StateOrProvince, | ||
City = shipment.OriginAddress.City, | ||
CountryCode = shipment.OriginAddress.CountryCode, | ||
PostalCode = shipment.OriginAddress.PostalCode, | ||
Email = "[email protected]", | ||
CompanyName = "EasyKeys", | ||
PhoneNumber = "unknown phone number" | ||
}, | ||
InsuredValue = new () | ||
{ | ||
Value = (double)shipment.Packages.Sum(x => x.InsuredValue), | ||
Unit = "USD" | ||
}, | ||
PackageClientReferenceId = "packageClientReferenceId", | ||
Items = new () | ||
Packages = new () | ||
{ | ||
new () | ||
{ | ||
Dimensions = new () | ||
{ | ||
Unit = DimensionsUnit.INCH, | ||
Length = 1, | ||
Width = 1, | ||
Height = 1 | ||
}, | ||
Weight = new () | ||
{ | ||
Unit = WeightUnit.POUND | ||
Unit = WeightUnit.POUND, | ||
Value = (double)shipment.Packages.Sum(x => x.RoundedWeight) | ||
}, | ||
LiquidVolume = new () | ||
InsuredValue = new () | ||
{ | ||
Unit = LiquidVolumeUnit.ML | ||
Value = (double)shipment.Packages.Sum(x => x.InsuredValue), | ||
Unit = "USD" | ||
}, | ||
Description = "asdf", | ||
Quantity = 1 | ||
PackageClientReferenceId = "packageClientReferenceId", | ||
Items = new () | ||
{ | ||
new () | ||
{ | ||
Weight = new () | ||
{ | ||
Unit = WeightUnit.POUND | ||
}, | ||
LiquidVolume = new () | ||
{ | ||
Unit = LiquidVolumeUnit.ML | ||
}, | ||
Description = "asdf", | ||
Quantity = 1 | ||
} | ||
} | ||
} | ||
}, | ||
ChannelDetails = new () | ||
{ | ||
ChannelType = ChannelType.EXTERNAL | ||
} | ||
} | ||
}, | ||
ChannelDetails = new () | ||
{ | ||
ChannelType = ChannelType.EXTERNAL | ||
} | ||
}; | ||
}; | ||
|
||
var token = await _authenticatorService.GetTokenAsync(cancellationToken); | ||
var token = await _authenticatorService.GetTokenAsync(cancellationToken); | ||
|
||
var result = await _shippingApi.GetRatesAsync(token, XAmznShippingBusinessId.AmazonShipping_US, rateRequest); | ||
var result = await _shippingApi.GetRatesAsync(token, XAmznShippingBusinessId.AmazonShipping_US, rateRequest); | ||
|
||
foreach (var amazonRate in result.Payload.Rates) | ||
foreach (var amazonRate in result.Payload.Rates) | ||
{ | ||
var rate = new Shipping.Abstractions.Models.Rate( | ||
amazonRate.ServiceName, | ||
amazonRate.ServiceName, | ||
amazonRate.CarrierName, | ||
(decimal)amazonRate.TotalCharge.Value, | ||
(decimal)amazonRate.TotalCharge.Value, | ||
amazonRate.Promise.DeliveryWindow.End.UtcDateTime); | ||
shipment.Rates.Add(rate); | ||
} | ||
} | ||
catch (ApiException<ErrorList> ex) | ||
{ | ||
foreach (var error in ex.Result.Errors) | ||
{ | ||
shipment.InternalErrors.Add(error.Message); | ||
} | ||
} | ||
catch (ApiException ex) | ||
{ | ||
shipment.InternalErrors.Add(ex.Message); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var rate = new Shipping.Abstractions.Models.Rate( | ||
amazonRate.ServiceName, | ||
amazonRate.ServiceName, | ||
amazonRate.CarrierName, | ||
(decimal)amazonRate.TotalCharge.Value, | ||
(decimal)amazonRate.TotalCharge.Value, | ||
amazonRate.Promise.DeliveryWindow.End.UtcDateTime); | ||
shipment.Rates.Add(rate); | ||
shipment.InternalErrors.Add(ex.Message); | ||
} | ||
|
||
return shipment; | ||
|
28 changes: 28 additions & 0 deletions
28
...yKeys.Shipping.Amazon.Rates/DependencyInjection/AmazonRatesServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using EasyKeys.Shipping.Amazon.Abstractions.Options; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace EasyKeys.Shipping.Amazon.Rates.DependencyInjection; | ||
|
||
public static class AmazonRatesServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddRestApiAmazonRateProvider( | ||
this IServiceCollection services, | ||
string sectionName = nameof(AmazonShippingApiOptions), | ||
Action<AmazonShippingApiOptions, IServiceProvider>? configOptions = null) | ||
{ | ||
services.AddLogging(); | ||
|
||
services.AddAmazonShippingClient(); | ||
|
||
services.AddTransient<IAmazonShippingRateProvider, AmazonShippingRateProvider>(); | ||
|
||
return services; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters