Skip to content

Commit

Permalink
Add AmazonShippingRateProvider for fetching rates
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/AmazonConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

var rateRequest = new GetRatesRequest()
{
ShipDate = DateTimeOffset.Now.AddDays(2).ToString("yyyy-MM-dd'T'HH:mm:ss'Z'"),
ShipTo = new Address()
{
Name = "test moffett",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3929,7 +3929,7 @@ public partial class GetRatesRequest
/// The ship date and time (the requested pickup). This defaults to the current date and time.
/// </summary>
[Newtonsoft.Json.JsonProperty("shipDate", Required = Newtonsoft.Json.Required.AllowNull, DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset ShipDate { get; set; }
public string ShipDate { get; set; }

[Newtonsoft.Json.JsonProperty("shipperInstruction", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public ShipperInstruction ShipperInstruction { get; set; }
Expand Down
113 changes: 113 additions & 0 deletions src/EasyKeys.Shipping.Amazon.Rates/AmazonShippingRateProvider.cs
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;
}
}
7 changes: 0 additions & 7 deletions src/EasyKeys.Shipping.Amazon.Rates/Class1.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<ProjectReference Include="..\EasyKeys.Shipping.Amazon.Abstractions\EasyKeys.Shipping.Amazon.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="DependencyInjection\" />
</ItemGroup>

</Project>
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);
}

0 comments on commit b4f766a

Please sign in to comment.