-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.py
68 lines (54 loc) · 1.89 KB
/
update.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
60
61
62
63
64
65
66
67
68
import boto3
import datetime
import json
import os
region_name = os.getenv('Region')
table_name = os.getenv('NSTable')
ddb = boto3.resource('dynamodb', region_name=region_name).Table(table_name)
def lambda_handler(event, context, ddb):
headers = {
"Content-Type": "application/json",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
"Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT",
"Access-Control-Allow-Origin": "*"}
try:
responseBody = event["body"]
data = responseBody.split("&")
long_url = data[0].split("=")[1]
short_url = data[1].split("=")[1]
noid = data[2].split("=")[1]
create_date_str = data[3].split("=")[1]
create_date = datetime.datetime.strptime(
create_date_str, '%Y-%m-%dT%H:%M:%S')
insert_date_str = create_date.strftime("%Y-%m-%dT%H:%M:%S")
item = ddb.get_item(Key={'short_id': noid})
if item.get('Item'):
record = {}
record["created_at"] = insert_date_str
record["long_url"] = long_url
record["short_id"] = noid
record["short_url"] = short_url
record["hits"] = 0
record["ttl"] = 4129578000
ddb.put_item(Item=record)
else:
return {
"statusCode": 200,
"headers": headers,
"body": json.dumps({
"message": "Rec {0} is not exist.".format(noid),
}),
}
except BaseException:
return {
"statusCode": 503,
"headers": headers,
"body": "Rec update is failed.",
}
return {
"statusCode": 200,
"headers": headers,
"body": json.dumps({
"message": "Rec {0} is updated.".format(noid),
}),
}