11package client
22
33import (
4+ "bytes"
45 "encoding/json"
56 "errors"
67 "fmt"
8+ "io"
9+ "mime/multipart"
710 "net/http"
811
912 "github.com/OrderMyGear/go-shippo/models"
1013)
1114
15+ var (
16+ ErrEmptyObjectID = errors .New ("empty object ID" )
17+ ErrNilInput = errors .New ("nil input" )
18+ )
19+
1220// CreateCarrierAccount creates a new carrier account object.
1321func (c * Client ) CreateCarrierAccount (input * models.CarrierAccountInput , shippoSubAccountID string ) (* models.CarrierAccount , error ) {
1422 if input == nil {
15- return nil , errors . New ( "nil input" )
23+ return nil , ErrNilInput
1624 }
1725
1826 output := & models.CarrierAccount {}
@@ -22,7 +30,7 @@ func (c *Client) CreateCarrierAccount(input *models.CarrierAccountInput, shippoS
2230
2331func (c * Client ) RegisterCarrierAccount (input * models.CarrierAccountInput , shippoSubAccountID string ) (* models.CarrierAccount , error ) {
2432 if input == nil {
25- return nil , errors . New ( "nil input" )
33+ return nil , ErrNilInput
2634 }
2735
2836 output := & models.CarrierAccount {}
@@ -33,7 +41,7 @@ func (c *Client) RegisterCarrierAccount(input *models.CarrierAccountInput, shipp
3341// RetrieveCarrierAccount retrieves an existing carrier account by object id.
3442func (c * Client ) RetrieveCarrierAccount (objectID string , shippoSubAccountID string ) (* models.CarrierAccount , error ) {
3543 if objectID == "" {
36- return nil , errors . New ( "Empty object ID" )
44+ return nil , ErrEmptyObjectID
3745 }
3846
3947 output := & models.CarrierAccount {}
@@ -60,10 +68,10 @@ func (c *Client) ListAllCarrierAccounts(shippoSubAccountID string) ([]*models.Ca
6068// AccountID and Carrier cannot be updated because they form the unique identifier together.
6169func (c * Client ) UpdateCarrierAccount (objectID string , input * models.CarrierAccountInput , shippoSubAccountID string ) (* models.CarrierAccount , error ) {
6270 if objectID == "" {
63- return nil , errors . New ( "Empty object ID" )
71+ return nil , ErrEmptyObjectID
6472 }
6573 if input == nil {
66- return nil , errors . New ( "nil input" )
74+ return nil , ErrNilInput
6775 }
6876
6977 output := & models.CarrierAccount {}
@@ -73,7 +81,7 @@ func (c *Client) UpdateCarrierAccount(objectID string, input *models.CarrierAcco
7381
7482func (c * Client ) ConnectCarrierAccount (objectID , redirectUrl , state string , shippoSubAccountID string ) (string , error ) {
7583 if objectID == "" {
76- return "" , errors . New ( "Empty object ID" )
84+ return "" , ErrEmptyObjectID
7785 }
7886
7987 url := fmt .Sprintf ("/carrier_accounts/%s/signin/initiate?redirect_uri=%s&state=%s&redirect=false" , objectID , redirectUrl , state )
@@ -86,3 +94,33 @@ func (c *Client) ConnectCarrierAccount(objectID, redirectUrl, state string, ship
8694
8795 return output .RedirectUri , nil
8896}
97+
98+ func (c * Client ) UploadCarrierAccountDocument (objectID string , input * models.CarrierAccountDocumentInput , shippoSubAccountID string ) error {
99+ if objectID == "" {
100+ return ErrEmptyObjectID
101+ }
102+ if input == nil {
103+ return ErrNilInput
104+ }
105+
106+ var buf bytes.Buffer
107+ mw := multipart .NewWriter (& buf )
108+
109+ if err := mw .WriteField ("document_type" , input .DocumentType ); err != nil {
110+ return fmt .Errorf ("Error writing document_type field: %s" , err .Error ())
111+ }
112+
113+ fw , err := mw .CreateFormFile ("file" , input .Filename )
114+ if err != nil {
115+ return fmt .Errorf ("Error creating file field: %s" , err .Error ())
116+ }
117+ if _ , err := io .Copy (fw , input .File ); err != nil {
118+ return fmt .Errorf ("Error writing file content: %s" , err .Error ())
119+ }
120+
121+ if err := mw .Close (); err != nil {
122+ return fmt .Errorf ("Error closing multipart writer: %s" , err .Error ())
123+ }
124+
125+ return c .doRaw (http .MethodPost , "/carrier_accounts/" + objectID + "/documents" , & buf , mw .FormDataContentType (), nil , c .subAccountHeader (shippoSubAccountID ))
126+ }
0 commit comments