Skip to content

Commit 636020f

Browse files
committed
Merge pull request zapier#25 from westerncapelabs/master
Update async delivery instructions.
2 parents aefad07 + a23c819 commit 636020f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![Travis CI Build](https://img.shields.io/travis/zapier/django-rest-hooks/master.svg)](https://travis-ci.org/zapier/django-rest-hooks)
22
[![PyPI Download](https://img.shields.io/pypi/v/django-rest-hooks.svg)](https://pypi.python.org/pypi/django-rest-hooks)
33
[![PyPI Status](https://img.shields.io/pypi/status/django-rest-hooks.svg)](https://pypi.python.org/pypi/django-rest-hooks)
4-
## What are Django REST Hooks?
4+
## What are Django REST Hooks?
55

66

77
REST Hooks are fancier versions of webhooks. Traditional webhooks are usually
@@ -361,20 +361,31 @@ import requests
361361

362362

363363
class DeliverHook(Task):
364-
def run(self, target, payload, instance=None, hook=None, **kwargs):
364+
def run(self, target, payload, instance_id=None, hook_id=None, **kwargs):
365365
"""
366366
target: the url to receive the payload.
367367
payload: a python primitive data structure
368-
instance: a possibly null "trigger" instance
369-
hook: the defining Hook object
368+
instance_id: a possibly None "trigger" instance ID
369+
hook_id: the ID of defining Hook object
370370
"""
371371
requests.post(
372372
url=target,
373373
data=json.dumps(payload),
374374
headers={'Content-Type': 'application/json'}
375375
)
376376

377-
deliver_hook_wrapper = DeliverHook.delay
377+
378+
def deliver_hook_wrapper(target, payload, instance, hook):
379+
# instance is None if using custom event, not built-in
380+
if instance is not None:
381+
instance_id = instance.id
382+
else:
383+
instance_id = None
384+
# pass ID's not objects because using pickle for objects is a bad thing
385+
kwargs = dict(target=target, payload=payload,
386+
instance_id=instance_id, hook_id=hook.id)
387+
DeliverHook.apply_async(kwargs=kwargs)
388+
378389
```
379390

380391
We also don't handle retries or cleanup. Generally, if you get a `410` or

0 commit comments

Comments
 (0)