Skip to content

Commit

Permalink
Add OneClickShipmentAsync and enhance rate provider
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 73 deletions.
93 changes: 92 additions & 1 deletion src/AmazonConsoleApp/Program.cs
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;
Expand Down Expand Up @@ -102,6 +102,97 @@

var shippingApi = new AmazonShippingApi(new HttpClient());
var result = await shippingApi.GetRatesAsync(accessToken.access_token, XAmznShippingBusinessId.AmazonShipping_US, rateRequest);

Check warning on line 104 in src/AmazonConsoleApp/Program.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4337,7 +4337,7 @@ public partial class OneClickShipmentRequest
/// 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.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset ShipDate { get; set; }
public System.String ShipDate { get; set; }

[Newtonsoft.Json.JsonProperty("packages", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
Expand Down
152 changes: 85 additions & 67 deletions src/EasyKeys.Shipping.Amazon.Rates/AmazonShippingRateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@
<ProjectReference Include="..\EasyKeys.Shipping.Amazon.Abstractions\EasyKeys.Shipping.Amazon.Abstractions.csproj" />
</ItemGroup>

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

</Project>

0 comments on commit 59779fa

Please sign in to comment.