|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using ShipEngine.ApiClient.Api; |
| 7 | +using ShipEngine.ApiClient.Model; |
| 8 | + |
| 9 | +namespace ShipEngineSampleApp |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + static void Main(string[] args) |
| 14 | + { |
| 15 | + var shipment = new AddressValidatingShipment |
| 16 | + { |
| 17 | + Confirmation = AddressValidatingShipment.ConfirmationEnum.None, |
| 18 | + CarrierId = null, |
| 19 | + ServiceCode = null, |
| 20 | + |
| 21 | + Packages = new List<ShipmentPackage> |
| 22 | + { |
| 23 | + new ShipmentPackage |
| 24 | + { |
| 25 | + Weight = new Weight(8, Weight.UnitEnum.Ounce), |
| 26 | + Dimensions = new Dimensions(Dimensions.UnitEnum.Inch, 5, 5, 5) |
| 27 | + } |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + var apiKey = GetApiKey(); |
| 32 | + var carrier = ChooseCarrier(apiKey); |
| 33 | + |
| 34 | + shipment.ShipFrom = GetAddress( |
| 35 | + "Where are you shipping FROM?", |
| 36 | + new AddressDTO("John Doe", "5551234567", "Acme Corp.", "100 Main St.", null, null, "Austin", "TX", "78610", "US") |
| 37 | + ); |
| 38 | + |
| 39 | + shipment.ShipTo = GetAddress( |
| 40 | + "Where are you shipping TO?", |
| 41 | + new AddressDTO("John Doe", "5551234567", "Acme Corp.", "100 Main St.", null, null, "Houston", "TX", "77002", "US") |
| 42 | + ); |
| 43 | + |
| 44 | + var rate = ChooseRate(shipment, carrier, apiKey); |
| 45 | + |
| 46 | + Console.WriteLine("Purchasing label..."); |
| 47 | + var isTestLabel = rate.CarrierCode == "stamps_com" || rate.CarrierCode == "endicia"; |
| 48 | + var labelsApi = new LabelsApi(); |
| 49 | + var label = labelsApi.LabelsPurchaseLabelWithRate(rate.RateId, new PurchaseLabelWithoutShipmentRequest(isTestLabel), apiKey); |
| 50 | + |
| 51 | + Console.WriteLine("Download your label:"); |
| 52 | + Console.WriteLine(label.LabelDownload.Href); |
| 53 | + |
| 54 | + WaitToQuit(); |
| 55 | + } |
| 56 | + |
| 57 | + static void WaitToQuit() |
| 58 | + { |
| 59 | + Console.WriteLine("\n\nPress any key to exit..."); |
| 60 | + Console.Read(); |
| 61 | + Environment.Exit(1); |
| 62 | + } |
| 63 | + |
| 64 | + static string GetApiKey() |
| 65 | + { |
| 66 | + Console.WriteLine("What's your API key?"); |
| 67 | + var apiKey = Console.ReadLine(); |
| 68 | + if (String.IsNullOrWhiteSpace(apiKey)) |
| 69 | + { |
| 70 | + apiKey = "ElJkhJuQIRoFq/kDEblco4LpZqRCdYNIoAVG7SywSXw"; |
| 71 | + Console.WriteLine($"Defaulting to {apiKey}"); |
| 72 | + } |
| 73 | + |
| 74 | + Console.WriteLine(); |
| 75 | + return apiKey; |
| 76 | + } |
| 77 | + |
| 78 | + static Carrier ChooseCarrier(string apiKey) |
| 79 | + { |
| 80 | + Console.WriteLine("Getting carriers..."); |
| 81 | + |
| 82 | + var carriersApi = new CarriersApi(); |
| 83 | + var carriers = carriersApi.CarriersList(apiKey).Carriers; |
| 84 | + |
| 85 | + Console.WriteLine($"Looks like you've got {carriers.Count} carriers setup."); |
| 86 | + for (var i = 0; i < carriers.Count; i++) |
| 87 | + { |
| 88 | + var iCarrier = carriers[i]; |
| 89 | + Console.WriteLine($"\t{i + 1}) {iCarrier.FriendlyName} ({iCarrier.CarrierId})"); |
| 90 | + } |
| 91 | + |
| 92 | + Console.Write("\nChoose a carrier: "); |
| 93 | + var selectionRaw = Console.ReadLine(); |
| 94 | + var selection = -1; |
| 95 | + if (selectionRaw == null || !int.TryParse(selectionRaw, out selection)) |
| 96 | + { |
| 97 | + Console.WriteLine("No!"); |
| 98 | + WaitToQuit(); |
| 99 | + } |
| 100 | + |
| 101 | + var carrier = carriers[selection - 1]; |
| 102 | + Console.WriteLine($"You selected {carrier.FriendlyName} ({carrier.CarrierId})."); |
| 103 | + Console.WriteLine(); |
| 104 | + |
| 105 | + return carrier; |
| 106 | + } |
| 107 | + |
| 108 | + static Rate ChooseRate(AddressValidatingShipment shipment, Carrier carrier, string apiKey) |
| 109 | + { |
| 110 | + Console.WriteLine("Getting rates..."); |
| 111 | + |
| 112 | + var ratesApi = new RatesApi(); |
| 113 | + var rateShipmentRequest = new RateShipmentRequest(shipment: shipment, rateOptions: new RateRequest(new List<string> { carrier.CarrierId })); |
| 114 | + var rates = ratesApi.RatesRateShipment(rateShipmentRequest, apiKey).RateResponse.Rates; |
| 115 | + |
| 116 | + Console.WriteLine("Let's choose a rate:"); |
| 117 | + |
| 118 | + for (var i = 0; i < rates.Count; i++) |
| 119 | + { |
| 120 | + Console.WriteLine($"\t{i + 1}) {rates[i].ServiceCode} - ${rates[i].ShippingAmount.Amount}"); |
| 121 | + } |
| 122 | + |
| 123 | + Console.Write("\nChoose a rate: "); |
| 124 | + |
| 125 | + var selectionRaw = Console.ReadLine(); |
| 126 | + var selection = -1; |
| 127 | + if (selectionRaw == null || !int.TryParse(selectionRaw, out selection)) |
| 128 | + { |
| 129 | + Console.WriteLine("No!"); |
| 130 | + WaitToQuit(); |
| 131 | + } |
| 132 | + |
| 133 | + Console.WriteLine($"You selected {rates[selection - 1].ServiceCode}."); |
| 134 | + Console.WriteLine(); |
| 135 | + |
| 136 | + return rates[selection - 1]; |
| 137 | + } |
| 138 | + |
| 139 | + static AddressDTO GetAddress(string displayText, AddressDTO defaultAddress) |
| 140 | + { |
| 141 | + Console.WriteLine($"\n{displayText}"); |
| 142 | + var address = new AddressDTO(); |
| 143 | + |
| 144 | + Console.Write("Name: "); |
| 145 | + address.Name = Console.ReadLine(); |
| 146 | + |
| 147 | + if (String.IsNullOrWhiteSpace(address.Name)) |
| 148 | + { |
| 149 | + address = defaultAddress; |
| 150 | + Console.WriteLine($"Defaulting to {address.AddressLine1}, {address.CityLocality}, {address.StateProvince} {address.PostalCode}"); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + Console.Write("Company: "); |
| 155 | + address.CompanyName = Console.ReadLine(); |
| 156 | + |
| 157 | + Console.Write("Address (line 1): "); |
| 158 | + address.AddressLine1 = Console.ReadLine(); |
| 159 | + |
| 160 | + Console.Write("Address (line 2): "); |
| 161 | + address.AddressLine2 = Console.ReadLine(); |
| 162 | + |
| 163 | + Console.Write("City/Locality: "); |
| 164 | + address.CityLocality = Console.ReadLine(); |
| 165 | + |
| 166 | + Console.Write("State/Province: "); |
| 167 | + address.StateProvince = Console.ReadLine(); |
| 168 | + |
| 169 | + Console.Write("Postal Code: "); |
| 170 | + address.PostalCode = Console.ReadLine(); |
| 171 | + |
| 172 | + Console.Write("Country Code: "); |
| 173 | + address.CountryCode = Console.ReadLine(); |
| 174 | + |
| 175 | + Console.Write("Phone: "); |
| 176 | + address.Phone = Console.ReadLine(); |
| 177 | + } |
| 178 | + |
| 179 | + Console.WriteLine(); |
| 180 | + return address; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments