-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
59 lines (49 loc) · 2.03 KB
/
handler.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
import json
import logging
from functools import wraps
from urllib import request
from http import HTTPStatus
import os
DISCORD_WEBHOOK = os.environ['DISCORD_WEBHOOK']
API_KEY = os.environ['API_KEY']
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
def send_discord_message(message, discord_webhook):
data = {
'content': message
}
headers = {
'Content-Type': 'application/json',
'User-Agent': 'postman'
}
data = json.dumps(data)
data = data.encode()
req = request.Request(discord_webhook, data, headers, method='POST')
with request.urlopen(req) as response:
return response.read(), response.getheaders()
def validate_api_token():
def decorator(f):
@wraps(f)
def decorated_function(event, context):
response_headers = {
"content-type": "application/json",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,PUT,DELETE,HEAD"
}
try:
api_token = event['headers'].get('Authorization')
is_valid_api_token = not api_token or api_token != API_KEY
if is_valid_api_token:
return {'statusCode': HTTPStatus.UNAUTHORIZED, 'body': json.dumps({"error": 'invalid token'}), "headers": response_headers}
data = f(event, context)
data = json.dumps(data)
return {'statusCode': HTTPStatus.OK, 'body': data, "headers": response_headers}
except:
logging.exception('unexpected error')
return {'statusCode': HTTPStatus.INTERNAL_SERVER_ERROR, 'body': json.dumps({"error": 'unexpected error'}), "headers": response_headers}
return decorated_function
return decorator
@validate_api_token()
def handler_open_garage(event, context) -> dict:
send_discord_message(f'open from aws', DISCORD_WEBHOOK)
return {}