-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathaddress.go
204 lines (179 loc) · 9.38 KB
/
address.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package easypost
import (
"context"
"net/http"
)
// AddressVerificationFieldError provides additional information on address
// validation errors.
type AddressVerificationFieldError struct {
Code string `json:"code,omitempty" url:"code,omitempty"`
Field string `json:"field,omitempty" url:"field,omitempty"`
Message string `json:"message,omitempty" url:"message,omitempty"`
Suggestion string `json:"suggestion,omitempty" url:"suggestion,omitempty"`
}
// AddressVerificationDetails contains extra information related to address
// verification.
type AddressVerificationDetails struct {
Latitude float64 `json:"latitude,omitempty" url:"latitude,omitempty"`
Longitude float64 `json:"longitude,omitempty" url:"longitude,omitempty"`
TimeZone string `json:"time_zone,omitempty" url:"time_zone,omitempty"`
}
// AddressVerification holds data relating to address verification status.
type AddressVerification struct {
Success bool `json:"success,omitempty" url:"success,omitempty"`
Errors []*AddressVerificationFieldError `json:"errors,omitempty" url:"errors,omitempty"`
Details *AddressVerificationDetails `json:"details,omitempty" url:"details,omitempty"`
}
// AddressVerifications contains the result of the requested address
// verifications.
type AddressVerifications struct {
ZIP4 *AddressVerification `json:"zip4,omitempty" url:"zip4,omitempty"`
Delivery *AddressVerification `json:"delivery,omitempty" url:"delivery,omitempty"`
}
// Address objects are used to represent people, places, and organizations in a
// number of contexts.
type Address struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
Reference string `json:"reference,omitempty" url:"reference,omitempty"`
Mode string `json:"mode,omitempty" url:"mode,omitempty"`
CreatedAt *DateTime `json:"created_at,omitempty" url:"created_at,omitempty"`
UpdatedAt *DateTime `json:"updated_at,omitempty" url:"updated_at,omitempty"`
Street1 string `json:"street1,omitempty" url:"street1,omitempty"`
Street2 string `json:"street2,omitempty" url:"street2,omitempty"`
City string `json:"city,omitempty" url:"city,omitempty"`
State string `json:"state,omitempty" url:"state,omitempty"`
Zip string `json:"zip,omitempty" url:"zip,omitempty"`
Country string `json:"country,omitempty" url:"country,omitempty"`
Name string `json:"name,omitempty" url:"name,omitempty"`
Company string `json:"company,omitempty" url:"company,omitempty"`
Phone string `json:"phone,omitempty" url:"phone,omitempty"`
Email string `json:"email,omitempty" url:"email,omitempty"`
Residential bool `json:"residential,omitempty" url:"residential,omitempty"`
CarrierFacility string `json:"carrier_facility,omitempty" url:"carrier_facility,omitempty"`
FederalTaxID string `json:"federal_tax_id,omitempty" url:"federal_tax_id,omitempty"`
StateTaxID string `json:"state_tax_id,omitempty" url:"state_tax_id,omitempty"`
Verifications *AddressVerifications `json:"verifications,omitempty" url:"verifications,omitempty"`
}
// CreateAddressOptions is used to specify verification options for address
// creation.
type CreateAddressOptions struct {
// TODO: These should be booleans, not strings, like the other libs
Verify []string `json:"verify,omitempty" url:"verify,omitempty"`
VerifyStrict []string `json:"verify_strict,omitempty" url:"verify_strict,omitempty"`
}
type createAddressRequest struct {
*CreateAddressOptions
Address *Address `json:"address,omitempty" url:"address,omitempty"`
}
type AddressVerifyResponse struct {
Address *Address `json:"address,omitempty" url:"address,omitempty"`
}
// ListAddressResult holds the results from the list addresses API.
type ListAddressResult struct {
Addresses []*Address `json:"addresses,omitempty" url:"addresses,omitempty"`
PaginatedCollection
}
// For some reason, the verify API returns the address in a nested dictionary.
type verifyAddressResponse struct {
Address **Address `json:"address,omitempty" url:"address,omitempty"`
}
// CreateAddress submits a request to create a new address, and returns the
// result.
//
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateAddress(
// &easypost.Address{
// Street1: "417 Montgomery Street",
// Street2: "Floor 5",
// City: "San Francisco",
// State: "CA",
// Zip: "94104",
// Country: "US",
// Company: "EasyPost",
// Phone: "415-123-4567",
// },
// &CreateAddressOptions{Verify: []string{"delivery"}},
// )
func (c *Client) CreateAddress(in *Address, opts *CreateAddressOptions) (out *Address, err error) {
return c.CreateAddressWithContext(context.Background(), in, opts)
}
// CreateAddressWithContext performs the same operation as CreateAddress, but
// allows specifying a context that can interrupt the request.
func (c *Client) CreateAddressWithContext(ctx context.Context, in *Address, opts *CreateAddressOptions) (out *Address, err error) {
req := &createAddressRequest{CreateAddressOptions: opts, Address: in}
err = c.do(ctx, http.MethodPost, "addresses", req, &out)
return
}
// ListAddresses provides a paginated result of Address objects.
func (c *Client) ListAddresses(opts *ListOptions) (out *ListAddressResult, err error) {
return c.ListAddressesWithContext(context.Background(), opts)
}
// ListAddressesWithContext performs the same operation as ListAddresses, but
// allows specifying a context that can interrupt the request.
func (c *Client) ListAddressesWithContext(ctx context.Context, opts *ListOptions) (out *ListAddressResult, err error) {
err = c.do(ctx, http.MethodGet, "addresses", opts, &out)
return
}
// GetNextAddressPage returns the next page of addresses
func (c *Client) GetNextAddressPage(collection *ListAddressResult) (out *ListAddressResult, err error) {
return c.GetNextAddressPageWithContext(context.Background(), collection)
}
// GetNextAddressPageWithPageSize returns the next page of addresses with a specific page size
func (c *Client) GetNextAddressPageWithPageSize(collection *ListAddressResult, pageSize int) (out *ListAddressResult, err error) {
return c.GetNextAddressPageWithPageSizeWithContext(context.Background(), collection, pageSize)
}
// GetNextAddressPageWithContext performs the same operation as GetNextAddressPage, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextAddressPageWithContext(ctx context.Context, collection *ListAddressResult) (out *ListAddressResult, err error) {
return c.GetNextAddressPageWithPageSizeWithContext(ctx, collection, 0)
}
// GetNextAddressPageWithPageSizeWithContext performs the same operation as GetNextAddressPageWithPageSize, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetNextAddressPageWithPageSizeWithContext(ctx context.Context, collection *ListAddressResult, pageSize int) (out *ListAddressResult, err error) {
if len(collection.Addresses) == 0 {
err = EndOfPaginationError
return
}
lastID := collection.Addresses[len(collection.Addresses)-1].ID
params, err := nextPageParameters(collection.HasMore, lastID, pageSize)
if err != nil {
return
}
return c.ListAddressesWithContext(ctx, params)
}
// VerifyAddress performs address verification.
func (c *Client) VerifyAddress(addressID string) (out *Address, err error) {
return c.VerifyAddressWithContext(context.Background(), addressID)
}
// VerifyAddressWithContext performs the same operation as VerifyAddress, but
// allows specifying a context that can interrupt the request.
func (c *Client) VerifyAddressWithContext(ctx context.Context, addressID string) (out *Address, err error) {
path := "addresses/" + addressID + "/verify"
res := &verifyAddressResponse{Address: &out}
err = c.do(ctx, http.MethodGet, path, nil, res)
return
}
// GetAddress retrieves a previously-created address by its ID.
func (c *Client) GetAddress(addressID string) (out *Address, err error) {
return c.GetAddressWithContext(context.Background(), addressID)
}
// GetAddressWithContext performs the same operation as GetAddress, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetAddressWithContext(ctx context.Context, addressID string) (out *Address, err error) {
err = c.do(ctx, http.MethodGet, "addresses/"+addressID, nil, &out)
return
}
// CreateAndVerifyAddress Create Address object and immediately verify it.
func (c *Client) CreateAndVerifyAddress(in *Address, opts *CreateAddressOptions) (out *Address, err error) {
return c.CreateAndVerifyAddressWithContext(context.Background(), in, opts)
}
// CreateAndVerifyAddressWithContext performs the same operation as CreateAndVerifyAddress, but allows
// specifying a context that can interrupt the request.
func (c *Client) CreateAndVerifyAddressWithContext(ctx context.Context, in *Address, opts *CreateAddressOptions) (out *Address, err error) {
req := &createAddressRequest{CreateAddressOptions: opts, Address: in}
response := AddressVerifyResponse{}
err = c.do(ctx, http.MethodPost, "addresses/create_and_verify", req, &response)
out = response.Address
return
}