-
Notifications
You must be signed in to change notification settings - Fork 17
/
rate.go
134 lines (121 loc) · 8.65 KB
/
rate.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
package easypost
import (
"context"
"net/http"
)
// A Rate contains information on shipping cost and delivery time.
type Rate struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
Mode string `json:"mode,omitempty" url:"mode,omitempty"`
CreatedAt *DateTime `json:"created_at,omitempty" url:"created_at,omitempty"`
UpdatedAt *DateTime `json:"updated_at,omitempty" url:"updated_at,omitempty"`
Service string `json:"service,omitempty" url:"service,omitempty"`
Carrier string `json:"carrier,omitempty" url:"carrier,omitempty"`
CarrierAccountID string `json:"carrier_account_id,omitempty" url:"carrier_account_id,omitempty"`
ShipmentID string `json:"shipment_id,omitempty" url:"shipment_id,omitempty"`
Rate string `json:"rate,omitempty" url:"rate,omitempty"`
Currency string `json:"currency,omitempty" url:"currency,omitempty"`
RetailRate string `json:"retail_rate,omitempty" url:"retail_rate,omitempty"`
RetailCurrency string `json:"retail_currency,omitempty" url:"retail_currency,omitempty"`
ListRate string `json:"list_rate,omitempty" url:"list_rate,omitempty"`
ListCurrency string `json:"list_currency,omitempty" url:"list_currency,omitempty"`
DeliveryDays int `json:"delivery_days,omitempty" url:"delivery_days,omitempty"`
DeliveryDate *DateTime `json:"delivery_date,omitempty" url:"delivery_date,omitempty"`
DeliveryDateGuaranteed bool `json:"delivery_date_guaranteed,omitempty" url:"delivery_date_guaranteed,omitempty"`
EstDeliveryDays int `json:"est_delivery_days,omitempty" url:"est_delivery_days,omitempty"`
BillingType string `json:"billing_type,omitempty" url:"billing_type,omitempty"`
}
// A SmartRate contains information on shipping cost and delivery time in addition to time-in-transit details.
type SmartRate struct {
ID string `json:"id,omitempty" url:"id,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
Mode string `json:"mode,omitempty" url:"mode,omitempty"`
CreatedAt *DateTime `json:"created_at,omitempty" url:"created_at,omitempty"`
UpdatedAt *DateTime `json:"updated_at,omitempty" url:"updated_at,omitempty"`
Service string `json:"service,omitempty" url:"service,omitempty"`
Carrier string `json:"carrier,omitempty" url:"carrier,omitempty"`
CarrierAccountID string `json:"carrier_account_id,omitempty" url:"carrier_account_id,omitempty"`
ShipmentID string `json:"shipment_id,omitempty" url:"shipment_id,omitempty"`
Rate float64 `json:"rate,omitempty" url:"rate,omitempty"`
Currency string `json:"currency,omitempty" url:"currency,omitempty"`
RetailRate float64 `json:"retail_rate,omitempty" url:"retail_rate,omitempty"`
RetailCurrency string `json:"retail_currency,omitempty" url:"retail_currency,omitempty"`
ListRate float64 `json:"list_rate,omitempty" url:"list_rate,omitempty"`
ListCurrency string `json:"list_currency,omitempty" url:"list_currency,omitempty"`
DeliveryDays int `json:"delivery_days,omitempty" url:"delivery_days,omitempty"`
DeliveryDate *DateTime `json:"delivery_date,omitempty" url:"delivery_date,omitempty"`
DeliveryDateGuaranteed bool `json:"delivery_date_guaranteed,omitempty" url:"delivery_date_guaranteed,omitempty"`
EstDeliveryDays int `json:"est_delivery_days,omitempty" url:"est_delivery_days,omitempty"`
TimeInTransit *TimeInTransit `json:"time_in_transit,omitempty" url:"time_in_transit,omitempty"`
BillingType string `json:"billing_type,omitempty" url:"billing_type,omitempty"`
}
// A StatelessRate contains information on shipping cost and delivery time, but does not have an ID (is ephemeral).
type StatelessRate struct {
BillingType string `json:"billing_type,omitempty" url:"billing_type,omitempty"`
Carrier string `json:"carrier,omitempty" url:"carrier,omitempty"`
CarrierAccountID string `json:"carrier_account_id,omitempty" url:"carrier_account_id,omitempty"`
Currency string `json:"currency,omitempty" url:"currency,omitempty"`
DeliveryDate *DateTime `json:"delivery_date,omitempty" url:"delivery_date,omitempty"`
DeliveryDateGuaranteed bool `json:"delivery_date_guaranteed,omitempty" url:"delivery_date_guaranteed,omitempty"`
DeliveryDays int `json:"delivery_days,omitempty" url:"delivery_days,omitempty"`
EstDeliveryDays int `json:"est_delivery_days,omitempty" url:"est_delivery_days,omitempty"`
ListCurrency string `json:"list_currency,omitempty" url:"list_currency,omitempty"`
ListRate string `json:"list_rate,omitempty" url:"list_rate,omitempty"`
Mode string `json:"mode,omitempty" url:"mode,omitempty"`
Object string `json:"object,omitempty" url:"object,omitempty"`
Rate string `json:"rate,omitempty" url:"rate,omitempty"`
RetailCurrency string `json:"retail_currency,omitempty" url:"retail_currency,omitempty"`
RetailRate string `json:"retail_rate,omitempty" url:"retail_rate,omitempty"`
Service string `json:"service,omitempty" url:"service,omitempty"`
ShipmentID string `json:"shipment_id,omitempty" url:"shipment_id,omitempty"`
}
// TimeInTransit provides details on the probability your package will arrive within a certain number of days
type TimeInTransit struct {
Percentile50 int `json:"percentile_50,omitempty" url:"percentile_50,omitempty"`
Percentile75 int `json:"percentile_75,omitempty" url:"percentile_75,omitempty"`
Percentile85 int `json:"percentile_85,omitempty" url:"percentile_85,omitempty"`
Percentile90 int `json:"percentile_90,omitempty" url:"percentile_90,omitempty"`
Percentile95 int `json:"percentile_95,omitempty" url:"percentile_95,omitempty"`
Percentile97 int `json:"percentile_97,omitempty" url:"percentile_97,omitempty"`
Percentile99 int `json:"percentile_99,omitempty" url:"percentile_99,omitempty"`
}
type getStatelessRatesResponse struct {
Rates *[]*StatelessRate `json:"rates,omitempty" url:"rates,omitempty"`
}
// GetRate retrieves a previously-created rate by its ID.
func (c *Client) GetRate(rateID string) (out *Rate, err error) {
return c.GetRateWithContext(context.Background(), rateID)
}
// GetRateWithContext performs the same operation as GetRate, but allows
// specifying a context that can interrupt the request.
func (c *Client) GetRateWithContext(ctx context.Context, rateID string) (out *Rate, err error) {
err = c.do(ctx, http.MethodGet, "rates/"+rateID, nil, &out)
return
}
// BetaGetStatelessRates fetches a list of stateless rates.
func (c *Client) BetaGetStatelessRates(in *Shipment) (out []*StatelessRate, err error) {
return c.BetaGetStatelessRatesWithContext(context.Background(), in)
}
// BetaGetStatelessRatesWithContext performs the same operation as BetaGetStatelessRates,
// but allows specifying a context that can interrupt the request.
func (c *Client) BetaGetStatelessRatesWithContext(ctx context.Context, in *Shipment) (out []*StatelessRate, err error) {
req := &createShipmentRequest{Shipment: in}
res := &getStatelessRatesResponse{Rates: &out}
err = c.do(ctx, http.MethodPost, "/beta/rates", &req, &res)
return
}
// LowestStatelessRate gets the lowest stateless rate from a list of stateless rates
func (c *Client) LowestStatelessRate(rates []*StatelessRate) (out StatelessRate, err error) {
return c.LowestStatelessRateWithCarrier(rates, nil)
}
// LowestStatelessRateWithCarrier performs the same operation as LowestStatelessRate,
// but allows specifying a list of carriers for the lowest rate
func (c *Client) LowestStatelessRateWithCarrier(rates []*StatelessRate, carriers []string) (out StatelessRate, err error) {
return c.LowestStatelessRateWithCarrierAndService(rates, carriers, nil)
}
// LowestStatelessRateWithCarrierAndService performs the same operation as LowestStatelessRate,
// but allows specifying a list of carriers and service for the lowest rate
func (c *Client) LowestStatelessRateWithCarrierAndService(rates []*StatelessRate, carriers []string, services []string) (out StatelessRate, err error) {
return c.lowestStatelessRate(rates, carriers, services)
}