forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorder.go
More file actions
126 lines (115 loc) · 4.43 KB
/
order.go
File metadata and controls
126 lines (115 loc) · 4.43 KB
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
package easypost
import (
"context"
"net/url"
"time"
)
// An Order object represents a collection of packages and can be used for
// multi-piece Shipments.
type Order 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"`
Shipments []*Shipment `json:"shipments,omitempty"`
Rates []*Rate `json:"rates,omitempty"`
Messages []*CarrierMessage `json:"messages,omitempty"`
IsReturn bool `json:"is_return"`
Service string `json:"service,omitempty"`
}
type createOrderRequest struct {
Order struct {
*Order
CarrierAccounts []*CarrierAccount `json:"carrier_accounts,omitempty"`
} `json:"order,omitempty"`
}
// CreateOrder creates a new order object. If the accounts parameter is given,
// the provided carrier accounts will be used to limit the returned rates to
// the given carrier(s).
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateOrder(
// &easypost.Order{
// ToAddress: &easypost.Address{ID: "adr_1001"},
// FromAddress: &easypost.Address{Id: "adr_101"},
// Shipments: []*easypost.Shipment{
// &easypost.Shipment{
// Parcel: &easypost.Parcel{
// PredefinedPackage: "FedExBox",
// Weight: 10.2,
// },
// },
// &easypost.Shipment{
// Parcel: &easypost.Parcel{
// PredefinedPackage: "FedExBox",
// Weight: 17.5,
// },
// },
// },
// },
// &easypost.CarrierAccount{ID: "ca_101"},
// &easypost.CarrierAccount{ID: "ca_102"},
// )
func (c *Client) CreateOrder(in *Order, accounts ...*CarrierAccount) (out *Order, err error) {
var req createOrderRequest
req.Order.Order, req.Order.CarrierAccounts = in, accounts
err = c.post(nil, "orders", &req, &out)
return
}
// CreateOrderWithContext performs the same operation as CreateOrder, but allows
// specifying a context that can interrupt the request.
func (c *Client) CreateOrderWithContext(ctx context.Context, in *Order, accounts ...*CarrierAccount) (out *Order, err error) {
var req createOrderRequest
req.Order.Order, req.Order.CarrierAccounts = in, accounts
err = c.post(ctx, "orders", &req, &out)
return
}
// GetOrder retrieves an existing Order object by ID.
func (c *Client) GetOrder(orderID string) (out *Order, err error) {
err = c.get(nil, "orders/"+orderID, &out)
return
}
// GetOrderWithContext performs the same operation as GetOrder, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetOrderWithContext(ctx context.Context, orderID string) (out *Order, err error) {
err = c.get(ctx, "orders/"+orderID, &out)
return
}
// GetOrderRates refreshes rates for an Order.
func (c *Client) GetOrderRates(orderID string) (out *Order, err error) {
err = c.get(nil, "orders/"+orderID+"/rates", &out)
return
}
// GetOrderRatesWithContext performs the same operation as GetOrderRates, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetOrderRatesWithContext(ctx context.Context, orderID string) (out *Order, err error) {
err = c.get(ctx, "orders/"+orderID+"/rates", &out)
return
}
// BuyOrder purchases an order. This operation populates the TrackingCode and
// PostageLabel attributes of each Shipment.
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.Buy("order_1", "FedEx", "FEDEX_GROUND")
func (c *Client) BuyOrder(orderID, carrier, service string) (out *Order, err error) {
vals := url.Values{
"carrier": []string{carrier},
"service": []string{service},
}
err = c.post(nil, "orders/"+orderID+"/buy", vals, &out)
return
}
// BuyOrderWithContext performs the same operation as GBuyOrder, but allows
// specifying a context that can interrupt the request.
func (c *Client) BuyOrderWithContext(ctx context.Context, orderID, carrier, service string) (out *Order, err error) {
vals := url.Values{
"carrier": []string{carrier},
"service": []string{service},
}
err = c.post(ctx, "orders/"+orderID+"/buy", vals, &out)
return
}