Example #1
0
        public InternationalResponse ValidateRequest(InternationalRequest request)
        {
            var result = new InternationalResponse();

            if (request == null)
            {
                result.Errors.Add(new USPSError {
                    Description = "Request was null"
                });
                return(result);
            }

            if (request.Packages.Count < 1)
            {
                result.Errors.Add(new USPSError {
                    Description = "Request requires at least one package."
                });
            }

            if (request.UserId.Trim().Length < 1)
            {
                result.Errors.Add(new USPSError {
                    Description = "UserId is Required for Requests"
                });
            }

            return(result);
        }
Example #2
0
        public InternationalResponse ProcessRequest(InternationalRequest request)
        {
            LastResponse = string.Empty;
            LastRequest  = string.Empty;

            // Validate Request First
            var result = ValidateRequest(request);

            if (result.Errors.Count > 0)
            {
                return(result);
            }

            try
            {
                var sURL = request.ApiUrl;
                sURL += "?API=IntlRateV2&XML=";

                // Build XML
                var requestXml = string.Empty;

                var sw = new StringWriter(CultureInfo.InvariantCulture);
                var xw = new XmlTextWriter(sw)
                {
                    Formatting = Formatting.None
                };

                // Start Request
                xw.WriteStartElement("IntlRateV2Request");
                xw.WriteAttributeString("USERID", request.UserId);
                xw.WriteElementString("Revision", request.Revision);

                foreach (var pak in request.Packages)
                {
                    pak.WriteToXml(ref xw);
                }

                //End Rate Request
                xw.WriteEndElement();
                xw.Flush();
                xw.Close();

                requestXml = sw.GetStringBuilder().ToString();

                if (!requestXml.StartsWith("<"))
                {
                    requestXml = requestXml.Substring(1, requestXml.Length - 1);
                }

                // Diagnostics
                LastRequest = requestXml;

                var sResponse  = string.Empty;
                var dataToSend = sURL + HttpUtility.UrlEncode(requestXml);

                sResponse = readHtmlPage(dataToSend);

                // Diagnostics
                LastResponse = sResponse;

                result = new InternationalResponse(sResponse);
            }
            catch (Exception ex)
            {
                result.Errors.Add(new USPSError
                {
                    Description = ex.Message + " " + ex.StackTrace,
                    Source      = "HCC Exception"
                });
            }
            return(result);
        }