forked from EasyPost/easypost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
insurance.go
95 lines (85 loc) · 3.94 KB
/
insurance.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
package easypost
import (
"context"
"net/http"
"time"
)
// An Insurance object represents insurance for packages purchased both via the
// EasyPost API and shipments purchased through third parties and later
// registered with EasyPost.
type Insurance 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"`
Amount string `json:"amount,omitempty"`
Carrier string `json:"carrier,omitempty"`
Provider string `json:"provider,omitempty"`
ProviderID string `json:"provider_id,omitempty"`
ShipmentID string `json:"shipment_id,omitempty"`
TrackingCode string `json:"tracking_code,omitempty"`
Status string `json:"status,omitempty"`
Tracker *Tracker `json:"tracker,omitempty"`
ToAddress *Address `json:"to_address,omitempty"`
FromAddress *Address `json:"from_address,omitempty"`
Fee *Fee `json:"fee,omitempty"`
Messages []string `json:"messages,omitempty"`
}
type createInsuranceRequest struct {
Insurance *Insurance `json:"insurance,omitempty"`
}
// ListInsurancesResult holds the results from the list insurances API.
type ListInsurancesResult struct {
Insurances []*Insurance `json:"insurances,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"`
}
// CreateInsurance creates an insurance object for a shipment purchased outside
// EasyPost. ToAddress, FromAddress, TrackingCode and Amount fields must be
// provided. Providing a value in the Carrier field is optional, but can help
// avoid ambiguity and provide a shorter response time.
// c := easypost.New(MyEasyPostAPIKey)
// out, err := c.CreateInsurance(
// &easypost.Insurance{
// ToAddress: &easypost.Address{ID: "adr_102"},
// FromAddress: &easypost.Address{ID: "adr_101"},
// TrackingCode: "9400110898825022579493",
// Carrier: "USPS",
// Reference: "insuranceRef1",
// Amount: 100,
// )
func (c *Client) CreateInsurance(in *Insurance) (out *Insurance, err error) {
return c.CreateInsuranceWithContext(context.Background(), in)
}
// CreateInsuranceWithContext performs the same operation as CreateInsurance,
// but allows specifying a context that can interrupt the request.
func (c *Client) CreateInsuranceWithContext(ctx context.Context, in *Insurance) (out *Insurance, err error) {
req := &createInsuranceRequest{Insurance: in}
err = c.post(ctx, "insurances", req, &out)
return
}
// ListInsurances provides a paginated result of Insurance objects.
func (c *Client) ListInsurances(opts *ListOptions) (out *ListInsurancesResult, err error) {
return c.ListInsurancesWithContext(context.Background(), opts)
}
// ListInsurancesWithContext performs the same operation as ListInsurances, but
// allows specifying a context that can interrupt the request.
func (c *Client) ListInsurancesWithContext(ctx context.Context, opts *ListOptions) (out *ListInsurancesResult, err error) {
err = c.do(ctx, http.MethodGet, "insurances", c.convertOptsToURLValues(opts), &out)
return
}
// GetInsurance returns the Insurance object with the given ID or reference.
func (c *Client) GetInsurance(insuranceID string) (out *Insurance, err error) {
return c.GetInsuranceWithContext(context.Background(), insuranceID)
}
// GetInsuranceWithContext performs the same operation as GetInsurance, but
// allows specifying a context that can interrupt the request.
func (c *Client) GetInsuranceWithContext(ctx context.Context, insuranceID string) (out *Insurance, err error) {
err = c.get(ctx, "insurances/"+insuranceID, &out)
return
}