Created by: Saku Karttunen (22.01.-31.01.2024)
If you are trying to run the API on Windows, consider running inside the WSL2 terminal. Or through the Docker container. (I made it for guaranteeing compatibility between operating systems)
I built the API on my Linux machine and I cannot guarantee that it works the same way on Windows.
The assignment was to create a simple HTTP API Endpoint that calculates the delivery fee for an order. I used Python for this assignment and some fraweworks that I list below.
The API is a single POST endpoint that takes in a JSON object as a request body and returns a JSON object as a response.
The original assignment is in it's original repository.
- Python 3.11
- FastAPI - A Python framework for creating APIs
- Uvicorn - For running the API
- pytest - For testing the API
- Docker - For containerizing the API
- Requests - For making randomized requests to the API
- Run docker compose
docker-compose up
# or if you want to run it in the background
docker-compose up -d
The container has the port 8000
exposed, so you can access it from localhost:8000
.
I added a fake client that makes randomized order requests to the API. So you can just run the program and see the results. This "client" only makes 5 orders and then stops.
Once the container is running. You can make more requests to the
endpoint with curl
or the fake_client.py
script.
- Enter the virtual environment
# Linux / macOS
source venv/bin/activate
# Windows
.\venv\Scripts\activate
- Install the dependencies
pip install -r requirements.txt
- Run the server
uvicorn main:app
- Make a POST request to the endpoint
curl -X POST -H "Content-Type: application/json" -d '{"cart_value": 790, "delivery_distance": 2235, "number_of_items": 4, "time": "2024-01-15T13:00:00Z"}' http://127.0.0.1:8000
or use the fake client for randomized requests:
# linux / macOS
python3 fake_client.py
The tests are located in the tests
directory. I used FastAPI's TestClient
to test the API.
and simple pytest
compatible functions to create unit tests for all the small parts as well
as more general tests for the entirety of the API.
To run the tests, run the following command in the root directory of the project:
pytest