django_webhooks
django_webhooks copied to clipboard
Django WebHooks makes it easy to integrate WebHooks into your Django Project.
Django WebHooks
About
Web Hooks is an open initiative to standardize event notifications between web services by subscribing to URLs.
Django WebHooks makes it easy to integrate WebHooks into your Django Project.
Using Django Webhooks
-
Download the code from GitHub:
git clone git://github.com/johnboxall/django-webhooks.git webhooks -
Edit
settings.pyand addwebhooksto yourINSTALLED_APPS:# settings.py ... INSTALLED_APPS = (... 'webhooks', ...) -
Register webhooks for models -
admin.pyis a good place:# admin.py from webhooks import webhooks webhooks.register(MyModel, ["fields", "to", "serialize"]) -
Create Listeners for the webhook. For example to create a Listener that will be messaged whenever a
Userinstance withusername="john"is saved:from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from webhooks.models import Listener user_type = ContentType.objects.get(app_label="auth", model="user") john = User.objects.get(username="john") Listener.create(obj_type=user_type, obj_property="username", obj_value="john", url="http://where.john/is/listening/", owner=john) -
Profit.
Creating a Webhook Endpoint
-
Django:
# views.py import simplejson def listener(request): json = request.raw_post_data webhook = simplejson.loads(json) -
PHP:
<?php // http://ca3.php.net/manual/en/reserved.variables.httprawpostdata.php // http://ca3.php.net/manual/en/wrappers.php.php $json = file_get_contents('php://input'); // http://ca3.php.net/manual/en/function.json-decode.php var_dump(json_decode($json, true)); ?>
Details
- HTTP POST request / raw_post_data
- Creating a listener ...
- ...
ToDo
- Code in
webhooks.models.Messageshould be moved intowebhooks.helpersso it can be subclassed / overridden more easily. - Add HMAC authorization headers to all Webhooks messages ([http://code.google.com/p/support/wiki/PostCommitWebHooks](see Google Code implementation)
- Add verify view for (think PayPal IPN)
Resources
- http://blog.webhooks.org/
- http://blogrium.com/2006/12/27/automator-for-the-web/
- http://blogrium.com/2006/11/27/lets-make-seeking-bliss-easier/
- http://blogrium.com/?p=70
- http://www.slideshare.net/progrium/web-hooks Web-Hooks by blogrium.com / superhappydevhouse
- http://groups.google.com/group/webhooks/ webhooks google group
- http://code.google.com/p/support/wiki/PostCommitWebHooks
- http://www.slideshare.net/progrium/web-hooks-and-the-programmable-world-of-tomorrow-presentation
- http://github.com/guides/post-receive-hooks