Skip to content

Commit 3ede812

Browse files
authored
OMG-25737 - Add SubAccount type and CreateSubAccount (#8)
2 parents 44c1ed5 + c5c7d45 commit 3ede812

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

client/carrier_account.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ func (c *Client) CreateCarrierAccount(input *models.CarrierAccountInput, shippoS
2020
return output, err
2121
}
2222

23+
func (c *Client) RegisterCarrierAccount(input *models.CarrierAccountInput, shippoSubAccountID string) (*models.CarrierAccount, error) {
24+
if input == nil {
25+
return nil, errors.New("nil input")
26+
}
27+
28+
output := &models.CarrierAccount{}
29+
err := c.doWithoutVersion(http.MethodPost, "/carrier_accounts/register/new", input, output, c.subAccountHeader(shippoSubAccountID))
30+
return output, err
31+
}
32+
2333
// RetrieveCarrierAccount retrieves an existing carrier account by object id.
2434
func (c *Client) RetrieveCarrierAccount(objectID string, shippoSubAccountID string) (*models.CarrierAccount, error) {
2535
if objectID == "" {

client/client.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
)
1414

1515
const (
16-
shippoAPIBaseURL = "https://api.goshippo.com/v1"
16+
shippoAPIBaseURL = "https://api.goshippo.com/v1"
17+
shippoAPIBaseURLNoVersion = "https://api.goshippo.com"
1718
)
1819

1920
type Client struct {
@@ -41,7 +42,17 @@ func (c *Client) SetTraceLogger(logger *log.Logger) *log.Logger {
4142
}
4243

4344
func (c *Client) do(method, path string, input, output interface{}, headers map[string]string) error {
44-
url := shippoAPIBaseURL + path
45+
return c._do(shippoAPIBaseURL, method, path, input, output, headers)
46+
}
47+
48+
// doWithoutVersion was added to support the /shippo-accounts endpoint that throws a 404 if `/v1/` is present
49+
// https://docs.goshippo.com/docs/platformaccounts/platform_using_accounts/#create-a-managed-shippo-account-for-your-customer
50+
func (c *Client) doWithoutVersion(method, path string, input, output interface{}, headers map[string]string) error {
51+
return c._do(shippoAPIBaseURLNoVersion, method, path, input, output, headers)
52+
}
53+
54+
func (c *Client) _do(baseUrl, method, path string, input, output interface{}, headers map[string]string) error {
55+
url := baseUrl + path
4556

4657
req, err := c.createRequest(method, url, input, headers)
4758
if err != nil {
@@ -105,8 +116,10 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}, heade
105116
}
106117

107118
for hk, hva := range req.Header {
108-
for _, hv := range hva {
109-
c.logPrintf("Client.createRequest() Header %s=%s", hk, hv)
119+
if hk != "Authorization" {
120+
for _, hv := range hva {
121+
c.logPrintf("Client.createRequest() Header %s=%s", hk, hv)
122+
}
110123
}
111124
}
112125

client/sub_account.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package client
2+
3+
import (
4+
"errors"
5+
"github.com/OrderMyGear/go-shippo/models"
6+
"net/http"
7+
)
8+
9+
func (c *Client) CreateSubAccount(input *models.SubAccountInput) (*models.SubAccount, error) {
10+
if input == nil {
11+
return nil, errors.New("nil input")
12+
}
13+
14+
output := &models.SubAccount{}
15+
err := c.doWithoutVersion(http.MethodPost, "/shippo-accounts", input, output, nil)
16+
return output, err
17+
}

models/sub_account.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package models
2+
3+
type SubAccountInput struct {
4+
Email string `json:"email"`
5+
FirstName string `json:"first_name"`
6+
LastName string `json:"last_name"`
7+
CompanyName string `json:"company_name"`
8+
}
9+
10+
type SubAccount struct {
11+
SubAccountInput
12+
CommonOutputFields
13+
Test bool `json:"test"`
14+
ObjectInfo *ObjectInfo `json:"object_info"`
15+
}

0 commit comments

Comments
 (0)