Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OMG-25427 - Set SHIPPO-ACCOUNT-ID header #7

Merged
merged 22 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8052583
Ignore .idea
slavick Sep 5, 2024
1caee0d
Set SHIPPO-ACCOUNT-ID header if property set in input object
slavick Sep 5, 2024
15d0c71
Change to use switch on object type to get shippo sub account id
slavick Sep 6, 2024
60b4966
Add ShippoSubAccountID to CarrierAccountInput
slavick Sep 6, 2024
301021e
Hoping this cleans up the go-shippo log output
slavick Sep 6, 2024
9b4c77d
Undo logging change that did not work
slavick Sep 6, 2024
93d21ba
Add ShippoSubAccountID to RefundInput
slavick Sep 9, 2024
3f16754
Add ShippoSubAccountID to TransactionInput
slavick Sep 9, 2024
94d48d4
Create ShippoSubAccount object to pass SHIPPO-ACCOUNT-ID for GET requ…
slavick Sep 9, 2024
688285e
Add ShippoSubAccountID to TrackingStatusInput
slavick Sep 10, 2024
91f7c33
Read shippo sub account ID from more types
slavick Sep 10, 2024
291719a
Remove code that creates header key values pairs for no reason
slavick Sep 10, 2024
cc97afa
Add temporary logging to determine if the shippo sub account id is be…
slavick Sep 10, 2024
9104863
Add headers to do and doList
slavick Sep 11, 2024
6430e7d
Change to pass headers to do and doList directly
slavick Sep 11, 2024
f429774
Add shippoSubAccountID everywhere
slavick Sep 11, 2024
ac4876a
Log request headers in defer
slavick Sep 11, 2024
b2f0c92
Don't need shippo sub account id for tracking status
slavick Sep 11, 2024
65e1b60
Don't require shippoSubAccountID to be passed to RetrieveCarrierAccou…
slavick Sep 12, 2024
2671a73
Revert "Don't require shippoSubAccountID to be passed to RetrieveCarr…
slavick Sep 12, 2024
8dc33b4
Change subAccountHeader to only set the SHIPPO-ACCOUNT-ID header if t…
slavick Sep 12, 2024
278e43f
Remove shippoSubAccountID from GetTrackingUpdate
slavick Sep 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions client/carrier_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func (c *Client) CreateCarrierAccount(input *models.CarrierAccountInput) (*model
}

// 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, &models.ShippoSubAccount{ShippoSubAccountID: shippoSubAccountID}, output)
return output, err
}

Expand Down
35 changes: 27 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ 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))
}
}

body := ""
if reqBodyDebug != nil {
body = string(reqBodyDebug)
Expand All @@ -121,7 +114,10 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}) (req
}()
}

var reqBody io.Reader
var (
reqBody io.Reader
subAcctID string
)
if bodyObject != nil {
data, err := json.Marshal(bodyObject)
if err != nil {
Expand All @@ -131,6 +127,21 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}) (req
reqBodyDebug = data

reqBody = bytes.NewBuffer(data)

switch v := bodyObject.(type) {
case models.CarrierAccountInput:
subAcctID = v.ShippoSubAccountID
case models.RefundInput:
subAcctID = v.ShippoSubAccountID
case models.ShipmentInput:
subAcctID = v.ShippoSubAccountID
case models.ShippoSubAccount:
subAcctID = v.ShippoSubAccountID
case models.TrackingStatusInput:
subAcctID = v.ShippoSubAccountID
case models.TransactionInput:
subAcctID = v.ShippoSubAccountID
}
slavick marked this conversation as resolved.
Show resolved Hide resolved
}

req, err = http.NewRequest(method, url, reqBody)
Expand All @@ -149,6 +160,14 @@ func (c *Client) createRequest(method, url string, bodyObject interface{}) (req
req.Header.Set("Connection", "close")
req.Close = true

// If a shippo sub account id is present, add it to the request headers
if len(subAcctID) > 0 {
c.logPrintf("Client.createRequest() setting SHIPPO-ACCOUNT-ID to %q", subAcctID)
req.Header.Set("SHIPPO-ACCOUNT-ID", subAcctID)
} else {
c.logPrintf("Client.createRequest() NOT setting SHIPPO-ACCOUNT-ID")
}

return req, nil
}

Expand Down
4 changes: 2 additions & 2 deletions client/shipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func (c *Client) CreateShipment(input *models.ShipmentInput) (*models.Shipment,
}

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

output := &models.Shipment{}
err := c.do(http.MethodGet, "/shipments/"+objectID, nil, output)
err := c.do(http.MethodGet, "/shipments/"+objectID, &models.ShippoSubAccount{ShippoSubAccountID: shippoSubAccountID}, output)
return output, err
}

Expand Down
13 changes: 7 additions & 6 deletions client/tracking_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// GetTrackingUpdate requests the tracking status of a shipment.
func (c *Client) GetTrackingUpdate(carrier, trackingNumber string) (*models.TrackingStatus, error) {
func (c *Client) GetTrackingUpdate(carrier, trackingNumber string, shippoSubAccountID string) (*models.TrackingStatus, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we'll need subaccount here, there's a carrier but it's usps not the carrier id: https://docs.goshippo.com/shippoapi/public-api/#operation/GetTrack

if carrier == "" {
return nil, errors.New("Empty carrier")
}
Expand All @@ -17,14 +17,14 @@ func (c *Client) GetTrackingUpdate(carrier, trackingNumber string) (*models.Trac
}

output := &models.TrackingStatus{}
err := c.do(http.MethodGet, "/tracks/"+carrier+"/"+trackingNumber, nil, output)
err := c.do(http.MethodGet, "/tracks/"+carrier+"/"+trackingNumber, &models.ShippoSubAccount{ShippoSubAccountID: shippoSubAccountID}, output)
return output, err
}

// RegisterTrackingWebhook registers a tracking webhook.
// TODO: documentation on this API endpoint is not clear.
// https://goshippo.com/docs/reference#tracks-create
func (c *Client) RegisterTrackingWebhook(carrier, trackingNumber, metadata string) (*models.TrackingStatus, error) {
func (c *Client) RegisterTrackingWebhook(carrier, trackingNumber, metadata string, shippoSubAccountID string) (*models.TrackingStatus, error) {
if carrier == "" {
return nil, errors.New("Empty carrier")
}
Expand All @@ -34,9 +34,10 @@ func (c *Client) RegisterTrackingWebhook(carrier, trackingNumber, metadata strin

output := &models.TrackingStatus{}
err := c.do(http.MethodPost, "/tracks/", &models.TrackingStatusInput{
Carrier: carrier,
TrackingNumber: trackingNumber,
Metadata: metadata,
Carrier: carrier,
TrackingNumber: trackingNumber,
Metadata: metadata,
ShippoSubAccountID: shippoSubAccountID,
}, output)
return output, err
}
4 changes: 2 additions & 2 deletions client/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func (c *Client) PurchaseShippingLabel(input *models.TransactionInput) (*models.
}

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

output := &models.Transaction{}
err := c.do(http.MethodGet, "/transactions/"+objectID, nil, output)
err := c.do(http.MethodGet, "/transactions/"+objectID, &models.ShippoSubAccount{ShippoSubAccountID: shippoSubAccountID}, output)
return output, err
}

Expand Down
9 changes: 5 additions & 4 deletions models/carrier_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package models

// See https://goshippo.com/docs/reference#carrier-accounts
type CarrierAccountInput struct {
Carrier string `json:"carrier"`
AccountID string `json:"account_id"`
Parameters map[string]interface{} `json:"parameters"`
Active bool `json:"active"`
Carrier string `json:"carrier"`
AccountID string `json:"account_id"`
Parameters map[string]interface{} `json:"parameters"`
Active bool `json:"active"`
ShippoSubAccountID string `json:"shippo_sub_account_id,omitempty"`
}

// See https://goshippo.com/docs/reference#carrier-accounts
Expand Down
5 changes: 3 additions & 2 deletions models/refund.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package models

// See https://goshippo.com/docs/reference#refunds
type RefundInput struct {
Transaction string `json:"transaction"`
Async bool `json:"async"`
Transaction string `json:"transaction"`
Async bool `json:"async"`
ShippoSubAccountID string `json:"shippo_sub_account_id,omitempty"`
}

// See https://goshippo.com/docs/reference#refunds
Expand Down
1 change: 1 addition & 0 deletions models/shipment.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type ShipmentInput struct {
Metadata string `json:"metadata,omitempty"`
Extra *ShipmentExtra `json:"extra,omitempty"`
Async bool `json:"async"`
ShippoSubAccountID string `json:"shippo_sub_account_id,omitempty"`
}

type ShipmentExtra struct {
Expand Down
5 changes: 5 additions & 0 deletions models/shippo_sub_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package models

type ShippoSubAccount struct {
ShippoSubAccountID string `json:"shippo_sub_account_id,omitempty"`
}
7 changes: 4 additions & 3 deletions models/tracking_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const (

// See https://goshippo.com/docs/reference#tracks
type TrackingStatusInput struct {
Carrier string `json:"carrier"`
TrackingNumber string `json:"tracking_number"`
Metadata string `json:"metadata,omitempty"`
Carrier string `json:"carrier"`
TrackingNumber string `json:"tracking_number"`
Metadata string `json:"metadata,omitempty"`
ShippoSubAccountID string `json:"shippo_subaccount_id,omitempty"`
}

// See https://goshippo.com/docs/reference#tracks
Expand Down
9 changes: 5 additions & 4 deletions models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import "time"

// See https://goshippo.com/docs/reference#transactions
type TransactionInput struct {
Rate string `json:"rate,omitempty"`
Metadata string `json:"metadata,omitempty"`
LabelFileType string `json:"label_file_type"`
Async bool `json:"async"`
Rate string `json:"rate,omitempty"`
Metadata string `json:"metadata,omitempty"`
LabelFileType string `json:"label_file_type"`
Async bool `json:"async"`
ShippoSubAccountID string `json:"shippo_sub_account_id,omitempty"`

Shipment *ShipmentInput `json:"shipment,omitempty"` // instant call only: https://goshippo.com/docs/reference#transactions-create-instant
CarrierAccount string `json:"carrier_account,omitempty"` // instant call only: https://goshippo.com/docs/reference#transactions-create-instant
Expand Down