-
Notifications
You must be signed in to change notification settings - Fork 0
/
USPSApi.cs
131 lines (119 loc) · 4.05 KB
/
USPSApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
using DxTLabel.Properties;
namespace DxTLabel
{
public class USPSAddress
{
public int ID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip5 { get; set; }
public string Zip4 { get; set; }
public XDocument GetAddressValidateRequestDocument()
{
XDocument requestDoc = new XDocument(
new XElement("AddressValidateRequest",
new XAttribute("USERID", Settings.Default.UspsApiUserID),
new XElement("Revision", "1"),
new XElement("Address",
new XAttribute("ID", ID),
new XElement("Address1", Address1),
new XElement("Address2", Address2),
new XElement("City", City),
new XElement("State", State),
new XElement("Zip5", Zip5),
new XElement("Zip4", Zip4)
)
)
);
return requestDoc;
}
public bool FixAddress()
{
var request = GetAddressValidateRequestDocument();
var result = new USPSApi(Settings.Default.UspsApiUrl).Request(USPSApi.Endpoint.VERIFY, request);
if (result != null)
{
if (!result.Descendants("Address").Any() ||
result.Descendants("Error").Any())
return false;
var addressElement = result.Descendants("Address").First();
Address1 = GetXMLElement(addressElement, "Address1");
Address2 = GetXMLElement(addressElement, "Address2");
City = GetXMLElement(addressElement, "City");
State = GetXMLElement(addressElement, "State");
Zip5 = GetXMLElement(addressElement, "Zip5");
Zip4 = GetXMLElement(addressElement, "Zip4");
return true;
}
return false;
}
internal static string GetXMLElement(XElement element, string name)
{
var el = element.Element(name);
if (el != null)
{
return el.Value;
}
return "";
}
public override string ToString()
{
var sb = new StringBuilder(string.Empty);
if (Address2.Length > 0) sb.Append(Address2);
if (Address1.Length > 0) sb.Append(" " + Address1);
sb.AppendLine();
sb.Append($"{City}, {State} {Zip5}");
if (Zip4.Length > 0) sb.Append($"-{Zip4}");
return sb.ToString();
}
}
internal class USPSApiEndpoint
{
public string Target { get; set; }
public USPSApiEndpoint(string target)
{
Target = target;
}
}
internal class USPSApi
{
private string _url;
public enum Endpoint
{
VERIFY,
}
private Dictionary<Endpoint, USPSApiEndpoint> _endpoints = new Dictionary<Endpoint, USPSApiEndpoint>()
{
{ Endpoint.VERIFY, new USPSApiEndpoint("API=Verify") },
};
public USPSApi(string apiUrl)
{
_url = apiUrl;
}
public XDocument Request(Endpoint endpoint, XDocument request)
{
try
{
var url = $"{_url}?{_endpoints[endpoint].Target}&XML=" + request;
var client = new WebClient();
var response = client.DownloadString(url);
var xdoc = XDocument.Parse(response.ToString());
return xdoc;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
}
}