A simple Flask API that serves a random Rick and Morty quote from quotes.json.
You can run this project either with Docker (recommended) or directly with Python.
- Docker
- Docker Compose (v2)
- Python 3.10+
- pip
Build and run the container:
docker build -t rick-morty-python-api .
docker run --rm -p 5000:5000 rick-morty-python-apiThen call the API:
curl http://localhost:5000/Optional query param:
curl "http://localhost:5000/?seed=123"docker compose up --buildThen call the API:
curl http://localhost:8888/Optional query param:
curl "http://localhost:8888/?seed=123"python3 -m venv .venv
. .venv/bin/activate
pip install -r requirements.txt
PYTHONPATH=src python -m app.appThen call the API:
curl http://localhost:5000/GET /returns a random quoteGET /healthreturns a simple health checkPOST /reloadreloadsquotes.json
Examples:
curl http://localhost:5000/health
curl -X POST http://localhost:5000/reloadClient -> Flask API (src/app/app.py)
|
+-> QuoteStore (src/app/quotes.py)
|
+-> quotes.json
All responses include:
source: API identifierrequest_id: request correlation id
GET / also includes:
quote: the quote textcount: total quote count
Sample response:
{
"quote": "Wubba Lubba Dub-Dub!",
"count": 42,
"source": "rick-morty",
"request_id": "6f5d2f9a-0c70-4c15-9c79-2a2a0b4f2e5f"
}Environment variables:
QUOTES_PATH: path toquotes.json(default:quotes.json)QUOTES_AUTO_RELOAD: auto-reload if file changes (true/false, default:false)DEBUG: Flask debug mode (true/false, default:false)PORT: server port (default:5000)
To set a custom request id, pass the X-Request-Id header.
src/app: Flask app packagetests: pytest testsquotes.json: quote data
Configs are in pyproject.toml. Install tools if you want to run them locally:
python -m pip install black ruff
black src tests
ruff check src testspython -m pip install -r requirements.txt
pytest