-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update single get function to be more robust
- Loading branch information
Showing
1 changed file
with
39 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,47 @@ | ||
import json | ||
import boto3 | ||
from botocore.vendored import requests | ||
import os | ||
from boto3.dynamodb.conditions import Key, Attr | ||
|
||
# always start with the lambda_handler | ||
|
||
|
||
# always start with the lambda_handler | ||
def lambda_handler(event, context): | ||
|
||
OMDB = os.environ["OMDBkey"] | ||
|
||
# make the connection to dynamodb | ||
# may require parameters if not using default AWS environment vars | ||
dynamodb = boto3.resource('dynamodb') | ||
|
||
table = dynamodb.Table('movies') | ||
# get item from database | ||
items = table.get_item( | ||
Key={"title": event["queryStringParameters"]["title"], "year": event["queryStringParameters"]["year"]}) | ||
items = items['Item'] | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps(items) | ||
} | ||
dynamodb = boto3.resource( | ||
"dynamodb" | ||
) # may require parameters if not using default AWS environment vars | ||
|
||
table = dynamodb.Table("movies") | ||
|
||
movie = requests.get( | ||
"https://www.omdbapi.com/?apikey=" | ||
+ OMDB | ||
+ "&t=" | ||
+ event["queryStringParameters"]["title"] | ||
+ "&y=" | ||
+ event["queryStringParameters"]["year"] | ||
) | ||
|
||
if movie.json()["Response"] == "True": | ||
|
||
# get item from database | ||
items = table.get_item( | ||
Key={"title": movie.json()["Title"], "year": movie.json()["Year"]} | ||
) | ||
return {"statusCode": 200, "body": json.dumps(items["Item"])} | ||
elif ( | ||
movie.json()["Response"] == "False" | ||
and movie.json()["Error"] == "Daily request limit reached!" | ||
): | ||
return {"statusCode": 429, "body": "OMDB API request limit reached!"} | ||
elif ( | ||
movie.json()["Response"] == "False" | ||
and movie.json()["Error"] == "Movie not found!" | ||
): | ||
return {"statusCode": 404, "body": "Movie Not Found!"} | ||
else: | ||
return {"statusCode": 520, "body": "Unknown Error!"} |