Eliro
Eliro

Reputation: 221

Basic movie database interacting with external API

In job offers I often see the word "REST-API". So, I'd like to learn what REST-API actually is. I have some experience in Django. Well, I'm a beginner actually.

I found on GitHub a simple task: https://github.com/netguru/python-recruitment-task The only thing I don't understand is how to do this:

POST /movies:
    Request body should contain only movie title, and its presence should be validated.
    Based on passed title, other movie details should be fetched from http://www.omdbapi.com/ (or other similar, public movie database) - and saved to application database.
    Request response should include full movie object, along with all data fetched from external API.

How should it looks like?

How to validate the "presence"?

How to download data from public databases?

Upvotes: 1

Views: 693

Answers (2)

Carlos Damázio
Carlos Damázio

Reputation: 104

How should it looks like?

Maybe you'll be using Django to make this task, but you should understand the concept with the following and, probably, might adapt:

import sys

import requests


API_KEY = '<API_KEY>'


def insert_to_db():
    raise NotImplementedError("Implement this function by yourself.")


if __name__ == "__main__":
    title = '+'.join([arg for arg in sys.argv[1:]])

    # Check if title is found in argument list. It's a validation.
    if not title:
        raise IndexError("No title entered!")

    # The request itself.
    response = requests.get(f"http://www.omdbapi.com/?t={title}&apikey={API_KEY}").json()
    if response:
        print(response)

This is a simple program, it reads your input (the movie title) through any terminal you're using, makes a simple request (using requests module) to your API and fetches a response in JSON. This is it's output:

$ python api.py city of god   
{'Title': 'City of God', 'Year': '2002', 'Rated': 'R', 'Released': '13 Feb 2004', 'Runtime': '130 min', 'Genre': 'Crime, Drama', 'Director': 'Fernando Meirelles, Kátia Lund(co-director)', 'Writer': 'Paulo Lins (novel), Bráulio Mantovani (screenplay)', 'Actors': 'Alexandre Rodrigues, Leandro Firmino, Phellipe Haagensen, Douglas Silva', 'Plot': "In the slums of Rio, two kids' paths diverge as one struggles to become a photographer and the other a kingpin.", 'Language': 'Portuguese', 'Country': 'Brazil, France, Germany', 'Awards': 'Nominated for 4 Oscars. Another 66 wins & 38 nominations.', 'Poster': 'https://m.media-amazon.com/images/M/MV5BMGU5OWEwZDItNmNkMC00NzZmLTk1YTctNzVhZTJjM2NlZTVmXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_SX300.jpg', 'Ratings': [{'Source': 'Internet Movie Database', 'Value': '8.6/10'}, {'Source': 'Rotten Tomatoes', 'Value': '91%'}, {'Source': 'Metacritic', 'Value': '79/100'}], 'Metascore': '79', 'imdbRating': '8.6', 'imdbVotes': '639,695', 'imdbID': 'tt0317248', 'Type': 'movie', 'DVD': '08 Jun 2004', 'BoxOffice': 'N/A', 'Production': 'Miramax Films', 'Website': 'http://www.miramax.com/movie/city-of-god', 'Response': 'True'}

How to validate the "presence"?

Analyzing the program should clear your doubts.

    title = '+'.join([arg for arg in sys.argv[1:]])

    # Check if title is found in argument list. It's a validation.
    if not title:
        raise IndexError("No title entered!")

This part of the code checks if a title is passed to your request. This is called validation and when you're dealing with APIs, it's important to clean the user input data before interacting with it, otherwise, it can mess up your API with unexpected behavior.

How to download data from public databases?

This is a general question, you might need to explain further what you're trying to download. Databases varies, and APIs interface the communication with the DB by executing queries on your behalf. You might need to elaborate on that.

But downloading a JSON file with the API in question is really simple. If you're using Linux and you're kind of familiar with curl, just make a request with it and redirect it's output to a file:

curl "http://www.omdbapi.com/?t=the+professional&apikey=<API_KEY>" >> out.json

With this example, you might relate an example of how to deal with external APIs, then it's up to you.

Upvotes: 3

Valentin Garreau
Valentin Garreau

Reputation: 1073

your question is very big. First of all i would recommend you to check some blog about API REST like this one

Then before coding, you can try some request directly with Postman, for understand how the post & request work.

And when this job is finish, take a look to django DRF (django rest framework), that do all the work for you :)

Upvotes: 0

Related Questions