The Delivery Fee Calculator is a FastAPI project that helps customers estimate the delivery cost based on their shopping cart. The delivery fee is determined by factors such as cart value, number of items, order time, and delivery distance.
- FastAPI: A modern, fast (high-performance), web framework for building APIs with Python 3.7 based on standard Python type hints.
- Python: A popular and easy-to-use programming language
- uvicorn: An ASGI server to run the FastAPI application.
- Swagger UI and ReDoc: FastAPI provides interactive API documentation through Swagger UI (http://localhost:8000/docs) and ReDoc (http://localhost:8000/redoc).
-
Navigate to the root directory of the project in your terminal:
cd delivery_fee_calculator/
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On Unix or macOS:
source venv/bin/activate
- On Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Run the app once virtual environment is activated:
uvicorn delivery_fee_calculator.main:app --reload
-
API Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
-
The endpoint can also be tested using
requests
in CLI.>>> import requests >>> payload = { ... "cart_value": 790, ... "delivery_distance": 2235, ... "number_of_items": 4, ... "time": "2024-01-15T13:00:00Z" ... } >>> response = requests.post("http://localhost:8000/fees", json=payload) >>> response.text '{"delivery_fee":710}'
Example:
{
"cart_value": 790,
"delivery_distance": 2235,
"number_of_items": 4,
"time": "2024-01-15T13:00:00Z"
}
Field | Type | Description | Example value |
---|---|---|---|
cart_value | Integer | Value of the shopping cart in cents. | 790 (790 cents = 7.90€) |
delivery_distance | Integer | The distance between the store and customer’s location in meters. | 2235 (2235 meters = 2.235 km) |
number_of_items | Integer | The number of items in the customer's shopping cart. | 4 (customer has 4 items in the cart) |
time | String | Order time in UTC in ISO format. | 2024-01-15T13:00:00Z |
Example:
{
"delivery_fee": 710
}
Field | Type | Description | Example value |
---|---|---|---|
delivery_fee | Integer | Calculated delivery fee in cents. | 710 (710 cents = 7.10€) |
delivery_fee_calculator
│
│ delivery_fee_calculator/
│ │
│ │
│ ├── delivery_fee_calculator/
│ │ ├── __init__.py
│ │ ├── fee_calculator.py
│ │ ├── main.py
│ │ ├── schemas.py
│ │
│ │
│ ├── tests/
│ │ ├── __init__.py
│ │ ├── test_data.py
│ │ ├── test_fee_calculator.py
│ │ ├── test_main.py
│
├── README.md
├── redo_screenshot.png
├── requirements.txt
├── swagger_ui_screenshot