Skip to content

Commit

Permalink
Update single get function to be more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
luhann committed Mar 18, 2024
1 parent 0d7470d commit 8f4d00e
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions moviedb-single.py
100644 → 100755
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!"}

0 comments on commit 8f4d00e

Please sign in to comment.