in workshop-3/app/monolith-service/service/mysfitsTableClient.py [0:0]
def getAllMysfits():
# Retrieve all Mysfits from DynamoDB using the DynamoDB scan operation.
# Note: The scan API can be expensive in terms of latency when a DynamoDB
# table contains a high number of records and filters are applied to the
# operation that require a large amount of data to be scanned in the table
# before a response is returned by DynamoDB. For high-volume tables that
# receive many requests, it is common to store the result of frequent/common
# scan operations in an in-memory cache. DynamoDB Accelerator (DAX) or
# use of ElastiCache can provide these benefits. But, because out Mythical
# Mysfits API is low traffic and the table is very small, the scan operation
# will suit our needs for this workshop.
response = client.scan(
TableName=table_name
)
logging.info(response["Items"])
# loop through the returned mysfits and add their attributes to a new dict
# that matches the JSON response structure expected by the frontend.
mysfitList = defaultdict(list)
for item in response["Items"]:
mysfit = {}
mysfit["mysfitId"] = item["MysfitId"]["S"]
mysfit["name"] = item["Name"]["S"]
mysfit["goodevil"] = item["GoodEvil"]["S"]
mysfit["lawchaos"] = item["LawChaos"]["S"]
mysfit["species"] = item["Species"]["S"]
mysfit["thumbImageUri"] = item["ThumbImageUri"]["S"]
mysfitList["mysfits"].append(mysfit)
# convert the create list of dicts in to JSON
return json.dumps(mysfitList)