-
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 AmazonShippingRateProvider for fetching rates
Introduces the `AmazonShippingRateProvider` class implementing the `IAmazonShippingRateProvider` interface to retrieve shipping rates from the Amazon API. Updates the `ShipDate` property type in `AmazonShippingApi.cs` from `DateTimeOffset` to `string`. Adds a new folder for dependency injection in the project file. Removes the unused `Class1.cs` file. Includes additional using directives in relevant files for improved functionality.
- Loading branch information
Brandon Moffett
committed
Dec 14, 2024
1 parent
acd3024
commit b4f766a
Showing
6 changed files
with
127 additions
and
8 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
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
113 changes: 113 additions & 0 deletions
113
src/EasyKeys.Shipping.Amazon.Rates/AmazonShippingRateProvider.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,113 @@ | ||
using EasyKeys.Shipping.Abstractions.Models; | ||
using EasyKeys.Shipping.Amazon.Abstractions.OpenApis.V2.Shipping; | ||
using EasyKeys.Shipping.Amazon.Abstractions.Services; | ||
|
||
namespace EasyKeys.Shipping.Amazon.Rates; | ||
|
||
public class AmazonShippingRateProvider : IAmazonShippingRateProvider | ||
{ | ||
private readonly IAmazonApiAuthenticatorService _authenticatorService; | ||
private readonly AmazonShippingApi _shippingApi; | ||
|
||
public AmazonShippingRateProvider( | ||
IAmazonApiAuthenticatorService authenticatorService, | ||
AmazonShippingApi shippingApi) | ||
{ | ||
_authenticatorService = authenticatorService; | ||
_shippingApi = shippingApi; | ||
} | ||
|
||
public async Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken cancellationToken = default) | ||
{ | ||
var rateRequest = new GetRatesRequest() | ||
{ | ||
ShipDate = shipment.Options.ShippingDate.ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"), | ||
ShipTo = new Abstractions.OpenApis.V2.Shipping.Address() | ||
{ | ||
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 () | ||
{ | ||
Unit = DimensionsUnit.INCH, | ||
Length = 1, | ||
Width = 1, | ||
Height = 1 | ||
}, | ||
Weight = new () | ||
{ | ||
Unit = WeightUnit.POUND, | ||
Value = (double)shipment.Packages.Sum(x => x.RoundedWeight) | ||
}, | ||
InsuredValue = new () | ||
{ | ||
Value = (double)shipment.Packages.Sum(x => x.InsuredValue), | ||
Unit = "USD" | ||
}, | ||
PackageClientReferenceId = "packageClientReferenceId", | ||
Items = new () | ||
{ | ||
new () | ||
{ | ||
Weight = new () | ||
{ | ||
Unit = WeightUnit.POUND | ||
}, | ||
LiquidVolume = new () | ||
{ | ||
Unit = LiquidVolumeUnit.ML | ||
}, | ||
Description = "asdf", | ||
Quantity = 1 | ||
} | ||
} | ||
} | ||
}, | ||
ChannelDetails = new () | ||
{ | ||
ChannelType = ChannelType.EXTERNAL | ||
} | ||
}; | ||
|
||
var token = await _authenticatorService.GetTokenAsync(cancellationToken); | ||
|
||
var result = await _shippingApi.GetRatesAsync(token, XAmznShippingBusinessId.AmazonShipping_US, rateRequest); | ||
|
||
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); | ||
} | ||
|
||
return shipment; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
8 changes: 8 additions & 0 deletions
8
src/EasyKeys.Shipping.Amazon.Rates/IAmazonShippingRateProvider.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,8 @@ | ||
using EasyKeys.Shipping.Abstractions.Models; | ||
|
||
namespace EasyKeys.Shipping.Amazon.Rates; | ||
|
||
public interface IAmazonShippingRateProvider | ||
{ | ||
Task<Shipment> GetRatesAsync(Shipment shipment, CancellationToken cancellationToken = default); | ||
} |