-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
149 lines (112 loc) · 3.64 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import json
import logging
import boto3
import datetime
import dynamo
import os
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.client('dynamodb', endpoint_url="http://host.docker.internal:4566")
table_name = os.environ["ITEMS_TABLE"]
def create(event, context):
logger.info(f'Incoming request is: {event}')
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occurred while creating item."
}
# return response
item_str = event['body']
item = json.loads(item_str)
current_timestamp = datetime.datetime.now().isoformat()
item['createdAt'] = current_timestamp
res = dynamodb.put_item(
TableName=table_name, Item=dynamo.to_item(item))
# If creation is successful
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 201,
"body": "Successfully added item"
}
return response
def get(event, context):
logger.info(f'Incoming request is: {event}')
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occurred while getting item."
}
item_id = event['pathParameters']['itemId']
item_query = dynamodb.get_item(
TableName=table_name, Key={'id': {'N': item_id}})
if 'Item' in item_query:
item = item_query['Item']
logger.info(f'Item is: {item}')
response = {
"statusCode": 200,
"body": json.dumps(dynamo.to_dict(item))
}
return response
def all(event, context):
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occurred while getting all items."
}
scan_result = dynamodb.scan(TableName=table_name)['Items']
items = []
for item in scan_result:
items.append(dynamo.to_dict(item))
response = {
"statusCode": 200,
"body": json.dumps(items)
}
return response
def update(event, context):
logger.info(f'Incoming request is: {event}')
item_id = event['pathParameters']['itemId']
# Set the default error response
response = {
"statusCode": 500,
"body": f"An error occurred while updating item {item_id}"
}
item_str = event['body']
item = json.loads(item_str)
# current_timestamp = datetime.datetime.now().isoformat()
# item['updatedAt'] = current_timestamp
res = dynamodb.update_item(
TableName=table_name,
Key={
'id': {'N': item_id}
},
UpdateExpression="set file_name=:f, media_type=:m, updatedAt=:u",
ExpressionAttributeValues={
':f': dynamo.to_item(item['file_name']),
':m': dynamo.to_item(item['media_type']),
':u': dynamo.to_item(datetime.datetime.now().isoformat())
},
ReturnValues="UPDATED_NEW"
)
# If update is successful for item
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 200,
}
return response
def delete(event, context):
logger.info(f'Incoming request is: {event}')
item_id = event['pathParameters']['itemId']
# Set the default error response
response = {
"statusCode": 500,
"body": f"An error occurred while deleting item {item_id}"
}
res = dynamodb.delete_item(TableName=table_name, Key={
'id': {'N': item_id}})
# If deletion is successful for item
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 204,
"body": "Successfully deleted"
}
return response