Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Commit cc9feff

Browse files
Add pickup and orders, convert to circle ci (#75)
* Add pickup and orders, convert to circle ci * Update order-lineitem.py * Update HISTORY.rst * Update config.yml * Remove key
1 parent 0190146 commit cc9feff

23 files changed

+1426
-31
lines changed

.circleci/config.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
orbs: # declare what orbs we are going to use
2+
node: circleci/[email protected]
3+
4+
# Use the latest 2.1 version of CircleCI pipeline process engine.
5+
# See: https://circleci.com/docs/2.0/configuration-reference
6+
7+
version: 2.1
8+
9+
jobs:
10+
build:
11+
working_directory: ~/repo
12+
13+
# Primary container image where all commands run
14+
15+
docker:
16+
- image: circleci/python:3.5.7 # primary container for the build job
17+
auth:
18+
username: mydockerhub-user
19+
password: $DOCKERHUB_PASSWORD
20+
21+
steps:
22+
- checkout
23+
- run:
24+
name: Install Dependencies
25+
command: | # use pipenv to install dependencies
26+
sudo pip install pipenv
27+
pipenv install
28+
29+
- run:
30+
name: Install goShippo
31+
command: pipenv run python setup.py install
32+
- run:
33+
name: Test
34+
command: pipenv run python -W always setup.py test

.travis.yml

-9
This file was deleted.

HISTORY.rst

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
Release History
22
---------------
33

4+
2.1.0 (2022-01-18)
5+
6+
**New features**
7+
8+
- Added support for Pickup and Order Objects
9+
- Converted testing to CircleCi
10+
11+
Release History
12+
---------------
13+
414
2.0.2 (2020-11-05)
515

616
**Dependency Version Bumps**

README.md

-10
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,6 @@ Run the test with the following command:
5555
python setup.py test --test-suite=shippo
5656
```
5757

58-
#### Dependencies:
59-
60-
##### requests & mock
61-
62-
```
63-
#!shell
64-
sudo easy_install requests
65-
sudo easy_install mock
66-
```
67-
6858
#### Using the API:
6959

7060
```python

examples/basic-shipment.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
shippo.config.api_key = "<API-KEY>"
1010

1111
# Example address_from object dict
12-
# The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
12+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
1313
address_from = {
1414
"name": "Shippo Team",
1515
"street1": "965 Mission St",
@@ -22,7 +22,7 @@
2222
}
2323

2424
# Example address_to object dict
25-
# The complete refence for the address object is available here: https://goshippo.com/docs/reference#addresses
25+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
2626

2727
address_to = {
2828
"name": "Shippo Friend",

examples/batch.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@
150150
# 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
151151
batch = shippo.Batch.retrieve(batch.object_id)
152152
to_remove = []
153-
for shipment in batch.batch_shipments.results:
153+
for batch_shipment in batch.batch_shipments.results:
154154
# the shipment object_id is stored under the field 'shipment' in batch objects
155-
if shipment.object_id == shipment.shipment:
155+
if shipment.object_id == batch_shipment.shipment:
156156
# but we used the batch object_id to remove it
157-
to_remove.append(shipment.object_id)
157+
to_remove.append(batch_shipment.object_id)
158158
# the post data here is just an array of ids
159159
removed = shippo.Batch.remove(batch.object_id, to_remove)
160160

examples/international-shipment.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"country": "US",
2828
"phone": "+15553419393",
2929
"email": "[email protected]",
30-
},
30+
}
3131

3232
# Example address_to object dict
3333
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
@@ -43,7 +43,7 @@
4343
"phone": "+1 555 341 9393",
4444
"email": "[email protected]",
4545
"metadata": "For Order Number 123"
46-
},
46+
}
4747

4848
# parcel object dict
4949
# The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels

examples/order-lineitem.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from datetime import datetime
2+
3+
import shippo
4+
5+
"""
6+
In this example, we are creating (and consuming) an order object with lineitem(s).
7+
"""
8+
9+
# Replace <API-KEY> with your key
10+
shippo.config.api_key = "<API-KEY>"
11+
12+
# Example address_from object dict
13+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
14+
address_from = {
15+
"name": "Shippo Team",
16+
"street1": "965 Mission St",
17+
"street2": "Unit 480",
18+
"city": "San Francisco",
19+
"state": "CA",
20+
"zip": "94103",
21+
"country": "US",
22+
"phone": "+1 555 341 9393",
23+
}
24+
25+
# Example address_to object dict
26+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
27+
28+
address_to = {
29+
"name": "Shippo Friend",
30+
"street1": "1092 Indian Summer Ct",
31+
"city": "San Jose",
32+
"state": "CA",
33+
"zip": "95122",
34+
"country": "US",
35+
"phone": "+1 555 341 9393",
36+
}
37+
38+
unit_price = 2.34
39+
unit_weight = 25.45
40+
unit_quantity = 2
41+
line_item1 = {
42+
"title": "Demo Line Item Object",
43+
"sku": "demo_1234",
44+
"quantity": unit_quantity,
45+
"total_price": f"{unit_price:.2f}",
46+
"currency": "USD",
47+
"weight": f"{unit_weight:.2f}",
48+
"weight_unit": "lb",
49+
"manufacture_country": "US"
50+
}
51+
line_items = [line_item1]
52+
53+
shipping_cost = 1.23
54+
subtotal_cost = unit_price
55+
tax_cost = 1.065*subtotal_cost
56+
total_cost = shipping_cost + subtotal_cost + tax_cost
57+
my_order = {
58+
"order_number": f"#{datetime.now().date()}",
59+
"order_status": "PAID",
60+
"to_address": address_to,
61+
"from_address": address_from,
62+
"line_items": line_items,
63+
"placed_at": datetime.now().isoformat(),
64+
"weight": f"{10.0:.2f}",
65+
"weight_unit": "lb",
66+
"shipping_method": "ground",
67+
"shipping_cost": f"{shipping_cost:.2f}",
68+
"shipping_cost_currency": "USD",
69+
"subtotal_price": f"{subtotal_cost:.2f}",
70+
"total_price": f"{total_cost:.2f}",
71+
"total_tax": f"{tax_cost:.2f}",
72+
"currency": "USD"
73+
}
74+
75+
order = shippo.Order.create(order_number=123,
76+
order_status="PAID",
77+
to_address=address_to,
78+
from_address=address_from,
79+
line_items=[line_item1],
80+
placed_at=datetime.now().isoformat(),
81+
weight=unit_weight*unit_quantity,
82+
weight_unit="lb")
83+
print(order)

examples/pickup.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import shippo
2+
from datetime import datetime, timedelta
3+
4+
"""
5+
In this tutorial we have an order with a sender address,
6+
recipient address and parcel. The shipment is going from the
7+
United States to an international location.
8+
9+
In addition to that we know that the customer expects the
10+
shipment to arrive within 3 days. We now want to purchase
11+
the cheapest shipping label with a transit time <= 3 days.
12+
"""
13+
14+
# replace <API-KEY> with your key
15+
shippo.config.api_key = "<API-KEY>"
16+
17+
# Example address_from object dict
18+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
19+
address_from = {
20+
"company": "",
21+
"street_no": "",
22+
"name": "Shippo Friend",
23+
"street1": "1092 Indian Summer Ct",
24+
"city": "San Jose",
25+
"state": "CA",
26+
"zip": "95122",
27+
"country": "US",
28+
"phone": "+1 555 341 9393",
29+
"email": "[email protected]"
30+
}
31+
32+
# Example address_to object dict
33+
# The complete reference for the address object is available here: https://goshippo.com/docs/reference#addresses
34+
35+
address_to_international = {
36+
"name": "Mrs. Hippo",
37+
"street1": "200 University Ave W",
38+
"street2": "",
39+
"city": "Waterloo",
40+
"state": "ON",
41+
"zip": "N2L 3G1",
42+
"country": "CA",
43+
"phone": "+1 555 341 9393",
44+
"email": "[email protected]",
45+
"metadata": "For Order Number 123"
46+
}
47+
48+
# parcel object dict
49+
# The complete reference for parcel object is here: https://goshippo.com/docs/reference#parcels
50+
parcel = {
51+
"length": "5",
52+
"width": "5",
53+
"height": "5",
54+
"distance_unit": "in",
55+
"weight": "2",
56+
"mass_unit": "lb"
57+
}
58+
59+
# Example CustomsItems object.
60+
# The complete reference for customs object is here: https://goshippo.com/docs/reference#customsitems
61+
customs_item = {
62+
"description": "T-Shirt",
63+
"quantity": 2,
64+
"net_weight": "400",
65+
"mass_unit": "g",
66+
"value_amount": "20",
67+
"value_currency": "USD",
68+
"origin_country": "US",
69+
"tariff_number": ""
70+
}
71+
72+
# Creating the CustomsDeclaration
73+
# The details on creating the CustomsDeclaration is here: https://goshippo.com/docs/reference#customsdeclarations
74+
customs_declaration = shippo.CustomsDeclaration.create(
75+
contents_type='MERCHANDISE',
76+
contents_explanation='T-Shirt purchase',
77+
non_delivery_option='RETURN',
78+
certify=True,
79+
certify_signer='Mr Hippo',
80+
items=[customs_item])
81+
82+
# Creating the shipment object. asynchronous=False indicates that the function will wait until all
83+
# rates are generated before it returns.
84+
85+
# The reference for the shipment object is here: https://goshippo.com/docs/reference#shipments
86+
# By default Shippo API operates on an async basis. You can read about our async flow here: https://goshippo.com/docs/async
87+
shipment_international = shippo.Shipment.create(
88+
address_from=address_from,
89+
address_to=address_to_international,
90+
parcels=[parcel],
91+
customs_declaration=customs_declaration.object_id,
92+
asynchronous=False)
93+
94+
# Get the first usps or dhl express rate.
95+
# The details on the returned object are here: https://goshippo.com/docs/reference#rates
96+
filtered_rates = []
97+
for rate in shipment_international.rates:
98+
if rate.provider.lower() == "usps" or rate.provider.lower() == "dhl_express":
99+
filtered_rates.append(rate)
100+
# return strtolower($rate['provider']) == 'usps' Or strtolower($rate['provider']) == 'dhl_express';
101+
102+
rate_international = filtered_rates[0]
103+
selected_rate_carrier_account = rate_international.carrier_account
104+
105+
# Purchase the desired rate.
106+
# The complete information about purchasing the label: https://goshippo.com/docs/reference#transaction-create
107+
transaction_international = shippo.Transaction.create(
108+
rate=rate_international.object_id, asynchronous=False)
109+
110+
if transaction_international.status != "SUCCESS":
111+
print("Failed purchasing the label due to:")
112+
for message in transaction_international.messages:
113+
print("- %s" % message['text'])
114+
115+
# $pickupTimeStart = date('Y-m-d H:i:s', time());
116+
# $pickupTimeEnd = date('Y-m-d H:i:s', time() + 60*60*24);
117+
pickupTimeStart = datetime.now() + timedelta(hours=1)
118+
pickupTimeEnd = pickupTimeStart + timedelta(days=1)
119+
120+
# Schedule the pickup
121+
# Only 1 pickup can be scheduled in a day
122+
try:
123+
pickup = shippo.Pickup.create(
124+
carrier_account= selected_rate_carrier_account,
125+
location= {
126+
"building_location_type" : "Knock on Door",
127+
"address" : address_from,
128+
},
129+
transactions= [transaction_international.object_id],
130+
requested_start_time= pickupTimeStart.isoformat() + "Z",
131+
requested_end_time= pickupTimeEnd.isoformat() + "Z",
132+
is_test= False
133+
)
134+
except shippo.error.InvalidRequestError as err:
135+
print("A pickup has already been scheduled for today.")
136+
else:
137+
if pickup.status == "SUCCESS":
138+
print("Pickup has been scheduled")
139+
else:
140+
print("Failed scheduling a pickup:")
141+
for message in pickup.messages:
142+
print("- %s" % message['text'])
143+
144+
# For more tutorials of address validation, tracking, returns, refunds, and other functionality, check out our
145+
# complete documentation: https://goshippo.com/docs/

shippo/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
CustomsDeclaration,
66
CustomsItem,
77
Manifest,
8+
Order,
89
Parcel,
10+
Pickup,
911
Rate,
1012
Refund,
1113
Shipment,

0 commit comments

Comments
 (0)