Exemplo n.º 1
0
        //Method for USPS API
        public string AddressValidateReq(BuilderProduct product)
        {
            string strResponse = "";
            string strUSPS     = "";

            strUSPS  = BaseURL + "?API=Verify&XML=<AddressValidateRequest USERID=\"" + USPS_UserID + "\" PASSWORD=\"" + USPS_Password + "\">";
            strUSPS += "<Revision>1</Revision>";
            strUSPS += "<Address ID=\"0\">";
            strUSPS += "<Address1>" + "</Address1>";
            strUSPS += "<Address2>" + $"{product.StreetNum} {product.StreetName} {product.AptNum} " + "</Address2>";
            strUSPS += "<City>" + $"{product.City} " + "</City>";
            strUSPS += "<State>" + $"{product.State} " + "</State>";
            strUSPS += "<Zip5>" + $"{product.Zip} " + "</Zip5>";
            strUSPS += "<Zip4>" + "</Zip4>";
            strUSPS += "</Address></AddressValidateRequest>";

            //Send request to USPS
            strResponse = GetDataFromSite(strUSPS);

            return(strResponse);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Build USPS AddressValidateRequest XML from Excel Source data matrix
        /// Deserialize each USPS Response
        /// Parse response to format and store Address and Carrier Route
        /// </summary>
        public void USPSAddressValidateRequest()
        {
            XmlSerializer x = new XmlSerializer(typeof(AddressValidateResponse));

            AddressValidateResponseAddress[] addressResponse;

            //Clear lists when new file loaded
            _parsedResponse.AddressPlusCarrier.Clear();
            _parsedResponse.CarrierPlusTally.Clear();

            #region Builder pattern for building each XML request

            for (int k = 0; k < _excel.wsRowCount; ++k)
            {
                _director.Construct(_builder, _excel, k);
                _bproduct = _builder.Retrieve();
                string xData = _webTool.AddressValidateReq(_bproduct);

                AddressValidateResponse myResponse = (AddressValidateResponse)x.Deserialize(new StringReader(xData));
                addressResponse = myResponse.Items;

                #region Debug
                //===================== DEBUG - PRINT OUT RESPONSE PARSED =================================
                Debug.WriteLine($"Address: {addressResponse[0].Address2}");
                Debug.WriteLine($"City: {addressResponse[0].City} | State: {addressResponse[0].State} | Zip: {addressResponse[0].Zip5}");
                Debug.WriteLine($"Carrier: {addressResponse[0].CarrierRoute}");
                #endregion

                //Store the response address and carrier route (Prepare for report 1)
                _parsedResponse.AddressPlusCarrier.Add(new string[2] {
                    $"{addressResponse[0].Address2} {addressResponse[0].City} {addressResponse[0].State} {addressResponse[0].Zip5}", $"{addressResponse[0].CarrierRoute}"
                });

                //Store the carrier route and tally any repeats (Prepare for report 2)
                try
                {
                    if (_parsedResponse.CarrierPlusTally.ContainsKey(addressResponse[0].CarrierRoute))
                    {
                        _parsedResponse.CarrierPlusTally[addressResponse[0].CarrierRoute]++;
                    }
                    else
                    {
                        _parsedResponse.CarrierPlusTally.Add(addressResponse[0].CarrierRoute, 1);
                    }
                }
                catch (ArgumentNullException)
                {
                    if (_parsedResponse.CarrierPlusTally.ContainsKey("N/A"))
                    {
                        _parsedResponse.CarrierPlusTally["N/A"]++;
                    }
                    else
                    {
                        _parsedResponse.CarrierPlusTally.Add("N/A", 1);
                    }
                }
            }

            #endregion

            #region Validate req v1

            /*
             * //Call AddressValidateRequest however many times there are # of rows in Excel Source
             * for (int p = 0; p<_excel.wsRowCount; ++p)
             * {
             *  //string xData = _webTool.AddressValidateRequest("Address1", "Address2", "City", "State", "Zip5", "Zip4");
             *  string xData = _webTool.AddressValidateRequest(
             *      "",
             *      $"{_excel.dataMatrix[p, 0]} {_excel.dataMatrix[p, 1]} {_excel.dataMatrix[p, 2]}",
             *      $"{ _excel.dataMatrix[p, 3]}",
             *      $"{ _excel.dataMatrix[p, 4]}",
             *      $"{ _excel.dataMatrix[p, 5]}",
             *      "");
             *
             *  AddressValidateResponse myResponse = (AddressValidateResponse)x.Deserialize(new StringReader(xData));
             *  addressResponse = myResponse.Items;
             *
             *  //===================== DEBUG - PRINT OUT RESPONSE PARSED =================================
             *  Debug.WriteLine($"Loop iteration: {p}");
             *  Debug.WriteLine($"Address: {addressResponse[0].Address2}");
             *  Debug.WriteLine($"City: {addressResponse[0].City} | State: {addressResponse[0].State} | Zip: {addressResponse[0].Zip5}");
             *  Debug.WriteLine($"Carrier: {addressResponse[0].CarrierRoute}");
             *
             *  //Store the response address and carrier route (Prepare for report 1)
             *  _parsedResponse.AddressPlusCarrier.Add(new string[2] { $"{addressResponse[0].Address2} {addressResponse[0].City} {addressResponse[0].State} {addressResponse[0].Zip5}", $"{addressResponse[0].CarrierRoute}" });
             *
             *  //Store the carrier route and tally any repeats (Prepare for report 2)
             *  if (_parsedResponse.CarrierPlusTally.ContainsKey(addressResponse[0].CarrierRoute))
             *  {
             *      _parsedResponse.CarrierPlusTally[addressResponse[0].CarrierRoute]++;
             *  }
             *  else
             *  {
             *      _parsedResponse.CarrierPlusTally.Add(addressResponse[0].CarrierRoute, 1);
             *  }
             * }
             */
            #endregion

            Debug.WriteLine("AddressValidationComplete");
        }