This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
58 lines (50 loc) · 1.83 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
import decimal
import json
import os
import boto3
from botocore.exceptions import ClientError
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if o % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
def hello(event, context):
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table(os.getenv("DYNAMODB_TABLE_DISCORD_GUILD"))
try:
responseEntry = table.get_item(
Key={
'id': int(event['pathParameters']['guildId'])
}
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print(responseEntry)
if 'Item' in responseEntry:
item = responseEntry['Item']
if 'json' in item and 'settings' in item['json'] and event['pathParameters']['key'] in item['json']['settings']:
body = {
"guildId": int(event['pathParameters']['guildId']),
"key": event['pathParameters']['key'],
"value": item['json']['settings'][event['pathParameters']['key']]
}
response = {
"statusCode": 200,
"body": json.dumps(body, cls=DecimalEncoder)
}
else:
body = {
"guildId": int(event['pathParameters']['guildId']),
"error": "Setting entry not found"
}
response = {
"statusCode": 404,
"body": json.dumps(body, cls=DecimalEncoder)
}
return response
# Use this code if you don't use the http event with the LAMBDA-PROXY
# integration