Esempio n. 1
0
        public async Task WhenInValidRequestIsMade_ShouldReturnErrorNode()
        {
            Environment.SetEnvironmentVariable("UspsSettings:UserName", "633FMCON0605");
            var address = new AddressValidateRequest
            {
                Address = new AddressXML
                {
                    Address1 = "9625 W 99th Pl",
                    Address2 = "",
                    City     = "Oak Lawn",
                    Id       = "0",
                    State    = "IL",
                    Zip4     = "",
                    Zip5     = "60453"
                },
                UserId = _options.Value.UserName
            };

            var xml  = XmlSerializationUtility.GetXmlString(address);
            var url2 = $"https://secure.shippingapis.com/shippingApi.dll?API=verify&XML={xml}";

            var http     = new HttpClient();
            var response = await http.GetAsync(url2);

            var stream = await response.Content.ReadAsStreamAsync();

            XDocument doc = XDocument.Load(stream);

            IEnumerable <XElement> nodes =
                doc.Root.Elements().Descendants("Error");

            Assert.Single(nodes);
        }
        public void WhenXMLIsSerialized_ShouldMatchUspsSchema()
        {
            var expectedXML = "<AddressValidateRequest USERID=\"xxx\"><Address ID=\"0\"><Address1>123 Main St</Address1>" +
                              "<Address2 /><City>Bedrock</City><State>IL</State><Zip5>90210</Zip5><Zip4 /></Address></AddressValidateRequest>";

            var address = new AddressValidateRequest
            {
                Address = new AddressXML
                {
                    Address1 = "123 Main St",
                    Address2 = "",
                    City     = "Bedrock",
                    Id       = "0",
                    State    = "IL",
                    Zip4     = "",
                    Zip5     = "90210"
                },
                UserId = "xxx"
            };
            var xml = XmlSerializationUtility.GetXmlString <AddressValidateRequest>(address);

            Assert.Equal(expectedXML, xml);
        }
Esempio n. 3
0
        internal static async Task <List <Address> > ValidateAsync(List <Address> input)
        {
            // limit is 5 addresses per request
            string requestGuid = Guid.NewGuid().ToString();

            Log.Information("{area}: New request for {packageTotal} packages. {requestGuid}", "Validate()", input.Count, requestGuid);

            List <Address>         output = new();
            AddressValidateRequest request;
            int index = 0;

            while (index < input.Count)
            {
                request = new AddressValidateRequest
                {
                    Address = input.Skip(index).Take(5).ToList(),
                    USERID  = UspsApiUsername,
                };

                Log.Information("{area}: Fetching rates for {packageCount} package(s). {requestGuid}", "Validate()", input.Count, requestGuid);

                XmlSerializer xsSubmit = new(typeof(AddressValidateRequest));
                var           xml      = "";

                using (StringWriter sww = new())
                {
                    using XmlWriter writer = XmlWriter.Create(sww);
                    xsSubmit.Serialize(writer, request);
                    xml = sww.ToString();
                }

                string uspsUrl = "https://secure.shippingapis.com/ShippingAPI.dll";
                FormUrlEncodedContent formData = new(new[]
                {
                    new KeyValuePair <string, string>("API", "Verify"),
                    new KeyValuePair <string, string>("XML", xml)
                });

                HttpClient httpClient = new()
                {
                    Timeout = TimeSpan.FromSeconds(50)
                };
                HttpResponseMessage response = null;
                int      retryCount          = 0;
                DateTime responseTimer       = DateTime.Now;

                while (response == null || response.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    if (retryCount > 0)
                    {
                        Log.Warning("{area}: USPS Failed to Respond after " + retryCount + " seconds. Attempt {retryCount}. {requestGuid}", "Validate()", retryCount, requestGuid);
                    }

                    response = await httpClient.PostAsync(uspsUrl, formData).ConfigureAwait(false);

                    Thread.Sleep(1000 * retryCount);
                    httpClient.CancelPendingRequests();

                    retryCount++;

                    if (retryCount > 50)
                    {
                        Log.Error("{area}: USPS Failed to Respond after 50 attempts. {requestGuid}", "Validate()", retryCount, requestGuid);
                        throw new UspsApiException("408: After many attempts, the request to the USPS API did not recieve a response. Please try again later.");
                    }
                }

                TimeSpan responseTime = DateTime.Now.TimeOfDay.Subtract(responseTimer.TimeOfDay);
                var      content      = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                Log.Information("{area}: USPS response received in {responseTime} ms. {requestGuid}", "Validate()", responseTime.Milliseconds, requestGuid);

                try
                {
                    XmlSerializer           deserializer = new(typeof(AddressValidateResponse));
                    var                     ms           = new MemoryStream(Encoding.UTF8.GetBytes(content));
                    AddressValidateResponse responseJson = (AddressValidateResponse)deserializer.Deserialize(ms);

                    index += 5;

                    if (responseJson.Error != null)
                    {
                        Log.Warning("{area}: USPS Returned Error: {uspsErrorNumber} {uspsErrorDescription} {requestGuid}", "Validate()", responseJson.Error.Number, responseJson.Error.Description, requestGuid);
                    }

                    foreach (Address validatedAddress in responseJson.Address)
                    {
                        Address orig = input.First(i => i.AddressDetailId == validatedAddress.AddressDetailId);

                        if (validatedAddress.Error != null)
                        {
                            Log.Warning("{area}: USPS Returned Error: {uspsErrorNumber} {uspsErrorDescription} {requestGuid}", "Validate()", validatedAddress.Error.Number, validatedAddress.Error.Description, requestGuid);
                            orig.Error = validatedAddress.Error;
                        }

                        orig.Business             = validatedAddress.Business;
                        orig.CarrierRoute         = validatedAddress.CarrierRoute;
                        orig.CentralDeliveryPoint = validatedAddress.CentralDeliveryPoint;
                        orig.CityAbbreviation     = validatedAddress.CityAbbreviation;
                        orig.DeliveryPoint        = validatedAddress.DeliveryPoint;
                        orig.DPVCMRA         = validatedAddress.DPVCMRA;
                        orig.DPVConfirmation = validatedAddress.DPVConfirmation;
                        orig.DPVFootnotes    = validatedAddress.DPVFootnotes;
                        orig.Footnotes       = validatedAddress.Footnotes;
                        orig.Vacant          = validatedAddress.Vacant;

                        // backup origingal address and overwrite with validated
                        orig.OriginalAddress1 = orig.Address1;
                        orig.Address1         = validatedAddress.Address1;
                        orig.OriginalAddress2 = orig.Address2;
                        orig.Address2         = validatedAddress.Address2;
                        orig.OriginalCity     = orig.City;
                        orig.City             = validatedAddress.City;
                        orig.OriginalState    = orig.State;
                        orig.State            = validatedAddress.State;
                        orig.OriginalZip      = orig.Zip5;
                        orig.Zip5             = validatedAddress.Zip5 + "-" + validatedAddress.Zip4;
                        output.Add(orig);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("{area}: Exception: {ex} {requestGuid}", "Validate()", ex.ToString(), requestGuid);
                    throw new UspsApiException(ex);
                }
            }

            if (output.Count != input.Count)
            {
                // something went wrong because counts should always match
                Console.WriteLine("Counts did not match between input and output");
                Log.Error("{area}: Counts did not match between input and output. {requestGuid}", "Validate()", requestGuid);
                throw new UspsApiException("Counts did not match between input and output");
            }

            return(output);
        }
Esempio n. 4
0
        public async Task <IActionResult> GetAsync(Address model)
        {
            var webRootPath = _hostingEnvironment.ContentRootPath;

            Address address = new Address
            {
                Address1     = model.Address1 ?? "",
                Address2     = model.Address2 ?? "",
                State        = model.State ?? "",
                City         = model.City ?? "",
                Zip5         = model.Zip5 ?? "",
                Zip4         = model.Zip4 ?? "",
                FirmName     = model.FirmName ?? "",
                Urbanization = model.Urbanization ?? ""
            };


            string userId = _configuration["USPS:UserId"];
            AddressValidateRequest request = new AddressValidateRequest
            {
                Address = address,
                UserId  = userId,
            };

            XmlSerializer xsSubmit = new XmlSerializer(typeof(AddressValidateRequest));
            var           xml      = "";

            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww))
                {
                    xsSubmit.Serialize(writer, request);
                    xml = sww.ToString();
                }
            }
            var requestXmlFile = Path.Combine(webRootPath, "request.xml");

            System.IO.File.WriteAllText(requestXmlFile, xml);
            //string uspsUrl = "http://production.shippingapis.com/ShippingAPI.dll";
            string uspsUrl  = "https://secure.shippingapis.com/ShippingAPI.dll";
            var    formData = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("API", "Verify"),
                new KeyValuePair <string, string>("XML", xml)
            });
            HttpClient httpClient = new HttpClient();
            var        response   = await httpClient.PostAsync(uspsUrl, formData);

            var content = await response.Content.ReadAsStringAsync();

            var responseXmlFile = Path.Combine(webRootPath, "response.xml");

            System.IO.File.WriteAllText(responseXmlFile, content);

            try
            {
                XmlSerializer           deserializer = new XmlSerializer(typeof(AddressValidateResponse));
                var                     ms           = new MemoryStream(Encoding.UTF8.GetBytes(content));
                AddressValidateResponse responseJson = (AddressValidateResponse)deserializer.Deserialize(ms);
                return(Ok(responseJson));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //XmlSerializer serializer = new XmlSerializer(typeof(Error));
                //var ms = new MemoryStream(Encoding.UTF8.GetBytes(content));
                //Error error = (Error)serializer.Deserialize(ms);
                return(NotFound(ex.Message));
            }
        }