Skip to content

Commit

Permalink
Merge pull request #57 from EasyPost/createAndVerify
Browse files Browse the repository at this point in the history
add create_and_verify endpoint functions in Address.
  • Loading branch information
Justintime50 authored Mar 8, 2022
2 parents c506873 + 04a1d04 commit 99cc6a2
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## Next Release

* Add missing `CreateAndVerifyAddress` and `CreateAndVerifyAddressWithContext` functions in Address file

## v2.1.0 (2022-03-07)

- Adds `CreateRefund`, `CreateRefundWithContext`, `ListRefunds`, `ListRefundsWithContext`, `GetRefund`, and `GetRefundWithContext` in the Refund file
Expand Down
25 changes: 24 additions & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type createAddressRequest struct {
Address *Address `json:"address,omitempty"`
}

type AddressVerifyResponse struct {
Address *Address `json:"address,omitempty"`
}

// CreateAddress submits a request to create a new address, and returns the
// result.
// c := easypost.New(MyEasyPostAPIKey)
Expand All @@ -89,7 +93,7 @@ type createAddressRequest struct {
// Company: "EasyPost",
// Phone: "415-123-4567",
// },
// &CreateAddrssOptions{Verify: []string{"delivery"}},
// &CreateAddressOptions{Verify: []string{"delivery"}},
// )
func (c *Client) CreateAddress(in *Address, opts *CreateAddressOptions) (out *Address, err error) {
req := &createAddressRequest{CreateAddressOptions: opts, Address: in}
Expand Down Expand Up @@ -161,3 +165,22 @@ func (c *Client) GetAddressWithContext(ctx context.Context, addressID string) (o
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) {
req := &createAddressRequest{CreateAddressOptions: opts, Address: in}
response := AddressVerifyResponse{}
err = c.post(context.Background(), "addresses/create_and_verify", req, &response)
out = response.Address
return
}

// 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
}
16 changes: 16 additions & 0 deletions tests/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ func (c *ClientTests) TestAddressCreateVerify() {
assert.Equal("417 MONTGOMERY ST STE 500", address.Street1)
}

func (c *ClientTests) TestAddressCreateAndVerify() {
client := c.TestClient()
assert := c.Assert()

address, _ := client.CreateAndVerifyAddress(
c.fixture.IncorrectAddressToVerify(),
&easypost.CreateAddressOptions{
Verify: []string{"delivery"},
},
)

assert.Equal(reflect.TypeOf(&easypost.Address{}), reflect.TypeOf(address))
assert.True(strings.HasPrefix(address.ID, "adr_"))
assert.Equal("417 MONTGOMERY ST STE 500", address.Street1)
}

func (c *ClientTests) TestAddressVerify() {
client := c.TestClient()
assert := c.Assert()
Expand Down
64 changes: 64 additions & 0 deletions tests/cassettes/TestAddressCreateAndVerify.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
version: 1
interactions:
- request:
body: '{"verify":["delivery"],"address":{"street1":"417 montgomery streat","street2":"FL
5","city":"San Francisco","state":"CA","zip":"94104","country":"US","company":"EasyPost","phone":"415-123-4567"}}'
form: {}
headers:
Content-Type:
- application/json
User-Agent:
- EasyPost/v2 GoClient/2.1.0 Go/go1.17.5 OS/darwin
url: https://api.easypost.com/v2/addresses/create_and_verify
method: POST
response:
body: '{"address":{"id":"adr_5a6e12049e6811ecbac9ac1f6bc72124","object":"Address","created_at":"2022-03-07T22:45:57+00:00","updated_at":"2022-03-07T22:45:57+00:00","name":null,"company":"EASYPOST","street1":"417
MONTGOMERY ST STE 500","street2":"","city":"SAN FRANCISCO","state":"CA","zip":"94104-1100","country":"US","phone":"4151234567","email":null,"mode":"test","carrier_facility":null,"residential":false,"federal_tax_id":null,"state_tax_id":null,"verifications":{"delivery":{"success":true,"errors":[],"details":{"latitude":37.79342,"longitude":-122.40288,"time_zone":"America/Los_Angeles"}}}}}'
headers:
Cache-Control:
- no-cache, no-store
Content-Type:
- application/json; charset=utf-8
Etag:
- W/"0c5eba43ee83bf545d155b60787c81ba"
Expires:
- "0"
Location:
- /api/v2/addresses/adr_5a6e12049e6811ecbac9ac1f6bc72124
Pragma:
- no-cache
Referrer-Policy:
- strict-origin-when-cross-origin
Strict-Transport-Security:
- max-age=31536000; includeSubDomains; preload
X-Backend:
- easypost
X-Canary:
- direct
X-Content-Type-Options:
- nosniff
X-Download-Options:
- noopen
X-Ep-Request-Uuid:
- ea6435fe62268b25ff109c14004b2a92
X-Frame-Options:
- SAMEORIGIN
X-Node:
- bigweb7nuq
X-Permitted-Cross-Domain-Policies:
- none
X-Proxied:
- intlb2nuq 88c34981dc
- extlb1nuq 88c34981dc
X-Request-Id:
- e79d7c0a-f88d-4884-830d-97ed16444430
X-Runtime:
- "0.107753"
X-Version-Label:
- easypost-202203042109-ebc16e3e74-master
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: ""

0 comments on commit 99cc6a2

Please sign in to comment.