-
Notifications
You must be signed in to change notification settings - Fork 8
/
post_to_destination.py
67 lines (48 loc) · 1.58 KB
/
post_to_destination.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Performs a POST to the specified destination URL. This config is
specific to running on the AWS Lambda service.
The URL is read from the `destination_url` environment variable.
"""
import requests
import json
import hookrich as hr
import logging
import os
log = logging.getLogger()
log.setLevel(logging.INFO)
def _response(**resp):
"""
Return an API Gateway compatible response.
"""
return {'body': json.dumps(resp)}
def post_to_destination(event_name, event_url):
"""
Call the destination URL with event details.
"""
payload = hr.enrich_payload(event_name, event_url)
payload['event_name'] = event_name
return requests.post(os.environ['destination_url'], data=payload).json()
def lambda_function_handler(event, context):
"""
AWS Lambda entry point.
"""
log.info("Received payload: {}".format(event))
try:
body = json.loads(event['body'])
event_name = body['event']
except (TypeError, KeyError):
msg = "Invalid webhook event: {}".format(event)
log.error(msg)
return _response(result='error', message=msg)
try:
event_url = body[hr.WEBHOOK_EVENTS[event_name]['url']]
except KeyError:
msg = "Unsupported event type: {}".format(event_name)
log.error(msg)
return _response(result='error', message=msg)
try:
response = post_to_destination(event_name, event_url)
except Exception as error:
log.error(str(error))
return _response(result='error', message=str(error))
return _response(result='success', response=response)