Created
December 30, 2014 20:19
-
-
Save joshfinnie/d332ae9c90c223e6b34c to your computer and use it in GitHub Desktop.
BreweryDB webhook example (Django)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
from django.conf import settings | |
from django.core.exceptions import PermissionDenied | |
def webhook_receive(request): | |
""" | |
View used to receive webhooks from BreweryDB | |
""" | |
isValid = hashlib.sha1(settings.BREWERYDB_API_KEY + request.GET['nonce']) == request.GET['key'] | |
if request.method is 'POST' and isValid: | |
# this will be one of beer, brewery, location, guild, or event | |
attribute = request.POST['attribute']; | |
# The corresponding beerId, breweryId, locationId, | |
# guildId, or eventId | |
# depending upon what the value of attribute is | |
attribute_id = request.POST['attributeId']; | |
# The action that was taken on the attribute. | |
# Either insert, delete, or edit | |
action = request.POST['action']; | |
# The subaction that was taken on the attribute. | |
# Either 'none' or various other options like | |
# 'socialaccount-update' or 'beer-add' | |
sub_action = request.POST['subAction']; | |
# Timestamp that the change was integrated | |
timestamp = request.POST['timestamp']; | |
# Now you can take whatever action you need based | |
# on the information you received | |
# The best practice is to put this data in a queue | |
# somewhere locally for you to process it in an | |
# asynchronous manner so you don't get your webhoook | |
# disabled because it takes too long to respond. | |
else: | |
raise PermissionDenied |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment