Repository navigation
Expand file tree
/
Copy pathlambda_handler.py
More file actions
61 lines (49 loc) · 1.9 KB
/
Copy pathlambda_handler.py
File metadata and controls
61 lines (49 loc) · 1.9 KB
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
import json
import boto3
def lambda_handler(event, context):
fetch_type = event['rawPath'].split('/')[-1]
print(fetch_type)
RDS_CLIENT = boto3.client('rds-data', region_name='us-east-1')
if fetch_type == 'self':
username = event['rawQueryString'].split('=')[-1]
try:
response = RDS_CLIENT.execute_statement(
continueAfterTimeout = True,
resourceArn = 'INSERT DB CLUSTER ARN HERE',
secretArn = 'INSERT SECRET ARN HERE',
database = 'users',
sql = 'SELECT * FROM ButtonParty WHERE username=:username;',
parameters = [
{'name': 'username', 'value': {'stringValue': str(username)}},
]
)['records'][0]
response = {
'username': response[0]['stringValue'],
'score': response[1]['longValue'],
'streak': response[2]['longValue']
}
except Exception as e:
response = {
'error': str(e)
}
elif fetch_type == 'lb':
try:
response = RDS_CLIENT.execute_statement(
continueAfterTimeout = True,
resourceArn = 'INSERT DB CLUSTER ARN HERE',
secretArn = 'INSERT SECRET ARN HERE',
database = 'users',
sql = 'SELECT username, score FROM ButtonParty ORDER BY score DESC LIMIT 10'
)['records']
response = [{
'username': response[i][0]['stringValue'],
'score': response[i][1]['longValue']
} for i in range(len(response))]
except Exception as e:
response = {
'error': str(e)
}
return {
'statusCode': 200,
'body': json.dumps(response)
}