public IEnumerable <ServiceInfo> InternationalServices(string userId, string password, string zip) { var request = new IntlRateV2Request { UserId = userId, Password = password, Packages = new List <InternationalPackage> { new InternationalPackage { Id = "1", SelectedMailType = MailType.All, Pounds = 1, OriginZip = zip, Country = "CANADA", ValueOfContents = "1.00", } } }; string response = DoRequest(request); if (HasError(response)) { throw new Exception("Service Request Error\n\n" + response); } var rateResponse = response.ToObject <IntlRateV2Response>(); return((from package in rateResponse.Packages from service in package.Services select new ServiceInfo { Id = service.Id, FullName = service.SvcDescription }).Distinct()); }
public IntlRateV2Response Get(IntlRateV2Request request) { string response = DoRequest(request); return(response.Contains("<Error>") && !response.Contains("<IntlRateV2Response>") ? new IntlRateV2Response { Error = response.ToObject <RequestError>() } : response.ToObject <IntlRateV2Response>()); }
private IntlRateV2Request CreateInternationalRequest(GetShippingOptionRequest shipmentPackage, string username, string password) { var request = new IntlRateV2Request { UserId = username, Password = password, Packages = new List <InternationalPackage>() }; decimal totalWeight = _shippingService.GetTotalWeight(shipmentPackage); IEnumerable <List <USPSVolumetrics> > splitVolumetrics = SplitShipmentByVolumetrics(shipmentPackage, totalWeight); foreach (var item in splitVolumetrics) { foreach (MailType baseService in EnabledBaseInternationalServices()) { request.Packages.Add(CreateInternationalPackage(shipmentPackage, item, baseService)); } } return(request); }
public static IntlRateV2Request GetInternationalRequest() { var request = new IntlRateV2Request { UserId = Settings.Default.UspsUserId, Password = Settings.Default.UspsPassword, Packages = new List <InternationalPackage> { new InternationalPackage { Pounds = 5, Ounces = 6, Machinable = true, SelectedMailType = MailType.Package, ValueOfContents = "600.00", Country = "Canada", Container = "RECTANGULAR", Width = 10, Length = 10, Height = 10, OriginZip = "18507", ExtraServices = new ExtraServices { ExtraService = new[] { "1" } } }, new InternationalPackage { Pounds = 5, Ounces = 6, Machinable = true, SelectedMailType = MailType.All, ValueOfContents = "600.00", Country = "Canada", Container = "RECTANGULAR", Width = 10, Length = 10, Height = 10, OriginZip = "18507", }, } }; return(request); }
private List <ShippingOption> DoInternationalRequest(GetShippingOptionRequest shipmentPackage, decimal minimumShippingRate, string password, string username) { IRateResponse response; IntlRateV2Request internationalRequest = CreateInternationalRequest(shipmentPackage, username, password); try { response = _rateService.Get(internationalRequest); } catch (Exception e) { throw new Exception("Unhandled exception with international request:\n\n" + internationalRequest.ToXmlString(), e); } return(response.Error == null ? InterpretShippingOptions((IntlRateV2Response)response, minimumShippingRate) : new List <ShippingOption> { new ShippingOption { Name = response.Error.Description } }); }
private string DoRequest(IntlRateV2Request request) { return(_request.GetResponse(_apiUrl, string.Format("API=IntlRateV2&XML={0}", request.ToXmlString()))); }
//[HttpGet("{country}")] public async Task <IActionResult> GetAsync([FromQuery] IntlRequestPackage model) { //return Ok(model); IntlRequestPackage package = new Models.IntlRateRequest.IntlRequestPackage { // AcceptanceDateTime = DateTime.UtcNow.AddDays(1), Country = model.Country, OriginZip = "90001", Ounces = 15.0m, PackageId = "1", ValueOfContents = "10.00", Container = "RECTANGULAR", Size = "REGULAR", Width = "15", Length = "15", Height = "16", Girth = "15", }; if (!string.IsNullOrEmpty(model.DestinationPostalCode)) { package.DestinationPostalCode = model.DestinationPostalCode; package.AcceptanceDateTime = DateTime.UtcNow.AddDays(1); } IntlRateV2Request intlRateV2Request = new IntlRateV2Request { Package = package }; XmlSerializer xsSubmit = new XmlSerializer(typeof(IntlRateV2Request)); var xml = ""; using (var sww = new StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, intlRateV2Request); xml = sww.ToString(); } } string uspsUrl = "http://production.shippingapis.com/ShippingAPI.dll"; var formData = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("API", "IntlRateV2"), new KeyValuePair <string, string>("XML", xml) }); HttpClient httpClient = new HttpClient(); var response = await httpClient.PostAsync(uspsUrl, formData); var content = await response.Content.ReadAsStringAsync(); var webRootPath = _hostingEnvironment.WebRootPath; var responseXmlFile = Path.Combine(webRootPath, "response.xml"); System.IO.File.WriteAllText(responseXmlFile, content); //XmlSerializer deserializer = new XmlSerializer(typeof(IntlRateV2Response)); //var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); //var responseJson = deserializer.Deserialize(ms); try { XmlSerializer deserializer = new XmlSerializer(typeof(IntlRateV2Response)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); IntlRateV2Response responseJson = (IntlRateV2Response)deserializer.Deserialize(ms); return(Ok(responseJson)); } catch (Exception ex) { XmlSerializer serializer = new XmlSerializer(typeof(Error)); var ms = new MemoryStream(Encoding.UTF8.GetBytes(content)); Error error = (Error)serializer.Deserialize(ms); return(NotFound(error)); } }