-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
40 lines (33 loc) · 1.26 KB
/
lambda_function.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
import json, boto3, logging, os
from botocore.exceptions import ClientError
from decimal import Decimal
logger = logging.getLogger()
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ.get('TABLENAME'))
def lambda_handler(event, context):
return get_item(event)
def get_item(event):
queryParam = event["queryStringParameters"]
try:
response = table.get_item(Key={'accountid': queryParam['accountid'], 'vendorid':queryParam['vendorid']})
return {
'statusCode': 200,
'headers': {
"Content-Type": 'application/json',
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": 'Content-Type,X-Amz-Date,X-Api-Key',
"Access-Control-Allow-Methods": "OPTIONS,POST"
},
'body': json.dumps(response, default=handle_decimal_type)
}
except ClientError:
logger.exception("Couldn't Getitem from table %s",table)
raise
#This function is used to convert decimal to float. Decimal can not be serialised to JSON format.
def handle_decimal_type(obj):
if isinstance(obj, Decimal):
if float(obj).is_integer():
return int(obj)
else:
return float(obj)
raise TypeError