Skip to content

Commit

Permalink
#80 ILogger for UPS provider, updated IShippingProvider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeybusygin authored Mar 2, 2025
1 parent a6d2652 commit 04b2488
Show file tree
Hide file tree
Showing 22 changed files with 442 additions and 338 deletions.
121 changes: 64 additions & 57 deletions SampleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
Expand Down Expand Up @@ -45,81 +46,87 @@ static async Task Main()
var origin = new Address("", "CT", "06405", "US");
var destination = new Address("", "", "20852", "US"); // US Address
//var origin = new Address("Amsterdam", "", "1043 AG", "NL"); // Netherlands Address
//var destination = new Address("London", "", "SW1A 2AA", "GB"); // Great Britain Address
//var destination = new Address("London", "", "SW1A 2AA", "GB"); // Great Britain Address
//var destination = new Address("", "", "88888", "US"); // Wrong US Address
//var destination = new Address("Domont", "", "95330", "FR"); // France Address
//var destination = new Address("", "", "00907", "PR"); // Puerto Rico Address
//var destination = new Address("", "", "L4W 1S2", "CA"); // Canada Address
//var destination = new Address("", "", "SW1E 5JL", "GB"); // UK Address
//var destination = new Address("", "", "1042 AG", "NL"); // Netherlands Address

// Create RateManager
var rateManager = new RateManager();

using (var httpClient = new HttpClient())
using (var loggerFactory = LoggerFactory.Create(builder =>
{
// Add desired providers
// UPS
var upsConfiguration = new UPSProviderConfiguration()
{
ClientId = upsClientId,
ClientSecret = upsClientSecret,
AccountNumber = upsAccountNumber,
UseProduction = false
};
rateManager.AddProvider(new UPSProvider(upsConfiguration, httpClient));

// FedEx
rateManager.AddProvider(new FedExProvider(fedexKey, fedexPassword, fedexAccountNumber, fedexMeterNumber, fedexUseProduction));
rateManager.AddProvider(new FedExSmartPostProvider(fedexKey, fedexPassword, fedexAccountNumber, fedexMeterNumber, fedexHubId, fedexUseProduction));

// USPS Domestic
rateManager.AddProvider(new USPSProvider(new USPSProviderConfiguration(uspsUserId), httpClient));
// USPS International
rateManager.AddProvider(new USPSInternationalProvider(new USPSProviderConfiguration(uspsUserId), httpClient));

// DHL
var dhlConfiguration = new DHLProviderConfiguration(dhlSiteId, dhlPassword, useProduction: false)
.ExcludeServices(new char[] { 'C' });
rateManager.AddProvider(new DHLProvider(dhlConfiguration, httpClient));

// Call GetRates()
var shipment = await rateManager.GetRatesAsync(origin, destination, packages,
new ShipmentOptions()
{
SaturdayDelivery = true
});
builder.AddConsole();
}))
{
// Create RateManager
var rateManager = new RateManager();

// Iterate through the rates returned
foreach (var rate in shipment.Rates)
using (var httpClient = new HttpClient())
{
Console.WriteLine(rate);
}
// Add desired providers
// UPS
var upsConfiguration = new UPSProviderConfiguration()
{
ClientId = upsClientId,
ClientSecret = upsClientSecret,
AccountNumber = upsAccountNumber,
UseProduction = false
};
var upsProviderLogger = loggerFactory.CreateLogger<UPSProvider>();
rateManager.AddProvider(new UPSProvider(upsConfiguration, httpClient, upsProviderLogger));

//// FedEx
rateManager.AddProvider(new FedExProvider(fedexKey, fedexPassword, fedexAccountNumber, fedexMeterNumber, fedexUseProduction));
rateManager.AddProvider(new FedExSmartPostProvider(fedexKey, fedexPassword, fedexAccountNumber, fedexMeterNumber, fedexHubId, fedexUseProduction));

//// USPS Domestic
rateManager.AddProvider(new USPSProvider(new USPSProviderConfiguration(uspsUserId), httpClient));
// USPS International
rateManager.AddProvider(new USPSInternationalProvider(new USPSProviderConfiguration(uspsUserId), httpClient));

//// DHL
var dhlConfiguration = new DHLProviderConfiguration(dhlSiteId, dhlPassword, useProduction: true).ExcludeServices(new char[] { 'C' });
rateManager.AddProvider(new DHLProvider(dhlConfiguration, httpClient));

// Call GetRates()
var shipment = await rateManager.GetRatesAsync(origin, destination, packages,
new ShipmentOptions()
{
SaturdayDelivery = true
});

// Iterate through the rates returned
foreach (var rate in shipment.Rates)
{
Console.WriteLine(rate);
}

// Iterate through the errors returned
if (shipment.Errors.Count > 0)
{
Console.WriteLine("Errors:");
foreach (var error in shipment.Errors)
// Iterate through the errors returned
if (shipment.Errors.Count > 0)
{
Console.WriteLine(error.Number);
Console.WriteLine(error.Source);
Console.WriteLine(error.Description);
Console.WriteLine("Errors:");
foreach (var error in shipment.Errors)
{
Console.WriteLine(error.Number);
Console.WriteLine(error.Source);
Console.WriteLine(error.Description);
}
}
}

// Iterate through the internal errors
if (shipment.InternalErrors.Count > 0)
{
Console.WriteLine("Internal Errors:");
foreach (var error in shipment.InternalErrors)
// Iterate through the internal errors
if (shipment.InternalErrors.Count > 0)
{
Console.WriteLine(error);
Console.WriteLine("Internal Errors:");
foreach (var error in shipment.InternalErrors)
{
Console.WriteLine(error);
}
}
}
}

Console.WriteLine("Done!");
Console.WriteLine("Done!");
}
}
}
}
72 changes: 18 additions & 54 deletions SampleApp/SampleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,65 +34,13 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.IdentityModel" />
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Security.Cryptography.Algorithms, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Algorithms.4.2.0\lib\net461\System.Security.Cryptography.Algorithms.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Encoding.4.0.0\lib\net46\System.Security.Cryptography.Encoding.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.Primitives.4.0.0\lib\net46\System.Security.Cryptography.Primitives.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.Security.Cryptography.X509Certificates, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Security.Cryptography.X509Certificates.4.1.0\lib\net461\System.Security.Cryptography.X509Certificates.dll</HintPath>
<Private>True</Private>
<Private>True</Private>
</Reference>
<Reference Include="System.ServiceModel" />
<Reference Include="System.ServiceModel.Http, Version=4.10.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.ServiceModel.Http.4.10.3\lib\net461\System.ServiceModel.Http.dll</HintPath>
</Reference>
<Reference Include="System.ServiceModel.Primitives, Version=4.10.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.ServiceModel.Primitives.4.10.3\lib\net461\System.ServiceModel.Primitives.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.8.0.0\lib\netstandard2.0\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.8.0.5\lib\netstandard2.0\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -107,13 +55,29 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ShippingRates\ShippingRates.csproj">
<Project>{0cac6d52-ffa9-4350-8e36-a22a2a4a8b88}</Project>
<Name>ShippingRates</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console">
<Version>3.1.0</Version>
</PackageReference>
<PackageReference Include="System.Security.Cryptography.X509Certificates">
<Version>4.1.2</Version>
</PackageReference>
<PackageReference Include="System.ServiceModel.Http">
<Version>4.10.3</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>8.0.5</Version>
</PackageReference>
<PackageReference Include="System.ValueTuple">
<Version>4.5.0</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
20 changes: 20 additions & 0 deletions SampleApp/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
18 changes: 0 additions & 18 deletions SampleApp/packages.config

This file was deleted.

42 changes: 42 additions & 0 deletions ShippingRates/Models/RateResultBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;

namespace ShippingRates.Models
{
internal class RateResultBuilder
{
private readonly string _providerName;
private readonly List<Rate> _rates = new List<Rate>();
private readonly List<Error> _errors = new List<Error>();
private readonly List<string> _internalErrors = new List<string>();

internal RateResultBuilder(string providerName)
{
_providerName = providerName;
}

internal void AddError(Error error)
{
_errors.Add(error);
}

internal void AddInternalError(string error)
{
_internalErrors.Add(error);
}

internal void AddRate(string providerCode, string name, decimal totalCharges, DateTime delivery, RateOptions options, string currencyCode)
{
_rates.Add(new Rate(_providerName, providerCode, name, totalCharges, delivery, options, currencyCode));
}

internal RateResult GetRateResult()
{
var result = new RateResult();
result.Rates.AddRange(_rates);
result.Errors.AddRange(_errors);
result.InternalErrors.AddRange(_internalErrors);
return result;
}
}
}
2 changes: 0 additions & 2 deletions ShippingRates/Models/Shipment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using ShippingRates.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
Expand All @@ -12,7 +11,6 @@ namespace ShippingRates
public class Shipment
{
public ReadOnlyCollection<Package> Packages { get; }
public ICollection<IRateAdjuster> RateAdjusters { get; set; }
public Address DestinationAddress { get; }
public Address OriginAddress { get; }
public ShipmentOptions Options { get; }
Expand Down
Loading

0 comments on commit 04b2488

Please sign in to comment.