Created
April 20, 2017 15:24
-
-
Save imsickofmaps/09dcb8c4aa37ed5057ce6c190d19b0ae to your computer and use it in GitHub Desktop.
This file contains 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
class DeliverHook(Task): | |
def run(self, target, payload, instance_id=None, hook_id=None, auth_token=None, **kwargs): | |
""" | |
target: the url to receive the payload. | |
payload: a python primitive data structure | |
instance_id: a possibly None "trigger" instance ID | |
hook_id: the ID of defining Hook object | |
""" | |
headers = { | |
'Content-Type': 'application/json' | |
} | |
if auth_token: | |
headers['Authorization'] = 'Token %s' % auth_token | |
requests.post( | |
url=target, | |
data=json.dumps(payload), | |
headers=headers | |
) | |
def deliver_hook_wrapper(target, payload, instance, hook): | |
if instance is not None: | |
if isinstance(instance.id, uuid.UUID): | |
instance_id = str(instance.id) | |
else: | |
instance_id = instance.id | |
else: | |
instance_id = None | |
kwargs = dict(target=target, payload=payload, | |
instance_id=instance_id, hook_id=hook.id, | |
auth_token=instance.user.auth_token.key) | |
DeliverHook.apply_async(kwargs=kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment