forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
191 lines (162 loc) · 7.58 KB
/
batch.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
package easypost
import (
"context"
"net/http"
"net/url"
"time"
)
// BatchStatus contains counts of statuses for the shipments in a batch.
type BatchStatus struct {
PostagePurchased int `json:"postage_purchased,omitempty"`
PostagePurchaseFailed int `json:"postage_purchase_failed,omitempty"`
QueuedForPurchase int `json:"queued_for_purchase,omitempty"`
CreationFailed int `json:"creation_failed,omitempty"`
}
// Batch represents a batch of shipments.
type Batch 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"`
State string `json:"state,omitempty"`
NumShipments int `json:"num_shipments,omitempty"`
Shipments []*Shipment `json:"shipments,omitempty"`
Status *BatchStatus `json:"status,omitempty"`
LabelURL string `json:"label_url,omitempty"`
ScanForm *ScanForm `json:"scan_form,omitempty"`
Pickup *Pickup `json:"pickup,omitempty"`
}
type batchRequest struct {
Batch *Batch `json:"batch,omitempty"`
}
// ListBatchesResult holds the results from the list insurances API.
type ListBatchesResult struct {
Batch []*Batch `json:"batches,omitempty"`
// HasMore indicates if there are more responses to be fetched. If True,
// additional responses can be fetched by updating the ListInsurancesOptions
// parameter's AfterID field with the ID of the last item in this object's
// Insurances field.
HasMore bool `json:"has_more,omitempty"`
}
// CreateBatch creates a new batch of shipments. It optionally accepts one or
// more shipments to add to the new batch. If successful, a new batch object is
// returned.
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateBatch(
// &easypost.Shipment{ID: "shp_100"},
// &easypost.Shipment{ID: "shp_101"},
// &easypost.Shipment{ID: "shp_102"},
// )
func (c *Client) CreateBatch(in ...*Shipment) (out *Batch, err error) {
return c.CreateBatchWithContext(context.Background(), in ...)
}
// CreateBatchWithContext performs the same operation as CreateBatch, but allows
// specifying a context that can interrupt the request.
func (c *Client) CreateBatchWithContext(ctx context.Context, in ...*Shipment) (out *Batch, err error) {
req := batchRequest{Batch: &Batch{Shipments: in}}
err = c.post(ctx, "batches", req, &out)
return
}
// CreateAndBuyBatch creates and buys a new batch of shipments in one request.
func (c *Client) CreateAndBuyBatch(in ...*Shipment) (out *Batch, err error) {
return c.CreateAndBuyBatchWithContext(context.Background(), in ...)
}
// CreateAndBuyBatchWithContext performs the same operation as
// CreateAndBuyBatch, but allows specifying a context that can interrupt the
// request.
func (c *Client) CreateAndBuyBatchWithContext(ctx context.Context, in ...*Shipment) (out *Batch, err error) {
req := batchRequest{Batch: &Batch{Shipments: in}}
err = c.post(ctx, "batches/create_and_buy", req, &out)
return
}
// ListBatches provides a paginated result of Insurance objects.
func (c *Client) ListBatches(opts *ListOptions) (out *ListBatchesResult, err error) {
return c.ListBatchesWithContext(context.Background(), opts)
}
// ListBatchesWithContext performs the same operation as ListBatches, but
// allows specifying a context that can interrupt the request.
func (c *Client) ListBatchesWithContext(ctx context.Context, opts *ListOptions) (out *ListBatchesResult, err error) {
err = c.do(ctx, http.MethodGet, "batches", c.convertOptsToURLValues(opts), &out)
return
}
// AddShipmentsToBatch adds shipments to an existing batch, and returns the
// updated batch object.
func (c *Client) AddShipmentsToBatch(batchID string, shipments ...interface{}) (out *Batch, err error) {
return c.AddShipmentsToBatchWithContext(context.Background(), batchID, shipments ...)
}
// AddShipmentsToBatchWithContext performs the same operation as
// AddShipmentsToBatch, but allows specifying a context that can interrupt the
// request.
func (c *Client) AddShipmentsToBatchWithContext(ctx context.Context, batchID string, shipments ...interface{}) (out *Batch, err error) {
params := make(map[int]interface{})
for i, shipment := range shipments {
params[i] = shipment
}
req := map[string]interface{}{"shipments": params}
err = c.post(ctx, "batches/"+batchID+"/add_shipments", req, &out)
return
}
// RemoveShipmentsFromBatch removes shipments from an existing batch, and
// returns the updated batch object.
func (c *Client) RemoveShipmentsFromBatch(batchID string, shipments ...interface{}) (out *Batch, err error) {
return c.RemoveShipmentsFromBatchWithContext(context.Background(), batchID, shipments ...)
}
// RemoveShipmentsFromBatchWithContext performs the same operation as
// RemoveShipmentsFromBatch, but allows specifying a context that can interrupt
// the request.
func (c *Client) RemoveShipmentsFromBatchWithContext(ctx context.Context, batchID string, shipments ...interface{}) (out *Batch, err error) {
params := make(map[int]interface{})
for i, shipment := range shipments {
params[i] = shipment
}
req := map[string]interface{}{"shipments": params}
err = c.post(ctx, "batches/"+batchID+"/remove_shipments", req, &out)
return
}
// BuyBatch initializes purchases for the shipments in the batch. The updated
// batch object is returned.
func (c *Client) BuyBatch(batchID string) (out *Batch, err error) {
return c.BuyBatchWithContext(context.Background(), batchID)
}
// BuyBatchWithContext performs the same operation as BuyBatch, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyBatchWithContext(ctx context.Context, batchID string) (out *Batch, err error) {
err = c.post(ctx, "batches/"+batchID+"/buy", nil, &out)
return
}
// GetBatch retrieves a Batch object by ID.
func (c *Client) GetBatch(batchID string) (out *Batch, err error) {
return c.GetBatchWithContext(context.Background(), batchID)
}
// GetBatchWithContext performs the same operation as GetBatch, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetBatchWithContext(ctx context.Context, batchID string) (out *Batch, err error) {
err = c.get(ctx, "batches/"+batchID, &out)
return
}
// GetBatchLabels generates a label for the batch. This can only be done once
// per batch, and all shipments must have a "postage_purchased" status.
func (c *Client) GetBatchLabels(batchID, format string) (out *Batch, err error) {
return c.GetBatchLabelsWithContext(context.Background(), batchID, format)
}
// GetBatchLabelsWithContext performs the same operation as GetBatchLabels, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetBatchLabelsWithContext(ctx context.Context, batchID, format string) (out *Batch, err error) {
params := url.Values{"file_format": []string{format}}
err = c.post(ctx, "batches/"+batchID+"/label", params, &out)
return
}
// CreateBatchScanForms generates a scan form for the batch.
func (c *Client) CreateBatchScanForms(batchID, format string) (out *Batch, err error) {
return c.CreateBatchScanFormsWithContext(context.Background(), batchID, format)
}
// CreateBatchScanFormsWithContext performs the same operation as
// CreateBatchScanForms, but allows specifying a context that can interrupt the
// request.
func (c *Client) CreateBatchScanFormsWithContext(ctx context.Context, batchID, format string) (out *Batch, err error) {
vals := url.Values{"file_format": []string{format}}
err = c.do(ctx, http.MethodPost, "batches/"+batchID+"/scan_form", vals, &out)
return
}