-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_reviews.py
92 lines (73 loc) · 2.78 KB
/
lambda_reviews.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
import os
import logging
import json
import boto3
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key
logger = logging.getLogger()
logger.setLevel(logging.INFO)
dynamodb_client = boto3.client("dynamodb")
if os.getenv("AWS_SAM_LOCAL"):
dynamodb_client = boto3.client('dynamodb', endpoint_url='http://docker.for.mac.localhost:8000/')
table_name = os.environ["TABLE_NAME"]
logger.info("Using table {}".format(table_name))
def create_response(status_code, body):
response = {
"statusCode": status_code,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(body) or ""
}
logger.info("Created response: {}".format(response))
return response
# WIP
def handler_get_all(event, context):
logger.info("Received event {}".format(event))
# Get profile_id from authentication header
profile_id = "33"
try:
result = dynamodb_client.query(
KeyConditionExpression=Key("profile_id").eq(profile_id),
TableName=table_name
)
logger.info("Get Item result: {}".format(result))
return create_response(200, result["Items"])
except ClientError as e:
logger.info("Get Item failed: {}".format(e))
return create_response(500, e.response["Error"]["Message"])
def handler_get(event, context):
logger.info("Received event {}".format(event))
try:
result = dynamodb_client.get_item(
TableName=table_name,
Key={"id": {"S": event["pathParameters"]["reviewId"]}}
)
logger.info("Get Item result: {}".format(result))
return create_response(200, result["Item"])
except ClientError as e:
logger.info("Get Item failed: {}".format(e))
return create_response(500, e.response["Error"]["Message"])
def handler_put(event, context):
logger.info("Received event {}".format(event))
item = json.loads(event["body"])['item']
try:
result = dynamodb_client.put_item(
TableName=table_name,
Item=item
)
logger.info("Get Item result: {}".format(result))
return create_response(200, result)
except ClientError as e:
logger.info("Get Item failed: {}".format(e))
return create_response(500, e.response["Error"]["Message"])
def handler_delete(event, context):
logger.info("Received event {}".format(event))
try:
result = dynamodb_client.delete_item(
TableName=table_name,
Key={"id": {"S": event["pathParameters"]["reviewId"]}}
)
logger.info("Delete Item result: {}".format(result))
return create_response(200, result)
except ClientError as e:
logger.info("Delete Item failed: {}".format(e))
return create_response(500, e.response["Error"]["Message"])