-
Notifications
You must be signed in to change notification settings - Fork 2
/
notification_lambda_function.py
54 lines (40 loc) · 1.22 KB
/
notification_lambda_function.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
import re
import boto3
client = boto3.client('dynamodb', region_name='us-west-2')
def load_notification(major, minor):
return client.get_item(
TableName='trippin-notifications',
Key={
'major': {'N': str(major)},
'minor': {'N': str(minor)}
}
).get('Item')
def to_response(body):
return {
'status': '200',
'statusDescription': 'OK',
'headers': {
'content-type': [
{
'key': 'Content-Type',
'value': 'text/plain'
}
]
},
'body': body
}
def not_found_response():
return {
'status': '404',
'statusDescription': 'Not Found',
'body': 'Not Found'
}
def lambda_handler(event, context):
request = event['Records'][0]['cf']['request']
print(f'method={request["method"]}, path={request["uri"]}')
rex = '^/notifications/([0-9]+)/([0-9]+)$'
if request['method'] == 'GET' and (m := re.match(rex, request['uri'])):
major, minor = m.group(1), m.group(2)
if item := load_notification(major, minor):
return to_response(item['notification']['S'])
return not_found_response()