A RESTful API for managing movie watchlists built with FastAPI. Users can retrieve, add, and remove movies from their personal watchlists with full validation and error handling.
- Retrieve user's watchlist with full movie details
- Add movies to watchlist with duplicate prevention
- Remove movies from watchlist
- Input validation using Pydantic models
- Asynchronous operations with asyncio
- Comprehensive unit tests with pytest
- Docker support for easy deployment
- FastAPI - Modern, fast web framework for building APIs
- Pydantic - Data validation using Python type annotations
- Python 3.11 - Asynchronous programming with asyncio
- pytest - Testing framework
- Docker - Containerization
fastapi-movie-watchlist/
├── main.py # FastAPI application and endpoints
├── movies.json # Movie data
├── requirements.txt # Python dependencies
├── Dockerfile # Docker image configuration
├── docker-compose.yml # Docker Compose configuration
├── .dockerignore # Docker ignore rules
├── tests/
│ └── test_main.py # Unit tests
└── README.md
GET /moviesReturns a list of all available movies.
Response: 200 OK
[
{
"id": "tt0111161",
"title": "The Shawshank Redemption",
"year": 1994,
"genres": ["drama"],
"plot": "Two imprisoned men bond over a number of years..."
}
]GET /watchlist/{user_id}Retrieves all movies in a user's watchlist.
Response: 200 OK
[
{
"id": "tt0111161",
"title": "The Shawshank Redemption",
"year": 1994,
"genres": ["drama"],
"plot": "Two imprisoned men bond over a number of years..."
}
]POST /watchlist/{user_id}Request Body:
{
"movie_id": "tt0111161"
}Response: 200 OK
{
"message": "Movie added successfully",
"watchlist": [...]
}Error Responses:
404 Not Found- Movie does not exist400 Bad Request- Movie already in watchlist
DELETE /watchlist/{user_id}/{movie_id}Response: 200 OK
{
"message": "Movie removed successfully",
"watchlist": [...]
}Error Responses:
404 Not Found- User has no watchlist or movie not in watchlist
- Python 3.11+
- pip
-
Clone the repository
git clone https://github.com/rodrick-mpofu/fastapi-movie-watchlist.git cd fastapi-movie-watchlist -
Create and activate virtual environment
# Create virtual environment python -m venv venv # Activate (Windows Git Bash) source venv/Scripts/activate # Activate (Mac/Linux) source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Run the application
uvicorn main:app --reload
-
Access the API
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- Alternative docs: http://localhost:8000/redoc
- Docker
- Docker Compose
-
Build and run the container
docker compose up --build
-
Access the API
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
-
Stop the container
# Press Ctrl+C, then: docker compose down
# Run all tests
pytest
# Run with verbose output
pytest -v
# Run specific test file
pytest tests/test_main.py -v# Get all movies
curl http://localhost:8000/movies
# Get user's watchlist
curl http://localhost:8000/watchlist/user_123
# Add movie to watchlist
curl -X POST "http://localhost:8000/watchlist/user_123" \
-H "Content-Type: application/json" \
-d '{"movie_id": "tt0111161"}'
# Remove movie from watchlist
curl -X DELETE "http://localhost:8000/watchlist/user_123/tt0111161"import requests
BASE_URL = "http://localhost:8000"
# Get all movies
response = requests.get(f"{BASE_URL}/movies")
movies = response.json()
# Add movie to watchlist
response = requests.post(
f"{BASE_URL}/watchlist/user_123",
json={"movie_id": "tt0111161"}
)
result = response.json()- Clean, modular code structure
- Comprehensive error handling
- Type hints throughout
- Async/await patterns for I/O operations
- Unit tests for all endpoints
- Edge case coverage
- Database reset between tests
- Persistent database (PostgreSQL/MongoDB)
- User authentication and authorization
- Movie rating and review system
- Search and filter movies
- Pagination for large watchlists
This project is part of a take-home coding challenge.
Rodrick Mpofu