Skip to content

Commit

Permalink
added basic CRUD methods for Webhook objects
Browse files Browse the repository at this point in the history
  • Loading branch information
victoryftw committed Jan 18, 2017
1 parent f3820ea commit 4264acb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
=== 3.5.0 2017-01-18

* Added basic CRUD methods for Webhook Objects
* Fixed Order test


=== 3.4.0 2016-12-20

* Added session pooling
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.4.0
3.5.0
15 changes: 13 additions & 2 deletions easypost/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None):
'Report': Report,
'ShipmentReport': Report,
'PaymentLogReport': Report,
'TrackerReport': Report
'TrackerReport': Report,
'Webhook': Webhook
}

prefixes = {
Expand All @@ -106,7 +107,8 @@ def convert_to_easypost_object(response, api_key, parent=None, name=None):
'user': User,
'shprep': Report,
'plrep': Report,
'trkrep': Report
'trkrep': Report,
'hook': Webhook
}

if isinstance(response, list):
Expand Down Expand Up @@ -984,3 +986,12 @@ def retrieve(cls, easypost_id, api_key=None, **params):
url = "%s/%s" % (cls.class_url(), easypost_id)
response, api_key = requestor.request('get', url)
return response["signed_url"]


class Webhook(AllResource, CreateResource, DeleteResource):
def update(self, **params):
requestor = Requestor(self.api_key)
url = self.instance_url()
response, api_key = requestor.request('put', url, params)
self.refresh_from(response, api_key)
return self
2 changes: 1 addition & 1 deletion easypost/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = '3.4.0'
VERSION = '3.5.0'
17 changes: 9 additions & 8 deletions tests/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ def test_order_create_then_buy(self):

# Assert the shipment's parcel
assert len(order.shipments) == 2
assert order.shipments[0].parcel.height == parcel1['height']
assert order.shipments[0].parcel.length == parcel1['length']
assert order.shipments[0].parcel.width == parcel1['width']
assert order.shipments[0].parcel.weight == parcel1['weight']
assert order.shipments[1].parcel.height == 5.0
assert order.shipments[1].parcel.length == 8.0
assert order.shipments[1].parcel.width == 5.0
assert order.shipments[1].parcel.weight == 16.0

assert order.shipments[1].parcel.height == parcel1['height']
assert order.shipments[1].parcel.length == parcel1['length']
assert order.shipments[1].parcel.width == parcel1['width']
assert order.shipments[1].parcel.weight == parcel1['weight']
assert order.shipments[0].parcel.height == 5.0
assert order.shipments[0].parcel.length == 8.0
assert order.shipments[0].parcel.width == 5.0
assert order.shipments[0].parcel.weight == 16.0

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

Expand Down
45 changes: 45 additions & 0 deletions tests/webhook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Unit tests related to 'Webhook' (https://www.easypost.com/webhooks-guide).

import unittest
import easypost
from constants import API_KEY as api_key

easypost.api_key = api_key


class WebhookTests(unittest.TestCase):

def test_webhooks(self):
# Create a webhook
webhook = easypost.Webhook.create(url="example.com")
assert webhook.id is not None
assert webhook.mode == "test"
assert webhook.url == "http://example.com"
assert webhook.disabled_at is None
assert type(webhook) == easypost.Webhook

# Retrieve a webhook
webhook2 = easypost.Webhook.retrieve(webhook.id)
assert webhook2.id is not None
assert webhook2.id == webhook.id

# Update a webhook (re-enable it)
webhook3 = webhook.update()
assert webhook3.id is not None
assert webhook3.id == webhook.id

# Index webhooks
webhooks = easypost.Webhook.all()
assert webhooks["webhooks"][len(webhooks["webhooks"]) - 1].id == webhook.id

# Delete a webhook
webhook.delete()
try:
easypost.Webhook.retrieve(webhook.id)
except easypost.Error as e:
assert e.http_status == 404
assert e.message == "The requested resource could not be found."


if __name__ == '__main__':
unittest.main()

0 comments on commit 4264acb

Please sign in to comment.