-
Notifications
You must be signed in to change notification settings - Fork 0
/
usps.go
145 lines (125 loc) · 3.58 KB
/
usps.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package usps
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/url"
)
// Client is a USPS API client.
type Client struct {
userID string
endpoint string
client *http.Client
}
// An Option sets an option on a Client. It has private methods to prevent its
// use outside of this package.
type Option interface {
set(*Client)
}
// A function adapter that implements the Option interface.
type optionFunc func(*Client)
func (fn optionFunc) set(r *Client) { fn(r) }
// Configure a Client.
func (c *Client) setOption(options ...Option) {
for _, opt := range options {
opt.set(c)
}
}
// WithEndpoint sets the endpoint of the USPS API.
func WithEndpoint(endpoint string) Option {
return optionFunc(func(c *Client) {
c.endpoint = endpoint
})
}
// WithHTTPClient sets the http.Client used to communicate with the USPS API.
func WithHTTPClient(client *http.Client) Option {
return optionFunc(func(c *Client) {
c.client = client
})
}
// NewClient returns a USPS API client.
func NewClient(userID string, options ...Option) *Client {
c := &Client{
userID: userID,
endpoint: "http://production.shippingapis.com/ShippingAPI.dll",
client: http.DefaultClient,
}
c.setOption(options...)
return c
}
//ValidateZip returns non empty Response if successful
func (c *Client) ValidateZip(zipCode string) (*CityStateLookupResponse, error) {
req, err := http.NewRequest("GET", c.endpoint, nil)
if err != nil {
return nil, err
}
// Construct the URL encoded query
query := `<CityStateLookupRequest USERID=%q><ZipCode ID="0"><Zip5>%s</Zip5></ZipCode></CityStateLookupRequest>`
req.URL.RawQuery = fmt.Sprintf("API=CityStateLookup&XML=%s", url.QueryEscape(fmt.Sprintf(query, c.userID, zipCode)))
// Get the request
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
var (
decoder = xml.NewDecoder(resp.Body)
zipResp *CityStateLookupResponse
apiErr *APIError
)
for {
// Read tokens in the XML document from the stream (resp.Body)
t, err := decoder.Token()
if err == io.EOF || t == nil {
break // end of stream
}
if err != nil {
return nil, err
}
switch se := t.(type) {
case xml.StartElement:
switch se.Name.Local {
case "CityStateLookupResponse":
if err = decoder.DecodeElement(&zipResp, &se); err != nil {
return nil, err
}
case "Error":
if err = decoder.DecodeElement(&apiErr, &se); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown element: %q", se.Name.Local)
}
}
}
if apiErr != nil {
return nil, apiErr
}
if zipResp.ZipCode.Error != nil {
return nil, zipResp.ZipCode.Error
}
return zipResp, nil
}
// CityStateLookupResponse is the XML response for the CityStateLookupRequest
// type.
type CityStateLookupResponse struct {
XMLName xml.Name `xml:"CityStateLookupResponse" json:"-"`
ZipCode struct {
ID string `xml:"ID,attr,omitempty" json:"id,omitempty"`
Zip5 string `xml:"Zip5,omitempty" json:"zip5,omitempty"`
City string `xml:"City,omitempty" json:"city,omitempty"`
State string `xml:"State,omitempty" json:"state,omitempty"`
Error *APIError `xml:"Error,omitempty" json:"error,omitempty"`
} `xml:"ZipCode,omitempty" json:"zipcode,omitempty"`
}
// APIError is the XML structure for errors returned by the API.
type APIError struct {
XMLName xml.Name `xml:"Error" json:"-"`
Number string `xml:"Number,omitempty" json:"number"`
Description string `xml:"Description,omitempty" json:"description"`
Source string `xml:"Source,omitempty" json:"source"`
}
// Implement the error interface.
func (e *APIError) Error() string {
return e.Description
}