-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
73 lines (55 loc) · 2.23 KB
/
lambda_function.py
File metadata and controls
73 lines (55 loc) · 2.23 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
62
63
64
65
66
67
68
69
70
71
72
73
import json, ipaddress, boto3
from datetime import datetime
dynamodb = boto3.resource('dynamodb', endpoint_url="https://dynamodb.us-east-1.amazonaws.com")
table = dynamodb.Table('age_series_stats')
def put_stat(client_ip, uuid, mod_id, avg_sim_performance, game_lang, os_lang):
data = {}
data["ip"] = client_ip
data["date"] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data["uuid"] = uuid
data["mod_id"] = mod_id
data["avg_sim_performance"] = avg_sim_performance
data["game_lang"] = game_lang
data["os_lang"] = os_lang
table.put_item(Item=data)
def lambda_handler(event, context):
# print(json.dumps(event))
http_type = event["requestContext"]["http"]["method"]
source_ip = event["requestContext"]["http"]["sourceIp"]
try:
if (http_type == "POST"):
if ("body" in event):
body = json.loads(event["body"])
uuid = ""
if ("uuid" in body):
uuid = body["uuid"]
mod_id = ""
if ("mod_id" in body):
mod_id = body["mod_id"]
avg_sim_performance = ""
if ("avg_sim_performance" in body):
avg_sim_performance = body["avg_sim_performance"]
game_lang = ""
if ("game_lang" in body):
game_lang = body["game_lang"]
os_lang = ""
if ("os_lang" in body):
os_lang = body["os_lang"]
print("Sending stat to DynamoDB...")
put_stat(source_ip, uuid, mod_id, avg_sim_performance, game_lang, os_lang)
except Exception:
pass # Don't pass exceptions, just return 200 all the time.
# Generic response
message = "Age Series Stats API"
code = 200
response = {
"isBase64Encoded": False,
"statusCode": code,
'headers': {
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET'
},
"body": message
}
return response