Last active
February 23, 2018 09:29
-
-
Save smoothml/72ceef858703e0f96b63b8bb995a1daa to your computer and use it in GitHub Desktop.
2018-02-23 Serverless computing handle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def handle(event, context): | |
# initialise DynamoDB client | |
dynamodb = boto3.client('dynamodb', 'eu-west-2') | |
# perform API call | |
response = requests.get("http://api.fixer.io/latest?base=GBP") | |
# handle bad responses | |
if response.status_code == 200: | |
logger.info("Request to Fixer successful") | |
else: | |
logger.info("Response status code {}".format(response.status_code)) | |
return {"success": False, "status_code": response.status_code} | |
# format JSON response into format required by DynamoDB | |
response_json = reformat_json(response.json()) | |
# put item into DynamoDB table | |
dynamodb.put_item(TableName="exchange-rates-table", | |
Item=response_json) | |
# return successful response | |
return {"success": True, "status_code": response.status_code} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment