-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrackings.go
164 lines (152 loc) · 8.53 KB
/
trackings.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
package aftership
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type DeliveryType string
type Tag string
const (
PickupAtStore DeliveryType = "pickup_at_store"
PickupAtCourier DeliveryType = "pickup_at_courier"
DoorToDoor DeliveryType = "door_to_door"
Pending Tag = "Pending"
InfoReceived Tag = "InfoReceived"
InTransit Tag = "InTransit"
OutForDelivery Tag = "OutForDelivery"
AttemptFail Tag = "AttemptFail"
Delivered Tag = "Delivered"
Exception Tag = "Exception"
Expired Tag = "Expired"
)
type Tracking struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
LastUpdatedAt string `json:"last_updated_at"`
Active bool `json:"active"`
TrackingNumber string `json:"tracking_number"`
UniqueToken string `json:"unique_token"`
Slug interface{} `json:"slug,omitempty"`
TrackingPostalCode string `json:"tracking_postal_code,omitempty"`
TrackingShipDate string `json:"tracking_ship_date,omitempty"`
TrackingAccountNumber string `json:"tracking_account_number,omitempty"`
TrackingKey string `json:"tracking_key,omitempty"`
TrackingOriginCountry string `json:"tracking_origin_country,omitempty"`
TrackingDestinationCountry string `json:"tracking_destination_country,omitempty"`
TrackingState string `json:"tracking_state,omitempty"`
Android interface{} `json:"android,omitempty"`
IOS interface{} `json:"ios,omitempty"`
EMails interface{} `json:"emails,omitempty"`
SubscribedEMails []string `json:"subscribed_emails,omitempty"`
SMSes interface{} `json:"smses,omitempty"`
SubscribedSMSes []string `json:"subscribed_smses,omitempty"`
Title string `json:"title,omitempty"`
CustomerName string `json:"customer_name,omitempty"`
DeliveryTime int `json:"delivery_time"`
ExpectedDelivery *string `json:"expected_delivery"`
OriginCountryISO3 string `json:"origin_country_iso3,omitempty" validate:"len=3"`
DestinationCountryISO3 string `json:"destination_country_iso3,omitempty" validate:"len=3"`
OrderID string `json:"order_id,omitempty"`
OrderIDPath string `json:"order_id_path,omitempty"`
CustomFields map[string]interface{} `json:"custom_fields,omitempty"`
Note string `json:"note,omitempty"`
Language string `json:"language,omitempty"`
OrderPromisedDeliveryDate string `json:"order_promised_delivery_date,omitempty"`
DeliveryType DeliveryType `json:"delivery_type,omitempty"`
PickupLocation string `json:"pickup_location,omitempty"`
PickupNote string `json:"pickup_note,omitempty"`
ShipmentPackageCount int `json:"shipment_package_count,omitempty"`
ShipmentType string `json:"shipment_type,omitempty"`
ShipmentWeight float64 `json:"shipment_weight,omitempty"`
ShipmentWeightUnit string `json:"shipment_weight_unit,omitempty"`
ShipmentPickupDate string `json:"shipment_pickup_date,omitempty"`
ShipmentDeliveryDate string `json:"shipment_delivery_date,omitempty"`
SignedBy string `json:"signed_by,omitempty"`
Source string `json:"source,omitempty"`
Tag Tag `json:"tag,omitempty"`
Subtag string `json:"subtag,omitempty"`
SubtagMessage string `json:"subtag_message,omitempty"`
TrackedCount int `json:"tracked_count,omitempty"`
LastMileTrackingSupported *bool `json:"last_mile_tracking_supported,omitempty"`
ReturnToSender bool `json:"return_to_sender,omitempty"`
Checkpoints []Checkpoint `json:"checkpoints"`
}
type Checkpoint struct {
CreatedAt time.Time `json:"created_at"`
Slug string `json:"slug"`
CheckpointTime string `json:"checkpoint_time,omitempty"`
Location string `json:"location,omitempty"`
City string `json:"city,omitempty"`
State string `json:"state,omitempty"`
CountryISO3 string `json:"country_iso3,omitempty"`
CountryName string `json:"country_name,omitempty"`
Zip string `json:"zip,omitempty"`
Tag Tag `json:"tag"`
Message string `json:"message"`
Subtag string `json:"subtag"`
SubtagMessage string `json:"subtag_message"`
}
type CreateTracking struct {
TrackingNumber string `json:"tracking_number" validate:"required"`
Slug interface{} `json:"slug,omitempty"`
TrackingPostalCode string `json:"tracking_postal_code,omitempty"`
TrackingShipDate string `json:"tracking_ship_date,omitempty"`
TrackingAccountNumber string `json:"tracking_account_number,omitempty"`
TrackingKey string `json:"tracking_key,omitempty"`
TrackingOriginCountry string `json:"tracking_origin_country,omitempty"`
TrackingDestinationCountry string `json:"tracking_destination_country,omitempty"`
TrackingState string `json:"tracking_state,omitempty"`
Android interface{} `json:"android,omitempty"`
IOS interface{} `json:"ios,omitempty"`
EMails interface{} `json:"emails,omitempty"`
SMSes interface{} `json:"smses,omitempty"`
Title string `json:"title,omitempty"`
CustomerName string `json:"customer_name,omitempty"`
OriginCountryISO3 string `json:"origin_country_iso3,omitempty" validate:"len=3"`
DestinationCountryISO3 string `json:"destination_country_iso3,omitempty" validate:"len=3"`
OrderID string `json:"order_id,omitempty"`
OrderIDPath string `json:"order_id_path,omitempty"`
CustomFields map[string]interface{} `json:"custom_fields,omitempty"`
Note string `json:"note,omitempty"`
Language string `json:"language,omitempty"`
OrderPromisedDeliveryDate string `json:"order_promised_delivery_date,omitempty"`
DeliveryType DeliveryType `json:"delivery_type,omitempty"`
PickupLocation string `json:"pickup_location,omitempty"`
PickupNote string `json:"pickup_note,omitempty"`
}
func (a *AfterShip) CreateTracking(ctx context.Context, create CreateTracking) (*Tracking, error) {
res, err := a.prepareAndSend(ctx, http.MethodPost, "/trackings", struct {
Tracking CreateTracking `json:"tracking"`
}{
Tracking: create,
})
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusCreated {
return nil, formatError(ErrUnexpectedResponseStatus, res)
}
var body struct {
Data struct {
Tracking Tracking `json:"tracking"`
} `json:"data"`
}
err = json.NewDecoder(res.Body).Decode(&body)
if err != nil {
return nil, err
}
return &body.Data.Tracking, nil
}
func (a *AfterShip) DeleteTracking(ctx context.Context, slug, trackingNumber string) error {
res, err := a.prepareAndSend(ctx, http.MethodDelete, fmt.Sprintf("/trackings/%s/%s", slug, trackingNumber), nil)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return formatError(ErrUnexpectedResponseStatus, res)
}
return nil
}