2018-07-31 16:17:24 -04:00
|
|
|
import json
|
|
|
|
|
2018-11-02 15:20:08 -04:00
|
|
|
import requests
|
2018-05-30 11:19:10 -04:00
|
|
|
from django_rq import job
|
2018-07-31 16:17:24 -04:00
|
|
|
from rest_framework.utils.encoders import JSONEncoder
|
2018-05-30 11:19:10 -04:00
|
|
|
|
2020-01-17 11:28:50 -05:00
|
|
|
from .choices import ObjectChangeActionChoices, WebhookContentTypeChoices
|
2020-01-23 15:04:40 -05:00
|
|
|
from .webhooks import generate_signature
|
2018-05-30 11:19:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
@job('default')
|
2019-03-24 15:35:42 -04:00
|
|
|
def process_webhook(webhook, data, model_name, event, timestamp, username, request_id):
|
2018-05-30 11:19:10 -04:00
|
|
|
"""
|
|
|
|
Make a POST request to the defined Webhook
|
|
|
|
"""
|
|
|
|
payload = {
|
2019-12-05 16:42:10 -05:00
|
|
|
'event': dict(ObjectChangeActionChoices)[event].lower(),
|
2018-05-30 11:19:10 -04:00
|
|
|
'timestamp': timestamp,
|
2018-12-04 00:40:54 -05:00
|
|
|
'model': model_name,
|
2019-03-24 15:31:12 -04:00
|
|
|
'username': username,
|
2019-03-24 15:35:42 -04:00
|
|
|
'request_id': request_id,
|
2018-05-30 11:19:10 -04:00
|
|
|
'data': data
|
|
|
|
}
|
|
|
|
headers = {
|
2020-01-22 20:33:57 -05:00
|
|
|
'Content-Type': webhook.http_content_type,
|
2018-05-30 11:19:10 -04:00
|
|
|
}
|
2019-10-13 03:09:58 -04:00
|
|
|
if webhook.additional_headers:
|
|
|
|
headers.update(webhook.additional_headers)
|
|
|
|
|
2018-05-30 11:19:10 -04:00
|
|
|
params = {
|
|
|
|
'method': 'POST',
|
|
|
|
'url': webhook.payload_url,
|
|
|
|
'headers': headers
|
|
|
|
}
|
|
|
|
|
2020-01-17 11:28:50 -05:00
|
|
|
if webhook.http_content_type == WebhookContentTypeChoices.CONTENTTYPE_JSON:
|
2018-07-31 16:17:24 -04:00
|
|
|
params.update({'data': json.dumps(payload, cls=JSONEncoder)})
|
2020-01-17 11:28:50 -05:00
|
|
|
elif webhook.http_content_type == WebhookContentTypeChoices.CONTENTTYPE_FORMDATA:
|
2018-05-30 11:19:10 -04:00
|
|
|
params.update({'data': payload})
|
|
|
|
|
|
|
|
prepared_request = requests.Request(**params).prepare()
|
|
|
|
|
|
|
|
if webhook.secret != '':
|
2018-08-07 14:19:46 -04:00
|
|
|
# Sign the request with a hash of the secret key and its content.
|
2020-01-23 15:04:40 -05:00
|
|
|
prepared_request.headers['X-Hook-Signature'] = generate_signature(prepared_request.body, webhook.secret)
|
2018-05-30 11:19:10 -04:00
|
|
|
|
|
|
|
with requests.Session() as session:
|
|
|
|
session.verify = webhook.ssl_verification
|
2019-10-13 01:43:08 -04:00
|
|
|
if webhook.ca_file_path:
|
|
|
|
session.verify = webhook.ca_file_path
|
2018-05-30 11:19:10 -04:00
|
|
|
response = session.send(prepared_request)
|
|
|
|
|
2020-01-23 15:04:40 -05:00
|
|
|
if 200 <= response.status_code <= 299:
|
2018-05-30 11:19:10 -04:00
|
|
|
return 'Status {} returned, webhook successfully processed.'.format(response.status_code)
|
|
|
|
else:
|
|
|
|
raise requests.exceptions.RequestException(
|
2020-01-17 11:28:50 -05:00
|
|
|
"Status {} returned with content '{}', webhook FAILED to process.".format(
|
|
|
|
response.status_code, response.content
|
|
|
|
)
|
2018-05-30 11:19:10 -04:00
|
|
|
)
|