forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarrier.go
157 lines (139 loc) · 6.19 KB
/
carrier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package easypost
import (
"context"
"time"
)
// CarrierField provides data for a single field in a carrier account.
type CarrierField struct {
Visibility string `json:"visibility,omitempty"`
Label string `json:"label,omitempty"`
Value string `json:"value,omitempty"`
}
// CarrierFields contains the data for carrier account fields for production
// and/or test credentials.
type CarrierFields struct {
Credentials map[string]*CarrierField `json:"credentials,omitempty"`
TestCredentials map[string]*CarrierField `json:"test_credentials,omitempty"`
AutoLink bool `json:"auto_link,omitempty"`
CustomWorkflow bool `json:"custom_workflow,omitempty"`
}
// CarrierAccount encapsulates credentials and other information related to a
// carrier account.
type CarrierAccount struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Reference string `json:"reference,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
Type string `json:"type,omitempty"`
Fields *CarrierFields `json:"fields,omitempty"`
Clone bool `json:"clone,omitempty"`
Description string `json:"description,omitempty"`
Readable string `json:"readable,omitempty"`
Credentials map[string]string `json:"credentials"`
TestCredentials map[string]string `json:"test_credentials"`
BillingType string `json:"billing_type,omitempty"`
}
// CarrierType contains information on a supported carrier. It can be used to
// determine the valid fields for a carrier account.
type CarrierType struct {
Object string `json:"object,omitempty"`
Type string `json:"type,omitempty"`
Fields *CarrierFields `json:"fields,omitempty"`
}
type carrierAccountRequest struct {
CarrierAccount *CarrierAccount `json:"carrier_account,omitempty"`
}
// GetCarrierTypes returns a list of supported carrier types for the current
// user.
func (c *Client) GetCarrierTypes() (out []*CarrierType, err error) {
return c.GetCarrierTypesWithContext(context.Background())
}
// GetCarrierTypesWithContext performs the same operation as GetCarrierTypes,
// but allows specifying a context that can interrupt the request.
func (c *Client) GetCarrierTypesWithContext(ctx context.Context) (out []*CarrierType, err error) {
err = c.get(ctx, "carrier_types", &out)
return
}
// CreateCarrierAccount creates a new carrier account. It can only be used with
// a production API key.
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateCarrierAccount(
// &easypost.CarrierAccount{
// Type: "UpsAccount",
// Description: "NY Location UPS Account",
// Reference: "my reference",
// Credentials: map[string]string{
// "user_id": "USERID",
// "password": "PASSWORD",
// "access_license_number": "ALN",
// },
// },
// )
func (c *Client) CreateCarrierAccount(in *CarrierAccount) (out *CarrierAccount, err error) {
return c.CreateCarrierAccountWithContext(context.Background(), in)
}
// CreateCarrierAccountWithContext performs the same operation as
// CreateCarrierAccount, but allows specifying a context that can interrupt the
// request.
func (c *Client) CreateCarrierAccountWithContext(ctx context.Context, in *CarrierAccount) (out *CarrierAccount, err error) {
req := &carrierAccountRequest{CarrierAccount: in}
err = c.post(ctx, "carrier_accounts", req, &out)
return
}
// ListCarrierAccounts returns a list of all carrier accounts available to the
// authenticated account.
func (c *Client) ListCarrierAccounts() (out []*CarrierAccount, err error) {
return c.ListCarrierAccountsWithContext(context.Background())
}
// ListCarrierAccountsWithContext performs the same operation as
// ListCarrierAccounts, but allows specifying a context that can interrupt the
// request.
func (c *Client) ListCarrierAccountsWithContext(ctx context.Context) (out []*CarrierAccount, err error) {
err = c.get(ctx, "carrier_accounts", &out)
return
}
// GetCarrierAccount retrieves a carrier account by its ID or reference.
func (c *Client) GetCarrierAccount(carrierAccountID string) (out *CarrierAccount, err error) {
return c.GetCarrierAccountWithContext(context.Background(), carrierAccountID)
}
// GetCarrierAccountWithContext performs the same operation as
// GetCarrierAccount, but allows specifying a context that can interrupt the
// request.
func (c *Client) GetCarrierAccountWithContext(ctx context.Context, carrierAccountID string) (out *CarrierAccount, err error) {
err = c.get(ctx, "carrier_accounts/"+carrierAccountID, &out)
return
}
// UpdateCarrierAccount updates the carrier account. Only the Description,
// Reference, Credentials and TestCredentials attributes can be updated.
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.UpdateCarrierAccount(
// &easypost.CarrierAccount{
// ID: "ca_1001",
// Description: "FL Location UPS Account",
// Credentials: map[string]string{
// "account_number": "B2B2B2",
// },
// },
// )
func (c *Client) UpdateCarrierAccount(in *CarrierAccount) (out *CarrierAccount, err error) {
return c.UpdateCarrierAccountWithContext(context.Background(), in)
}
// UpdateCarrierAccountWithContext performs the same operation as
// UpdateCarrierAccount, but allows specifying a context that can interrupt the
// request.
func (c *Client) UpdateCarrierAccountWithContext(ctx context.Context, in *CarrierAccount) (out *CarrierAccount, err error) {
req := &carrierAccountRequest{CarrierAccount: in}
err = c.patch(ctx, "carrier_accounts/"+in.ID, req, &out)
return
}
// DeleteCarrierAccount removes the carrier account with the given ID.
func (c *Client) DeleteCarrierAccount(carrierAccountID string) error {
return c.DeleteCarrierAccountWithContext(context.Background(), carrierAccountID)
}
// DeleteCarrierAccountWithContext performs the same operation as
// DeleteCarrierAccount, but allows specifying a context that can interrupt the
// request.
func (c *Client) DeleteCarrierAccountWithContext(ctx context.Context, carrierAccountID string) error {
return c.del(ctx, "carrier_accounts/"+carrierAccountID)
}