-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathProgram.cs
More file actions
145 lines (128 loc) · 6.54 KB
/
Copy pathProgram.cs
File metadata and controls
145 lines (128 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using ShippingRates.ShippingProviders;
using ShippingRates.ShippingProviders.FedEx;
using ShippingRates.ShippingProviders.Usps;
using ShippingRates;
namespace SampleApp
{
class Program
{
static async Task Main()
{
var appSettings = ConfigurationManager.AppSettings;
// You will need OAuth Client Id, Client Secret, and Account Number to use the UPS provider
// More details: https://developer.ups.com/oauth-developer-guide?loc=en_US
var upsClientId = appSettings["UPSClientId"];
var upsClientSecret = appSettings["UPSClientSecret"];
var upsAccountNumber = appSettings["UPSAccountNumber"];
// You will need Client Id, Client Secret, and Account # to use the FedEx provider.
var fedexClientId = appSettings["FedExClientId"];
var fedexClientSecret = appSettings["FedExClientSecret"];
var fedexAccountNumber = appSettings["FedExAccountNumber"];
var fedexHubId = appSettings["FedExHubId"]; // 5531 is the hubId to use in FedEx's test environment
var fedexUseProduction = Convert.ToBoolean(appSettings["FedExUseProduction"]);
// You will need a Site ID and Password to use DHL provider.
var dhlSiteId = appSettings["DHLSiteId"];
var dhlPassword = appSettings["DHLPassword"];
// Setup package and destination/origin addresses
var packages = new List<Package>
{
new Package(12, 12, 12, 35, 150),
new Package(4, 4, 6, 15, 250)
};
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("", "", "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
using (var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
}))
{
// Create RateManager
var rateManager = new RateManager();
using (var httpClient = new HttpClient())
{
// 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
var fedExConfiguration = new FedExProviderConfiguration()
{
ClientId = fedexClientId,
ClientSecret = fedexClientSecret,
AccountNumber = fedexAccountNumber,
HubId = fedexHubId,
UseProduction = fedexUseProduction
};
var fedExProviderLogger = loggerFactory.CreateLogger<FedExProvider>();
rateManager.AddProvider(new FedExProvider(fedExConfiguration, httpClient, fedExProviderLogger));
rateManager.AddProvider(new FedExSmartPostProvider(fedExConfiguration, httpClient));
// USPS
var uspsProviderConfiguration = new UspsProviderConfiguration()
{
ClientId = appSettings["UspsClientId"],
ClientSecret = appSettings["UspsClientSecret"],
UseProduction = Convert.ToBoolean(appSettings["UspsUseProduction"])
};
var uspsProviderLogger = loggerFactory.CreateLogger<UspsProvider>();
rateManager.AddProvider(new UspsProvider(uspsProviderConfiguration, httpClient, uspsProviderLogger));
// 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)
{
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)
{
Console.WriteLine(error);
}
}
}
Console.WriteLine("Done!");
}
}
}
}