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

Commit 68ac626

Browse files
authored
added webhook creation method and example (#62)
* bump version, include webhook example, add webhook class * Update `example/tracking.py` * Update `api_requestor.py` cleaner empty request body check (from 204 DELETE).
1 parent 79af320 commit 68ac626

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

examples/tracking.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,24 @@
2323
tracking = shippo.Track.get_status(carrier_token, tracking_number)
2424
print(tracking)
2525

26-
# Registering a tracking webhook
26+
# Create a webhook endpoint (FYI-basic auth not supported)
27+
# For a full list of Webhook Event Types see https://goshippo.com/docs/webhooks/
28+
new_webhook_response = shippo.Webhook.create(url='https://exampledomain.com',event='all')
29+
print(new_webhook_response)
30+
31+
# list webhook(s)
32+
webhook_list = shippo.Webhook.list_webhooks()
33+
print(webhook_list)
34+
35+
# remove all webhooks
36+
for webhook in webhook_list['results']:
37+
print("about to delete webhook {}".format(webhook['object_id']))
38+
webhook_remove = shippo.Webhook.delete(object_id=webhook['object_id'])
39+
# print empty 204 status
40+
print(webhook_remove)
41+
42+
43+
# Registering a tracking number for webhook
2744
webhook_response = shippo.Track.create(
2845
carrier=carrier_token,
2946
tracking_number=tracking_number,

shippo/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
Shipment,
1212
Track,
1313
Transaction,
14+
Webhook,
1415
)

shippo/api_requestor.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ def interpret_response(self, rbody, rcode):
162162
try:
163163
if hasattr(rbody, 'decode'):
164164
rbody = rbody.decode('utf-8')
165-
resp = util.json.loads(rbody)
165+
if rbody == '':
166+
rbody = '{"msg": "empty_response"}'
167+
resp = util.json.loads(rbody)
166168
except Exception:
167169
raise error.APIError(
168170
"Invalid response body from API: %s "

shippo/resource.py

+69
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ def update(cls, object_id, api_key=None, **params):
231231
response, api_key = requestor.request('put', url, params)
232232
return convert_to_shippo_object(response, api_key)
233233

234+
@classmethod
235+
def remove(cls, object_id, api_key=None, **params):
236+
object_id = object_id
237+
extn = urllib.parse.quote_plus(object_id)
238+
requestor = api_requestor.APIRequestor(api_key)
239+
url = cls.class_url() + extn
240+
response, api_key = requestor.request('delete', url, params)
241+
return "Deleted the webhook"
242+
234243

235244
# ---- API objects ----
236245

@@ -364,6 +373,66 @@ class CarrierAccount(CreateableAPIResource, ListableAPIResource, FetchableAPIRes
364373
def class_url(cls):
365374
return "v1/carrier_accounts/"
366375

376+
class Webhook(CreateableAPIResource, ListableAPIResource, FetchableAPIResource, UpdateableAPIResource):
377+
"""
378+
retrieve, update and delete webhooks for a Shippo account programmatically. The same functionality is already exposed in the Shippo dashboard at https://app.goshippo.com/api/.
379+
380+
To add both a webhook and track a url at the same see the shippo.Track.create function.
381+
"""
382+
383+
@classmethod
384+
def class_url(cls):
385+
cls_name = cls.class_name()
386+
return "v1/%ss/" % (cls_name,)
387+
388+
@classmethod
389+
def list_webhooks(cls, api_key=None, **params):
390+
"""List all the webhooks associated with the account"""
391+
return super(Webhook, cls).all(api_key, **params)
392+
393+
@classmethod
394+
def create(cls, api_key=None, **params):
395+
"""Create a Webhook to push events from Shippo (i.e tracking,transations)
396+
397+
Arguments:
398+
**params
399+
url (str) -- url of your webhook (make sure it is not behind basic auth.
400+
endpoint must return 200 when it receives a POST
401+
event (str) -- any valid webhook event as listed here https://goshippo.com/docs/webhooks.
402+
is_test (str) -- set the webhook object to test or live mode
403+
Keyword Arguments:
404+
api_key (str) -- an api key, if not specified here it will default to the key
405+
set in your environment var or by shippo.api_key = "..."
406+
407+
Returns:
408+
(ShippoObject) -- The server response
409+
"""
410+
411+
return super(Webhook, cls).create(api_key, **params)
412+
413+
@classmethod
414+
def update_webhook(cls,object_id, api_key=None, **params):
415+
"""
416+
Update webhook's url, is_test, and/or event
417+
"""
418+
return super(Webhook, cls).update(api_key, **params)
419+
420+
@classmethod
421+
def delete(cls, object_id,api_key=None, **params):
422+
""" Remove webhook
423+
424+
Arguments:
425+
object_id (str) -- object_id of webhook
426+
Keyword Arguments:
427+
api_key (str) -- an api key, if not specified here it will default to the key
428+
set in your environment var or by shippo.api_key = "..."
429+
430+
Returns:
431+
(ShippoObject) -- The server response
432+
"""
433+
return super(Webhook, cls).remove(object_id,api_key, **params)
434+
435+
367436

368437
class Track(CreateableAPIResource):
369438
"""

shippo/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '2.0.0'
1+
VERSION = '2.0.1'

0 commit comments

Comments
 (0)