-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
155 lines (117 loc) · 3.69 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
150
151
152
153
154
155
from datetime import datetime
import boto3
import os
import uuid
import json
import logging
import dynamo # helper function
import uuid
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb = boto3.client('dynamodb')
# dynamodb = boto3.resource(
# 'dynamodb', region_name=str(os.environ['REGION_NAME']))
table_name = str(os.environ['DYNAMODB_TABLE'])
def create(event, context):
logger.info(f'Incoming request is: {event}')
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occured while creating post."
}
post_str = event['body']
post = json.loads(post_str)
current_timestamp = datetime.now().isoformat()
post['createdAt'] = current_timestamp
post['id'] = str(uuid.uuid4())
res = dynamodb.put_item(
TableName=table_name,
Item=dynamo.to_item(post)
)
# If creation is successful
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 201,
}
return response
def get(event, context):
print(":::::==>>>", event['pathParameters'])
logger.info(f'Incoming request is: {event}')
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occured while getting post."
}
print(":::::==>>>", event)
post_id = event['pathParameters']['postId']
post_query = dynamodb.get_item(
TableName=table_name, Key={'id': {'S': post_id}})
if 'Item' in post_query:
post = post_query['Item']
logger.info(f'Post is: {post}')
response = {
"statusCode": 200,
'headers': {'Content-Type': 'application/json'},
"body": json.dumps(dynamo.to_dict(post))
}
return response
def all(event, context):
# Set the default error response
response = {
"statusCode": 500,
"body": "An error occured while getting all posts."
}
scan_result = dynamodb.scan(TableName=table_name)['Items']
posts = []
for item in scan_result:
posts.append(dynamo.to_dict(item))
response = {
"statusCode": 200,
"body": json.dumps(posts)
}
return response
def update(event, context):
logger.info(f'Incoming request is: {event}')
post_id = event['pathParameters']['postId']
# Set the default error response
response = {
"statusCode": 500,
"body": f"An error occured while updating post {post_id}"
}
post_str = event['body']
post = json.loads(post_str)
res = dynamodb.update_item(
TableName=table_name,
Key={
'id': {'S': post_id}
},
UpdateExpression="set content=:c, author=:a, updatedAt=:u",
ExpressionAttributeValues={
':c': dynamo.to_item(post['content']),
':a': dynamo.to_item(post['author']),
':u': dynamo.to_item(datetime.now().isoformat())
},
ReturnValues="UPDATED_NEW"
)
# If updation is successful for post
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 200,
}
return response
def delete(event, context):
logger.info(f'Incoming request is: {event}')
post_id = event['pathParameters']['postId']
# Set the default error response
response = {
"statusCode": 500,
"body": f"An error occured while deleting post {post_id}"
}
res = dynamodb.delete_item(TableName=table_name, Key={
'id': {'S': post_id}})
# If deletion is successful for post
if res['ResponseMetadata']['HTTPStatusCode'] == 200:
response = {
"statusCode": 204,
}
return response