Skip to content

Commit

Permalink
OMG-25427 - Set SHIPPO-ACCOUNT-ID header (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
slavick authored Sep 18, 2024
2 parents 01b4d96 + 278e43f commit 44c1ed5
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 80 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/pkg/
/vendor/
/vendor/
.idea
12 changes: 6 additions & 6 deletions client/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
)

// CreateAddress creates a new address object.
func (c *Client) CreateAddress(input *models.AddressInput) (*models.Address, error) {
func (c *Client) CreateAddress(input *models.AddressInput, shippoSubAccountID string) (*models.Address, error) {
if input == nil {
return nil, errors.New("nil input")
}

output := &models.Address{}
err := c.do(http.MethodPost, "/addresses/", input, output)
err := c.do(http.MethodPost, "/addresses/", input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RetrieveAddress retrieves an existing address by object id.
func (c *Client) RetrieveAddress(objectID string) (*models.Address, error) {
func (c *Client) RetrieveAddress(objectID string, shippoSubAccountID string) (*models.Address, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.Address{}
err := c.do(http.MethodGet, "/addresses/"+objectID, nil, output)
err := c.do(http.MethodGet, "/addresses/"+objectID, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// ListAllAddresses lists all addresses.
func (c *Client) ListAllAddresses() ([]*models.Address, error) {
func (c *Client) ListAllAddresses(shippoSubAccountID string) ([]*models.Address, error) {
list := []*models.Address{}
err := c.doList(http.MethodGet, "/addresses/", nil, func(v json.RawMessage) error {
item := &models.Address{}
Expand All @@ -41,6 +41,6 @@ func (c *Client) ListAllAddresses() ([]*models.Address, error) {

list = append(list, item)
return nil
})
}, c.subAccountHeader(shippoSubAccountID))
return list, err
}
16 changes: 8 additions & 8 deletions client/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// RetrieveBatch retrieves an existing batch. BatchShipments are displayed 100 at a time.
// You can iterate through each "page" by specifying non-zero value to page parameter.
// You can also filter based on BatchShipment status using objectResultsFilter parameter
func (c *Client) RetrieveBatch(objectID string, page uint, objectResultsFilter string) (*models.Batch, error) {
func (c *Client) RetrieveBatch(objectID string, page uint, objectResultsFilter string, shippoSubAccountID string) (*models.Batch, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}
Expand All @@ -30,12 +30,12 @@ func (c *Client) RetrieveBatch(objectID string, page uint, objectResultsFilter s
}

output := &models.Batch{}
err := c.do(http.MethodGet, url, nil, output)
err := c.do(http.MethodGet, url, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// AddBatchShipmentsToBatch adds batch shipment(s) to an existing Batch.
func (c *Client) AddBatchShipmentsToBatch(objectID string, batchShipments []*models.BatchShipmentInput) (*models.Batch, error) {
func (c *Client) AddBatchShipmentsToBatch(objectID string, batchShipments []*models.BatchShipmentInput, shippoSubAccountID string) (*models.Batch, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}
Expand All @@ -44,12 +44,12 @@ func (c *Client) AddBatchShipmentsToBatch(objectID string, batchShipments []*mod
}

output := &models.Batch{}
err := c.do(http.MethodPost, "/batches/"+objectID+"/add_shipments", &batchShipments, output)
err := c.do(http.MethodPost, "/batches/"+objectID+"/add_shipments", &batchShipments, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RemoveBatchShipmentsFromBatch removes batch shipment(s) from an existing Batch.
func (c *Client) RemoveBatchShipmentsFromBatch(objectID string, batchShipmentIDs []string) (*models.Batch, error) {
func (c *Client) RemoveBatchShipmentsFromBatch(objectID string, batchShipmentIDs []string, shippoSubAccountID string) (*models.Batch, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}
Expand All @@ -58,20 +58,20 @@ func (c *Client) RemoveBatchShipmentsFromBatch(objectID string, batchShipmentIDs
}

output := &models.Batch{}
err := c.do(http.MethodPost, "/batches/"+objectID+"/remove_shipments", &batchShipmentIDs, output)
err := c.do(http.MethodPost, "/batches/"+objectID+"/remove_shipments", &batchShipmentIDs, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// PurchaseBatch purchases an existing batch with an ObjectStatus of "VALID".
// Once you send invoke this function, the batch ObjectStatus will be change to PURCHASING.
// When all the shipments are purchased, the ObjectStatus will change to PURCHASED
// and you will receive a batch_purchased webhook indicating that the batch has been purchased.
func (c *Client) PurchaseBatch(objectID string) (*models.Batch, error) {
func (c *Client) PurchaseBatch(objectID string, shippoSubAccountID string) (*models.Batch, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.Batch{}
err := c.do(http.MethodPost, "/batches/"+objectID+"/purchase", nil, output)
err := c.do(http.MethodPost, "/batches/"+objectID+"/purchase", nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}
20 changes: 10 additions & 10 deletions client/carrier_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ import (
)

// CreateCarrierAccount creates a new carrier account object.
func (c *Client) CreateCarrierAccount(input *models.CarrierAccountInput) (*models.CarrierAccount, error) {
func (c *Client) CreateCarrierAccount(input *models.CarrierAccountInput, shippoSubAccountID string) (*models.CarrierAccount, error) {
if input == nil {
return nil, errors.New("nil input")
}

output := &models.CarrierAccount{}
err := c.do(http.MethodPost, "/carrier_accounts/", input, output)
err := c.do(http.MethodPost, "/carrier_accounts/", input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RetrieveCarrierAccount retrieves an existing carrier account by object id.
func (c *Client) RetrieveCarrierAccount(objectID string) (*models.CarrierAccount, error) {
func (c *Client) RetrieveCarrierAccount(objectID string, shippoSubAccountID string) (*models.CarrierAccount, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.CarrierAccount{}
err := c.do(http.MethodGet, "/carrier_accounts/"+objectID, nil, output)
err := c.do(http.MethodGet, "/carrier_accounts/"+objectID, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// ListAllCarrierAccounts lists all carrier accounts.
func (c *Client) ListAllCarrierAccounts() ([]*models.CarrierAccount, error) {
func (c *Client) ListAllCarrierAccounts(shippoSubAccountID string) ([]*models.CarrierAccount, error) {
list := []*models.CarrierAccount{}
err := c.doList(http.MethodGet, "/carrier_accounts/", nil, func(v json.RawMessage) error {
item := &models.CarrierAccount{}
Expand All @@ -42,13 +42,13 @@ func (c *Client) ListAllCarrierAccounts() ([]*models.CarrierAccount, error) {

list = append(list, item)
return nil
})
}, c.subAccountHeader(shippoSubAccountID))
return list, err
}

// UpdateCarrierAccount updates an existing carrier account.
// AccountID and Carrier cannot be updated because they form the unique identifier together.
func (c *Client) UpdateCarrierAccount(objectID string, input *models.CarrierAccountInput) (*models.CarrierAccount, error) {
func (c *Client) UpdateCarrierAccount(objectID string, input *models.CarrierAccountInput, shippoSubAccountID string) (*models.CarrierAccount, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}
Expand All @@ -57,19 +57,19 @@ func (c *Client) UpdateCarrierAccount(objectID string, input *models.CarrierAcco
}

output := &models.CarrierAccount{}
err := c.do(http.MethodPut, "/carrier_accounts/"+objectID, input, output)
err := c.do(http.MethodPut, "/carrier_accounts/"+objectID, input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

func (c *Client) ConnectCarrierAccount(objectID, redirectUrl, state string) (string, error) {
func (c *Client) ConnectCarrierAccount(objectID, redirectUrl, state string, shippoSubAccountID string) (string, error) {
if objectID == "" {
return "", errors.New("Empty object ID")
}

url := fmt.Sprintf("/carrier_accounts/%s/signin/initiate?redirect_uri=%s&state=%s&redirect=false", objectID, redirectUrl, state)

output := &models.ConnectOauth{}
err := c.do(http.MethodGet, url, nil, output)
err := c.do(http.MethodGet, url, nil, output, c.subAccountHeader(shippoSubAccountID))
if err != nil {
return "", err
}
Expand Down
28 changes: 21 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func (c *Client) SetTraceLogger(logger *log.Logger) *log.Logger {
return oldLogger
}

func (c *Client) do(method, path string, input, output interface{}) error {
func (c *Client) do(method, path string, input, output interface{}, headers map[string]string) error {
url := shippoAPIBaseURL + path

req, err := c.createRequest(method, url, input)
req, err := c.createRequest(method, url, input, headers)
if err != nil {
return fmt.Errorf("Error creating request object: %s", err.Error())
}
Expand All @@ -58,11 +58,11 @@ func (c *Client) do(method, path string, input, output interface{}) error {
return nil
}

func (c *Client) doList(method, path string, input interface{}, outputCallback listOutputCallback) error {
func (c *Client) doList(method, path string, input interface{}, outputCallback listOutputCallback, headers map[string]string) error {
nextURL := shippoAPIBaseURL + path + "?results=25"

for {
req, err := c.createRequest(method, nextURL, input)
req, err := c.createRequest(method, nextURL, input, headers)
if err != nil {
return fmt.Errorf("Error creating request object: %s", err.Error())
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (c *Client) doList(method, path string, input interface{}, outputCallback l
return nil
}

func (c *Client) createRequest(method, url string, bodyObject interface{}) (req *http.Request, err error) {
func (c *Client) createRequest(method, url string, bodyObject interface{}, headers map[string]string) (req *http.Request, err error) {
var reqBodyDebug []byte

if c.logger != nil {
Expand All @@ -104,10 +104,9 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}) (req
return
}

headers := []string{}
for hk, hva := range req.Header {
for _, hv := range hva {
headers = append(headers, fmt.Sprintf("%s=%s", hk, hv))
c.logPrintf("Client.createRequest() Header %s=%s", hk, hv)
}
}

Expand Down Expand Up @@ -149,6 +148,13 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}) (req
req.Header.Set("Connection", "close")
req.Close = true

// add any passed in headers
if headers != nil {
for k, v := range headers {
req.Header.Set(k, v)
}
}

return req, nil
}

Expand Down Expand Up @@ -199,3 +205,11 @@ func (c *Client) logPrintf(format string, args ...interface{}) {
c.logger.Printf(format, args...)
}
}

func (c *Client) subAccountHeader(id string) map[string]string {
headers := make(map[string]string)
if len(id) > 0 {
headers["SHIPPO-ACCOUNT-ID"] = id
}
return headers
}
24 changes: 12 additions & 12 deletions client/customs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
)

// CreateCustomsItem creates a new customs item object.
func (c *Client) CreateCustomsItem(input *models.CustomsItemInput) (*models.CustomsItem, error) {
func (c *Client) CreateCustomsItem(input *models.CustomsItemInput, shippoSubAccountID string) (*models.CustomsItem, error) {
if input == nil {
return nil, errors.New("nil input")
}

output := &models.CustomsItem{}
err := c.do(http.MethodPost, "/customs/items/", input, output)
err := c.do(http.MethodPost, "/customs/items/", input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RetrieveCustomsItem retrieves an existing customs item by object id.
func (c *Client) RetrieveCustomsItem(objectID string) (*models.CustomsItem, error) {
func (c *Client) RetrieveCustomsItem(objectID string, shippoSubAccountID string) (*models.CustomsItem, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.CustomsItem{}
err := c.do(http.MethodGet, "/customs/items/"+objectID, nil, output)
err := c.do(http.MethodGet, "/customs/items/"+objectID, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// ListAllCustomsItems lists all customs item objects.
func (c *Client) ListAllCustomsItems() ([]*models.CustomsItem, error) {
func (c *Client) ListAllCustomsItems(shippoSubAccountID string) ([]*models.CustomsItem, error) {
list := []*models.CustomsItem{}
err := c.doList(http.MethodGet, "/customs/items/", nil, func(v json.RawMessage) error {
item := &models.CustomsItem{}
Expand All @@ -41,12 +41,12 @@ func (c *Client) ListAllCustomsItems() ([]*models.CustomsItem, error) {

list = append(list, item)
return nil
})
}, c.subAccountHeader(shippoSubAccountID))
return list, err
}

// CreateCustomsDeclaration creates a new customs declaration object.
func (c *Client) CreateCustomsDeclaration(input *models.CustomsDeclarationInput) (*models.CustomsDeclaration, error) {
func (c *Client) CreateCustomsDeclaration(input *models.CustomsDeclarationInput, shippoSubAccountID string) (*models.CustomsDeclaration, error) {
if input == nil {
return nil, errors.New("nil input")
}
Expand All @@ -58,23 +58,23 @@ func (c *Client) CreateCustomsDeclaration(input *models.CustomsDeclarationInput)
}

output := &models.CustomsDeclaration{}
err := c.do(http.MethodPost, "/customs/declarations/", input, output)
err := c.do(http.MethodPost, "/customs/declarations/", input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RetrieveCustomsDeclaration retrieves an existing customs declaration by object id.
func (c *Client) RetrieveCustomsDeclaration(objectID string) (*models.CustomsDeclaration, error) {
func (c *Client) RetrieveCustomsDeclaration(objectID string, shippoSubAccountID string) (*models.CustomsDeclaration, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.CustomsDeclaration{}
err := c.do(http.MethodGet, "/customs/declarations/"+objectID, nil, output)
err := c.do(http.MethodGet, "/customs/declarations/"+objectID, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// ListAllCustomsDeclaration lists all customs declaration objects.
func (c *Client) ListAllCustomsDeclaration() ([]*models.CustomsDeclaration, error) {
func (c *Client) ListAllCustomsDeclaration(shippoSubAccountID string) ([]*models.CustomsDeclaration, error) {
list := []*models.CustomsDeclaration{}
err := c.doList(http.MethodGet, "/customs/declarations/", nil, func(v json.RawMessage) error {
item := &models.CustomsDeclaration{}
Expand All @@ -84,6 +84,6 @@ func (c *Client) ListAllCustomsDeclaration() ([]*models.CustomsDeclaration, erro

list = append(list, item)
return nil
})
}, c.subAccountHeader(shippoSubAccountID))
return list, err
}
12 changes: 6 additions & 6 deletions client/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (
)

// CreateManifest creates a new manifest object.
func (c *Client) CreateManifest(input *models.ManifestInput) (*models.Manifest, error) {
func (c *Client) CreateManifest(input *models.ManifestInput, shippoSubAccountID string) (*models.Manifest, error) {
if input == nil {
return nil, errors.New("nil input")
}

output := &models.Manifest{}
err := c.do(http.MethodPost, "/manifests/", input, output)
err := c.do(http.MethodPost, "/manifests/", input, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// RetrieveManifest retrieves an existing manifest by object id.
func (c *Client) RetrieveManifest(objectID string) (*models.Manifest, error) {
func (c *Client) RetrieveManifest(objectID string, shippoSubAccountID string) (*models.Manifest, error) {
if objectID == "" {
return nil, errors.New("Empty object ID")
}

output := &models.Manifest{}
err := c.do(http.MethodGet, "/manifests/"+objectID, nil, output)
err := c.do(http.MethodGet, "/manifests/"+objectID, nil, output, c.subAccountHeader(shippoSubAccountID))
return output, err
}

// ListAllManifests lists all manifest objects.
func (c *Client) ListAllManifests() ([]*models.Manifest, error) {
func (c *Client) ListAllManifests(shippoSubAccountID string) ([]*models.Manifest, error) {
list := []*models.Manifest{}
err := c.doList(http.MethodGet, "/manifests/", nil, func(v json.RawMessage) error {
item := &models.Manifest{}
Expand All @@ -41,6 +41,6 @@ func (c *Client) ListAllManifests() ([]*models.Manifest, error) {

list = append(list, item)
return nil
})
}, c.subAccountHeader(shippoSubAccountID))
return list, err
}
Loading

0 comments on commit 44c1ed5

Please sign in to comment.