Skip to content

A simple API using Python FastAPI and Strawberry.

Notifications You must be signed in to change notification settings

soraia653/Movies-API

Repository files navigation

Simple API using Python and Strawberry

Implemented a simple API that fetches movie data using the OMDB API.

Coverage

Requirements

  • Python 3.9 or higher
  • Docker and Docker Compose (optional)

Configuration

To run this project, you will need to update the following variables in the app/settings.py file.

  • OMDB_API_KEY

  • HOST

  • PORT

Installation

You can configure the API by modifying the settings in the app/settings.py file.

  • Clone this repository
  git clone https://github.com/soraia653/movies-api.git
  cd movies-api
  • Install Poetry:
  curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
  • Install project dependencies using Poetry:
  poetry install

Usage

Running Locally

  • Start the application:
  python app/main.py

You can access the GraphQL interface at http://localhost:8000/graphql.

Running with Docker Compose

To run the application using Docker Compose, navigate to the root of the project and run:

  docker-compose up -d

You can access the GraphQL interface at http://localhost:8000/graphql.

To stop the running containers, use CTRL + C or run:

  docker-compose down

Running Tests

To run tests, run the following command:

  pytest

This will run all existing tests in the tests folder.

Examples

  • Example query:
query getMovies {
  getMovie(title: "Harry Potter", first: 2) {
    edges {
      node {
        id
        imdbID
        Title
        Type
        Poster
      }
    }
    pageInfo {
      hasNextPage
    }
  }
}
  • Expected Result:
{
  "data": {
    "getMovie": {
      "edges": [
        {
          "node": {
            "id": 1,
            "imdbID": "tt1201607",
            "Title": "Harry Potter and the Deathly Hallows: Part 2",
            "Type": "movie",
            "Poster": "https://m.media-amazon.com/images/M/MV5BMGVmMWNiMDktYjQ0Mi00MWIxLTk0N2UtN2ZlYTdkN2IzNDNlXkEyXkFqcGdeQXVyODE5NzE3OTE@._V1_SX300.jpg"
          }
        },
        {
          "node": {
            "id": 2,
            "imdbID": "tt0241527",
            "Title": "Harry Potter and the Sorcerer's Stone",
            "Type": "movie",
            "Poster": "https://m.media-amazon.com/images/M/MV5BNmQ0ODBhMjUtNDRhOC00MGQzLTk5MTAtZDliODg5NmU5MjZhXkEyXkFqcGdeQXVyNDUyOTg3Njg@._V1_SX300.jpg"
          }
        }
      ],
      "pageInfo": {
        "hasNextPage": true
      }
    }
  }
}

Lessons Learned

I decided to use Strawberry instead of Graphene since it is the recommended library as per FastAPI documentation.

While working on this project, one of the steps was applying Pagination to the query. I quickly realized that Strawberry's documentation had many errors and their tutorial did not work. I spent some time on this issue and was able to fix.

I ended up opening a PR request w/ Strawberry to fix their documentation.

The moral of the story is that with one simple project I ended up contributing to an open-source project (albeit a very small contribution) and perhaps helping someone use this library.

About

A simple API using Python FastAPI and Strawberry.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published