forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddress.go
More file actions
176 lines (155 loc) · 7 KB
/
Copy pathaddress.go
File metadata and controls
176 lines (155 loc) · 7 KB
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
package easypost
import (
"context"
"net/http"
"time"
)
// AddressVerificationFieldError provides additional information on address
// validation errors.
type AddressVerificationFieldError struct {
Code string `json:"code,omitempty"`
Field string `json:"field,omitempty"`
Message string `json:"message,omitempty"`
Suggestion string `json:"suggestion,omitempty"`
}
// AddressVerificationDetails contains extra information related to address
// verification.
type AddressVerificationDetails struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
TimeZone string `json:"time_zone"`
}
// AddressVerification holds data relating to address verification status.
type AddressVerification struct {
Success bool `json:"success"`
Errors []*AddressVerificationFieldError `json:"errors"`
Details *AddressVerificationDetails `json:"details"`
}
// AddressVerifications contains the result of the requested address
// verifications.
type AddressVerifications struct {
ZIP4 *AddressVerification `json:"zip4"`
Delivery *AddressVerification `json:"delivery"`
}
// Address objects are used to represent people, places, and organizations in a
// number of contexts.
type Address struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Reference string `json:"reference,omitempty"`
Mode string `json:"mode,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Street1 string `json:"street1,omitempty"`
Street2 string `json:"street2,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
Zip string `json:"zip,omitempty"`
Country string `json:"country,omitempty"`
Name string `json:"name,omitempty"`
Company string `json:"company,omitempty"`
Phone string `json:"phone,omitempty"`
Email string `json:"email,omitempty"`
Residential bool `json:"residential,omitempty"`
CarrierFacility string `json:"carrier_facility,omitempty"`
FederalTaxID string `json:"federal_tax_id,omitempty"`
StateTaxID string `json:"state_tax_id,omitempty"`
Verifications *AddressVerifications `json:"verifications,omitempty"`
}
// CreateAddressOptions is used to specify verification options for address
// creation.
type CreateAddressOptions struct {
Verify []string `json:"verify,omitempty"`
VerifyStrict []string `json:"verify_strict,omitempty"`
}
type createAddressRequest struct {
*CreateAddressOptions
Address *Address `json:"address,omitempty"`
}
type AddressVerifyResponse struct {
Address *Address `json:"address,omitempty"`
}
// ListAddressResult holds the results from the list addresses API.
type ListAddressResult struct {
Addresses []*Address `json:"addresses,omitempty"`
// HasMore indicates if there are more responses to be fetched. If True,
// additional responses can be fetched by updating the ListAddressOptions
// parameter's AfterID field with the ID of the last item in this object's
// Addresses field.
HasMore bool `json:"has_more,omitempty"`
}
// For some reason, the verify API returns the address in a nested dictionary.
type verifyAddressResponse struct {
Address **Address `json:"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.post(ctx, "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", c.convertOptsToURLValues(opts), &out)
return
}
// 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.get(ctx, path, 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.get(ctx, "addresses/"+addressID, &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.post(ctx, "addresses/create_and_verify", req, &response)
out = response.Address
return
}