/django_webhooks

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

  1. Download the code from GitHub:

     git clone git://github.com/johnboxall/django-webhooks.git webhooks
    
  2. Edit settings.py and add webhooks to your INSTALLED_APPS:

     # settings.py
     ...
     INSTALLED_APPS = (... 'webhooks', ...)
    
  3. Register webhooks for models - admin.py is a good place:

     # admin.py
    
     from webhooks import webhooks
    
     webhooks.register(MyModel, ["fields", "to", "serialize"])
    
  4. Create Listeners for the webhook. For example to create a Listener that will be messaged whenever a User instance with username="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)
    
  5. Profit.

Creating a Webhook Endpoint

  1. Django:

     # views.py
     import simplejson
     
     def listener(request):
         json = request.raw_post_data
         webhook = simplejson.loads(json)
    
  2. 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

  1. HTTP POST request / raw_post_data
  2. Creating a listener ...
  3. ...

ToDo

  1. Code in webhooks.models.Message should be moved into webhooks.helpers so it can be subclassed / overridden more easily.
  2. Add HMAC authorization headers to all Webhooks messages ([http://code.google.com/p/support/wiki/PostCommitWebHooks](see Google Code implementation)
  3. Add verify view for (think PayPal IPN)

Resources