This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathbatch.py
166 lines (153 loc) · 5.17 KB
/
batch.py
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
import shippo
import time
"""
In this tutorial we see how to use and interact with batches
"""
# Replace <API-KEY> with your key
shippo.config.api_key = "<API-KEY>"
example_batch = {
# replace with your carrier account
"default_carrier_account": "<CARRIER-ACCOUNT>",
# replace with desired service level, https://goshippo.com/docs/reference#servicelevels
"default_servicelevel_token": "<SERVICE-LEVEL-TOKEN>",
"label_filetype": "PDF_4x6",
"metadata": "this is metadata",
"batch_shipments": [
{
"shipment": { # see basic-shipment.py on how to create shipments
"address_from": {
"name": "Mr Hippo",
"street1": "965 Mission St",
"street2": "Ste 201",
"city": "San Francisco",
"state": "CA",
"zip": "94103",
"country": "US",
"phone": "4151234567",
},
"address_to": {
"name": "Mrs Hippo",
"company": "",
"street1": "Broadway 1",
"street2": "",
"city": "New York",
"state": "NY",
"zip": "10007",
"country": "US",
"phone": "4151234567",
},
"parcels": [{
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "2",
"mass_unit": "oz"
}]
}
},
{
"shipment": {
"address_from": {
"name": "Mr Hippo",
"street1": "1092 Indian Summer Ct",
"city": "San Jose",
"state": "CA",
"zip": "95122",
"country": "US",
"phone": "4151234567",
},
"address_to": {
"name": "Mrs Hippo",
"company": "",
"street1": "Broadway 1",
"street2": "",
"city": "New York",
"state": "NY",
"zip": "10007",
"country": "US",
"phone": "4151234567",
},
"parcels": [{
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "20",
"mass_unit": "lb"
}]
}
}
]
}
# create batch, passing in each attribute as a parameter
batch = shippo.Batch.create(**example_batch)
"""
The batch endpoint is async so we need to retrieve it in order to see the details
In this example we are long-polling but in practice you should use webhooks over
long-polling, we would encourage you to add a webhook through the UI
see https://app.goshippo.com/api/
"""
batch = shippo.Batch.retrieve(batch.object_id)
tries = 0
TIMEOUT = 60 # thirty second timeout
while batch.status == 'VALIDATING' and tries < TIMEOUT:
time.sleep(0.5)
batch = shippo.Batch.retrieve(batch.object_id)
tries += 1
print(batch)
# now we want to add a shipment to our batch
# create a sample shipment
address_from = {
"name": "Shippo Team",
"street1": "965 Mission St",
"street2": "Unit 480",
"city": "San Francisco",
"state": "CA",
"zip": "94103",
"country": "US",
"phone": "+1 555 341 9393",
}
address_to = {
"name": "Shippo Friend",
"street1": "1092 Indian Summer Ct",
"city": "San Jose",
"state": "CA",
"zip": "95122",
"country": "US",
"phone": "+1 555 341 9393",
}
parcel = {
"length": "5",
"width": "5",
"height": "5",
"distance_unit": "in",
"weight": "2",
"mass_unit": "lb",
}
shipment = shippo.Shipment.create(
address_from=address_from,
address_to=address_to,
parcels=[parcel],
asynchronous=False
)
# the post data should be in an array even if it's just one shipment
# each shipment object_id should be in an dictionary as shown below
added = shippo.Batch.add(batch.object_id, [{'shipment': shipment.object_id}])
print(added)
# now let's remove a shipment from our batch
# find the object_id of the shipment you want to remove, it will not be the same as the id you used to add it
batch = shippo.Batch.retrieve(batch.object_id)
to_remove = []
for batch_shipment in batch.batch_shipments.results:
# the shipment object_id is stored under the field 'shipment' in batch objects
if shipment.object_id == batch_shipment.shipment:
# but we used the batch object_id to remove it
to_remove.append(batch_shipment.object_id)
# the post data here is just an array of ids
removed = shippo.Batch.remove(batch.object_id, to_remove)
# now we're ready to purchase
purchase = shippo.Batch.purchase(batch.object_id)
print(purchase)
# For more tutorals of address validation, tracking, returns, refunds, and other functionality, check out our
# complete documentation: https://goshippo.com/docs/