forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshipment.go
414 lines (363 loc) · 20.2 KB
/
shipment.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package easypost
import (
"context"
"net/http"
"net/url"
"time"
)
// A Form represents a form associated with a Shipment.
type Form struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Mode string `json:"mode,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
FormType string `json:"form_type,omitempty"`
FormURL string `json:"form_url,omitempty"`
SubmittedElectronically bool `json:"submitted_electronically,omitempty"`
}
// PostageLabel provides details of a shipping label for a purchased shipment.
type PostageLabel struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
IntegratedForm string `json:"integrated_form,omitempty"`
LabelDate *time.Time `json:"label_date,omitempty"`
LabelEPL2URL string `json:"label_epl2_url,omitempty"`
LabelFileType string `json:"label_file_type,omitempty"`
LabelPDFURL string `json:"label_pdf_url,omitempty"`
LabelResolution float64 `json:"label_resolution,omitempty"`
LabelSize string `json:"label_size,omitempty"`
LabelType string `json:"label_type,omitempty"`
LabelURL string `json:"label_url,omitempty"`
LabelZPLURL string `json:"label_zpl_url,omitempty"`
}
// A Shipment represents its namesake, and is made up of a "to" and "from"
// addresses, the Parcel being shipped, and any customs forms required for
// international deliveries.
type Shipment struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
Reference string `json:"reference,omitempty"`
Mode string `json:"mode,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
ToAddress *Address `json:"to_address,omitempty"`
FromAddress *Address `json:"from_address,omitempty"`
ReturnAddress *Address `json:"return_address,omitempty"`
BuyerAddress *Address `json:"buyer_address,omitempty"`
Parcel *Parcel `json:"parcel,omitempty"`
Carrier string `json:"carrier,omitempty"`
Service string `json:"service,omitempty"`
CarrierAccountIDs []string `json:"carrier_accounts,omitempty"`
CustomsInfo *CustomsInfo `json:"customs_info,omitempty"`
ScanForm *ScanForm `json:"scan_form,omitempty"`
Forms []*Form `json:"forms,omitempty"`
Insurance string `json:"insurance,omitempty"`
Rates []*Rate `json:"rates,omitempty"`
SelectedRate *Rate `json:"selected_rate,omitempty"`
PostageLabel *PostageLabel `json:"postage_label,omitempty"`
Messages []*CarrierMessage `json:"messages,omitempty"`
Options *ShipmentOptions `json:"options,omitempty"`
IsReturn bool `json:"is_return,omitempty"`
TrackingCode string `json:"tracking_code,omitempty"`
USPSZone int `json:"usps_zone,omitempty"`
Status string `json:"status,omitempty"`
Tracker *Tracker `json:"tracker,omitempty"`
Fees []*Fee `json:"fees,omitempty"`
RefundStatus string `json:"refund_status,omitempty"`
BatchID string `json:"batch_id,omitempty"`
BatchStatus string `json:"batch_status,omitempty"`
BatchMessage string `json:"batch_message,omitempty"`
TaxIdentifiers []*TaxIdentifier `json:"tax_identifiers,omitempty"`
}
// ListShipmentsOptions is used to specify query parameters for listing Shipment
// objects.
type ListShipmentsOptions struct {
BeforeID string `url:"before_id,omitempty"`
AfterID string `url:"after_id,omitempty"`
StartDateTime *time.Time `url:"start_datetime,omitempty"`
EndDateTime *time.Time `url:"end_datetime,omitempty"`
PageSize int `url:"page_size,omitempty"`
Purchased *bool `url:"purchased,omitempty"`
IncludeChildren *bool `url:"include_children,omitempty"`
}
// ListShipmentsResult holds the results from the list shipments API.
type ListShipmentsResult struct {
Shipments []*Shipment `json:"shipments,omitempty"`
// HasMore indicates if there are more responses to be fetched. If True,
// additional responses can be fetched by updating the ListShipmentsOptions
// parameter's AfterID field with the ID of the last item in this object's
// Shipments field.
HasMore bool `json:"has_more,omitempty"`
}
type buyShipmentRequest struct {
Rate *Rate `json:"rate,omitempty"`
Insurance string `json:"insurance,omitempty"`
CarbonOffset bool `json:"carbon_offset,omitempty"`
EndShipperID string `json:"end_shipper_id,omitempty"`
}
type createShipmentRequest struct {
Shipment *Shipment `json:"shipment,omitempty"`
CarbonOffset bool `json:"carbon_offset,omitempty"`
}
type getShipmentRatesRequest struct {
CarbonOffset bool `json:"carbon_offset,omitempty"`
}
type getShipmentRatesResponse struct {
Rates *[]*Rate `json:"rates,omitempty"`
}
type generateFormRequest struct {
Form map[string]interface{} `json:"form,omitempty"`
}
// CreateShipment creates a new Shipment object. The ToAddress, FromAddress and
// Parcel attributes are required. These objects may be fully-specified to
// create new ones at the same time as creating the Shipment, or they can refer
// to existing objects via their ID attribute. Passing in one or more carrier
// accounts to CreateShipment limits the returned rates to the specified
// carriers.
//
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateShipment(
// &easypost.Shipment{
// ToAddress: &easypost.Address{
// Name: "Dr. Steve Brule",
// Street1: "179 N Harbor Dr",
// City: "Redondo Beach",
// State: "CA",
// Zip: "90277",
// Country: "US",
// Phone: "8573875756",
// Email: "[email protected]",
// },
// FromAddress: &easypost.Address{ID: "adr_101"},
// Parcel: &easypost.Parcel{
// Length: 20.2,
// Width: 10.9,
// Height: 5,
// Weight: 65.9,
// },
// CustomsInfo: &easypost.CustomsInfo{ID: "cstinfo_1"},
// },
// )
func (c *Client) CreateShipment(in *Shipment) (out *Shipment, err error) {
return c.CreateShipmentWithContext(context.Background(), in)
}
// CreateShipmentWithCarbonOffset performs the same operation as CreateShipment, but includes a carbon offset.
func (c *Client) CreateShipmentWithCarbonOffset(in *Shipment) (out *Shipment, err error) {
return c.CreateShipmentWithCarbonOffsetWithContext(context.Background(), in)
}
// CreateShipmentWithContext performs the same operation as CreateShipment, but
// allows specifying a context that can interrupt the request.
func (c *Client) CreateShipmentWithContext(ctx context.Context, in *Shipment) (out *Shipment, err error) {
req := &createShipmentRequest{Shipment: in}
err = c.post(ctx, "shipments", &req, &out)
return
}
// CreateShipmentWithCarbonOffsetWithContext performs the same operation as CreateShipmentWithCarbonOffset, but
// allows specifying a context that can interrupt the request.
func (c *Client) CreateShipmentWithCarbonOffsetWithContext(ctx context.Context, in *Shipment) (out *Shipment, err error) {
req := &createShipmentRequest{Shipment: in, CarbonOffset: true}
err = c.post(ctx, "shipments", &req, &out)
return
}
// ListShipments provides a paginated result of Shipment objects.
func (c *Client) ListShipments(opts *ListShipmentsOptions) (out *ListShipmentsResult, err error) {
return c.ListShipmentsWithContext(context.Background(), opts)
}
// ListShipmentsWithContext performs the same operation as ListShipments, but
// allows specifying a context that can interrupt the request.
func (c *Client) ListShipmentsWithContext(ctx context.Context, opts *ListShipmentsOptions) (out *ListShipmentsResult, err error) {
err = c.do(ctx, http.MethodGet, "shipments", c.convertOptsToURLValues(opts), &out)
return
}
// GetShipment retrieves a Shipment object by ID.
func (c *Client) GetShipment(shipmentID string) (out *Shipment, err error) {
return c.GetShipmentWithContext(context.Background(), shipmentID)
}
// GetShipmentWithContext performs the same operation as GetShipment, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetShipmentWithContext(ctx context.Context, shipmentID string) (out *Shipment, err error) {
err = c.get(ctx, "shipments/"+shipmentID, &out)
return
}
func (c *Client) buyShipment(ctx context.Context, shipmentID string, in *buyShipmentRequest) (out *Shipment, err error) {
err = c.post(ctx, "shipments/"+shipmentID+"/buy", &in, &out)
return
}
// BuyShipment purchases a shipment. If successful, the returned Shipment will
// have the PostageLabel attribute populated.
//
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.Buy("shp_100", &easypost.Rate{ID: "rate_1001"}, "249.99")
func (c *Client) BuyShipment(shipmentID string, rate *Rate, insurance string) (out *Shipment, err error) {
return c.BuyShipmentWithContext(context.Background(), shipmentID, rate, insurance)
}
// BuyShipmentWithContext performs the same operation as BuyShipment, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyShipmentWithContext(ctx context.Context, shipmentID string, rate *Rate, insurance string) (out *Shipment, err error) {
req := &buyShipmentRequest{Rate: rate, Insurance: insurance}
return c.buyShipment(ctx, shipmentID, req)
}
// BuyShipmentWithCarbonOffset performs the same operation as BuyShipment, but includes a carbon offset.
func (c *Client) BuyShipmentWithCarbonOffset(shipmentID string, rate *Rate, insurance string) (out *Shipment, err error) {
return c.BuyShipmentWithCarbonOffsetWithContext(context.Background(), shipmentID, rate, insurance)
}
// BuyShipmentWithCarbonOffsetWithContext performs the same operation as BuyShipmentWithCarbonOffset, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyShipmentWithCarbonOffsetWithContext(ctx context.Context, shipmentID string, rate *Rate, insurance string) (out *Shipment, err error) {
req := &buyShipmentRequest{Rate: rate, Insurance: insurance, CarbonOffset: true}
return c.buyShipment(ctx, shipmentID, req)
}
// BuyShipmentWithEndShipper performs the same operation as BuyShipment, but includes an EndShipper ID.
func (c *Client) BuyShipmentWithEndShipper(shipmentID string, rate *Rate, insurance string, endShipperID string) (out *Shipment, err error) {
return c.BuyShipmentWithEndShipperWithContext(context.Background(), shipmentID, rate, insurance, endShipperID)
}
// BuyShipmentWithEndShipperWithContext performs the same operation as BuyShipmentWithEndShipper, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyShipmentWithEndShipperWithContext(ctx context.Context, shipmentID string, rate *Rate, insurance string, endShipperID string) (out *Shipment, err error) {
req := &buyShipmentRequest{Rate: rate, Insurance: insurance, EndShipperID: endShipperID}
return c.buyShipment(ctx, shipmentID, req)
}
// BuyShipmentWithCarbonOffsetAndEndShipper performs the same operation as BuyShipment, but includes a carbon offset and an EndShipper ID.
func (c *Client) BuyShipmentWithCarbonOffsetAndEndShipper(shipmentID string, rate *Rate, insurance string, endShipperID string) (out *Shipment, err error) {
return c.BuyShipmentWithCarbonOffsetAndEndShipperWithContext(context.Background(), shipmentID, rate, insurance, endShipperID)
}
// BuyShipmentWithCarbonOffsetAndEndShipperWithContext performs the same operation as BuyShipmentWithCarbonOffsetAndEndShipper, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyShipmentWithCarbonOffsetAndEndShipperWithContext(ctx context.Context, shipmentID string, rate *Rate, insurance string, endShipperID string) (out *Shipment, err error) {
req := &buyShipmentRequest{Rate: rate, Insurance: insurance, CarbonOffset: true, EndShipperID: endShipperID}
return c.buyShipment(ctx, shipmentID, req)
}
// GetShipmentLabel enables retrieving the label for a shipment in a different
// format. The PostageLabel field in the returned Shipment object will reflect
// the new format.
func (c *Client) GetShipmentLabel(shipmentID, format string) (out *Shipment, err error) {
return c.GetShipmentLabelWithContext(context.Background(), shipmentID, format)
}
// GetShipmentLabelWithContext performs the same operation as GetShipmentLabel,
// but allows specifying a context that can interrupt the request.
func (c *Client) GetShipmentLabelWithContext(ctx context.Context, shipmentID, format string) (out *Shipment, err error) {
vals := url.Values{"file_format": []string{format}}
err = c.do(ctx, http.MethodGet, "shipments/"+shipmentID+"/label", vals, &out)
return
}
// GetShipmentSmartrates fetches the available smartrates for a shipment.
func (c *Client) GetShipmentSmartrates(shipmentID string) (out []*SmartRate, err error) {
return c.GetShipmentSmartratesWithContext(context.Background(), shipmentID)
}
// GetShipmentSmartratesWithContext performs the same operation as GetShipmentRates,
// but allows specifying a context that can interrupt the request.
func (c *Client) GetShipmentSmartratesWithContext(ctx context.Context, shipmentID string) (out []*SmartRate, err error) {
res := struct {
Smartrates *[]*SmartRate `json:"result,omitempty"`
}{Smartrates: &out}
err = c.get(ctx, "shipments/"+shipmentID+"/smartrate", &res)
return
}
// InsureShipment purchases insurance for the shipment. Insurance should be
// purchased after purchasing the shipment, but before it has been processed by
// the carrier. On success, the purchased insurance will be reflected in the
// returned Shipment object's Insurance field.
func (c *Client) InsureShipment(shipmentID, amount string) (out *Shipment, err error) {
return c.InsureShipmentWithContext(context.Background(), shipmentID, amount)
}
// InsureShipmentWithContext performs the same operation as InsureShipment, but
// allows specifying a context that can interrupt the request.
func (c *Client) InsureShipmentWithContext(ctx context.Context, shipmentID, amount string) (out *Shipment, err error) {
vals := url.Values{"amount": []string{amount}}
err = c.post(ctx, "shipments/"+shipmentID+"/insure", vals, &out)
return
}
// RefundShipment requests a refund from the carrier.
func (c *Client) RefundShipment(shipmentID string) (out *Shipment, err error) {
return c.RefundShipmentWithContext(context.Background(), shipmentID)
}
// RefundShipmentWithContext performs the same operation as RefundShipment, but
// allows specifying a context that can interrupt the request.
func (c *Client) RefundShipmentWithContext(ctx context.Context, shipmentID string) (out *Shipment, err error) {
err = c.post(ctx, "shipments/"+shipmentID+"/refund", nil, &out)
return
}
// RerateShipment fetches the available rates for a shipment with the current rates.
func (c *Client) RerateShipment(shipmentID string) (out []*Rate, err error) {
return c.RerateShipmentWithContext(context.Background(), shipmentID)
}
// RerateShipmentWithContext performs the same operation as RerateShipment,
// but allows specifying a context that can interrupt the request.
func (c *Client) RerateShipmentWithContext(ctx context.Context, shipmentID string) (out []*Rate, err error) {
req := &getShipmentRatesRequest{CarbonOffset: false}
res := &getShipmentRatesResponse{Rates: &out}
err = c.post(ctx, "shipments/"+shipmentID+"/rerate", &req, &res)
return
}
// RerateShipmentWithCarbonOffset performs the same operation as RerateShipment, but includes a carbon offset.
func (c *Client) RerateShipmentWithCarbonOffset(shipmentID string) (out []*Rate, err error) {
return c.RerateShipmentWithCarbonOffsetWithContext(context.Background(), shipmentID)
}
// RerateShipmentWithCarbonOffsetWithContext performs the same operation as RerateShipmentWithCarbonOffset, but allows
// specifying a context that can interrupt the request.
func (c *Client) RerateShipmentWithCarbonOffsetWithContext(ctx context.Context, shipmentID string) (out []*Rate, err error) {
req := &getShipmentRatesRequest{CarbonOffset: true}
res := &getShipmentRatesResponse{Rates: &out}
err = c.post(ctx, "shipments/"+shipmentID+"/rerate", &req, &res)
return
}
// Deprecated: Use LowestShipmentRate instead.
// LowestRate gets the lowest rate of a shipment
func (c *Client) LowestRate(shipment *Shipment) (out Rate, err error) {
return c.LowestShipmentRate(shipment)
}
// LowestShipmentRate gets the lowest rate of a shipment
func (c *Client) LowestShipmentRate(shipment *Shipment) (out Rate, err error) {
return c.LowestShipmentRateWithCarrier(shipment, nil)
}
// Deprecated: Use LowestShipmentRateWithCarrier instead.
// LowestRateWithCarrier performs the same operation as LowestRate,
// but allows specifying a list of carriers for the lowest rate
func (c *Client) LowestRateWithCarrier(shipment *Shipment, carriers []string) (out Rate, err error) {
return c.LowestShipmentRateWithCarrier(shipment, carriers)
}
// LowestShipmentRateWithCarrier performs the same operation as LowestShipmentRate,
// but allows specifying a list of carriers for the lowest rate
func (c *Client) LowestShipmentRateWithCarrier(shipment *Shipment, carriers []string) (out Rate, err error) {
return c.LowestShipmentRateWithCarrierAndService(shipment, carriers, nil)
}
// Deprecated: Use LowestShipmentRateWithCarrierAndService instead.
// LowestRateWithCarrierAndService performs the same operation as LowestRate,
// but allows specifying a list of carriers and service for the lowest rate
func (c *Client) LowestRateWithCarrierAndService(shipment *Shipment, carriers []string, services []string) (out Rate, err error) {
return c.LowestShipmentRateWithCarrierAndService(shipment, carriers, services)
}
// LowestShipmentRateWithCarrierAndService performs the same operation as LowestShipmentRate,
// but allows specifying a list of carriers and service for the lowest rate
func (c *Client) LowestShipmentRateWithCarrierAndService(shipment *Shipment, carriers []string, services []string) (out Rate, err error) {
return c.lowestObjectRate(shipment.Rates, carriers, services)
}
// LowestSmartrate gets the lowest smartrate of a shipment with the specified delivery days and accuracy
func (c *Client) LowestSmartrate(shipment *Shipment, deliveryDays int, deliveryAccuracy string) (out SmartRate, err error) {
smartrates, _ := c.GetShipmentSmartrates(shipment.ID)
return c.lowestSmartRate(smartrates, deliveryDays, deliveryAccuracy)
}
// GenerateShipmentForm generates a form of a given type for a shipment
func (c *Client) GenerateShipmentForm(shipmentID string, formType string) (out *Shipment, err error) {
return c.GenerateShipmentFormWithContext(context.Background(), shipmentID, formType)
}
// GenerateShipmentFormWithContext performs the same operation as GenerateShipmentForm,
// but allows specifying a context that can interrupt the request.
func (c *Client) GenerateShipmentFormWithContext(ctx context.Context, shipmentID string, formType string) (out *Shipment, err error) {
return c.GenerateShipmentFormWithOptionsWithContext(ctx, shipmentID, formType, make(map[string]interface{}))
}
// GenerateShipmentFormWithOptions generates a form of a given type for a shipment, using provided options
func (c *Client) GenerateShipmentFormWithOptions(shipmentID string, formType string, formOptions map[string]interface{}) (out *Shipment, err error) {
return c.GenerateShipmentFormWithOptionsWithContext(context.Background(), shipmentID, formType, formOptions)
}
// GenerateShipmentFormWithOptionsWithContext performs the same operation as GenerateShipmentFormWithOptions,
// but allows specifying a context that can interrupt the request.
func (c *Client) GenerateShipmentFormWithOptionsWithContext(ctx context.Context, shipmentID string, formType string, formOptions map[string]interface{}) (out *Shipment, err error) {
formOptions["type"] = formType
req := &generateFormRequest{Form: formOptions}
err = c.post(ctx, "shipments/"+shipmentID+"/forms", &req, &out)
return
}