Skip to content

Commit 4264acb

Browse files
author
Jake Epstein
committed
added basic CRUD methods for Webhook objects
1 parent f3820ea commit 4264acb

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
=== 3.5.0 2017-01-18
2+
3+
* Added basic CRUD methods for Webhook Objects
4+
* Fixed Order test
5+
6+
17
=== 3.4.0 2016-12-20
28

39
* Added session pooling

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.4.0
1+
3.5.0

easypost/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None):
8282
'Report': Report,
8383
'ShipmentReport': Report,
8484
'PaymentLogReport': Report,
85-
'TrackerReport': Report
85+
'TrackerReport': Report,
86+
'Webhook': Webhook
8687
}
8788

8889
prefixes = {
@@ -106,7 +107,8 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None):
106107
'user': User,
107108
'shprep': Report,
108109
'plrep': Report,
109-
'trkrep': Report
110+
'trkrep': Report,
111+
'hook': Webhook
110112
}
111113

112114
if isinstance(response, list):
@@ -984,3 +986,12 @@ def retrieve(cls, easypost_id, api_key=None, **params):
984986
url = "%s/%s" % (cls.class_url(), easypost_id)
985987
response, api_key = requestor.request('get', url)
986988
return response["signed_url"]
989+
990+
991+
class Webhook(AllResource, CreateResource, DeleteResource):
992+
def update(self, **params):
993+
requestor = Requestor(self.api_key)
994+
url = self.instance_url()
995+
response, api_key = requestor.request('put', url, params)
996+
self.refresh_from(response, api_key)
997+
return self

easypost/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '3.4.0'
1+
VERSION = '3.5.0'

tests/order.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ def test_order_create_then_buy(self):
6464

6565
# Assert the shipment's parcel
6666
assert len(order.shipments) == 2
67-
assert order.shipments[0].parcel.height == parcel1['height']
68-
assert order.shipments[0].parcel.length == parcel1['length']
69-
assert order.shipments[0].parcel.width == parcel1['width']
70-
assert order.shipments[0].parcel.weight == parcel1['weight']
71-
assert order.shipments[1].parcel.height == 5.0
72-
assert order.shipments[1].parcel.length == 8.0
73-
assert order.shipments[1].parcel.width == 5.0
74-
assert order.shipments[1].parcel.weight == 16.0
67+
68+
assert order.shipments[1].parcel.height == parcel1['height']
69+
assert order.shipments[1].parcel.length == parcel1['length']
70+
assert order.shipments[1].parcel.width == parcel1['width']
71+
assert order.shipments[1].parcel.weight == parcel1['weight']
72+
assert order.shipments[0].parcel.height == 5.0
73+
assert order.shipments[0].parcel.length == 8.0
74+
assert order.shipments[0].parcel.width == 5.0
75+
assert order.shipments[0].parcel.weight == 16.0
7576

7677
order.buy(carrier='USPS', service='Priority')
7778

tests/webhook.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Unit tests related to 'Webhook' (https://www.easypost.com/webhooks-guide).
2+
3+
import unittest
4+
import easypost
5+
from constants import API_KEY as api_key
6+
7+
easypost.api_key = api_key
8+
9+
10+
class WebhookTests(unittest.TestCase):
11+
12+
def test_webhooks(self):
13+
# Create a webhook
14+
webhook = easypost.Webhook.create(url="example.com")
15+
assert webhook.id is not None
16+
assert webhook.mode == "test"
17+
assert webhook.url == "http://example.com"
18+
assert webhook.disabled_at is None
19+
assert type(webhook) == easypost.Webhook
20+
21+
# Retrieve a webhook
22+
webhook2 = easypost.Webhook.retrieve(webhook.id)
23+
assert webhook2.id is not None
24+
assert webhook2.id == webhook.id
25+
26+
# Update a webhook (re-enable it)
27+
webhook3 = webhook.update()
28+
assert webhook3.id is not None
29+
assert webhook3.id == webhook.id
30+
31+
# Index webhooks
32+
webhooks = easypost.Webhook.all()
33+
assert webhooks["webhooks"][len(webhooks["webhooks"]) - 1].id == webhook.id
34+
35+
# Delete a webhook
36+
webhook.delete()
37+
try:
38+
easypost.Webhook.retrieve(webhook.id)
39+
except easypost.Error as e:
40+
assert e.http_status == 404
41+
assert e.message == "The requested resource could not be found."
42+
43+
44+
if __name__ == '__main__':
45+
unittest.main()

0 commit comments

Comments
 (0)