Fix for adding records to dynamodb -
This commit is contained in:
parent
f62f7eaa94
commit
f790e3eae8
|
@ -8,7 +8,7 @@ class AddDynamoItem:
|
|||
"""Add a new record to a dynamo db table."""
|
||||
|
||||
def __init__(
|
||||
self, table_name: str, item_data: str
|
||||
self, table_name: str, item_data: dict
|
||||
):
|
||||
"""
|
||||
:param table_name: The name of hte Dynamo DB table to add information to.
|
||||
|
@ -17,6 +17,7 @@ class AddDynamoItem:
|
|||
and a response string.
|
||||
"""
|
||||
self.table_name = table_name
|
||||
self.fix_floats(item_data)
|
||||
self.item_data = item_data
|
||||
|
||||
|
||||
|
@ -25,11 +26,18 @@ class AddDynamoItem:
|
|||
# Get the service resource.
|
||||
self.dynamodb = SimpleAuth("dynamodb", config).get_resource()
|
||||
self.table = self.dynamodb.Table(self.table_name)
|
||||
self.item_data = json.loads(self.item_data, parse_float=Decimal)
|
||||
|
||||
|
||||
result = self.table.put_item(Item=self.item_data)
|
||||
if "ResponseMetadata" in result:
|
||||
del result["ResponseMetadata"]
|
||||
result_str = json.dumps(result)
|
||||
return dict(response=result_str, mimetype="application/json")
|
||||
|
||||
def fix_floats(self, data_dict: dict):
|
||||
"""Boto3 does not like floats. It will not accept them. So we
|
||||
identify floats ahead of time and turn them into strings to avoid
|
||||
unexpected issues for people. DynamoDB will correctly interpret
|
||||
the number on the other side, as it already knows the types."""
|
||||
for key, val in data_dict.items():
|
||||
if isinstance(val, float):
|
||||
data_dict[key] = str(val)
|
||||
|
|
Loading…
Reference in New Issue