This is a FastAPI-based application that calculates delivery order prices, including delivery fees and small order surcharges, based on venue and user location details. The application fetches venue data from an external API and calculates distances using the Haversine formula.
- Calculate total delivery order price.
- Includes delivery fees, small order surcharges, and cart value.
- Uses FastAPI for a robust and fast web framework.
- Validates requests and responses using Pydantic models.
- Fetches external venue data for dynamic calculations.
- Python 3.10 or higher.
- Virtual environment for dependency management.
-
Clone the repository:
git clone <repository-url> cd <repository-folder>
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the required dependencies:
pip install -r requirements.txt
-
Navigate to the project directory:
cd <repository-folder>
-
Start the FastAPI application using Uvicorn:
uvicorn app.main:app --reload
-
Access the application in your browser or API client:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc Documentation: http://127.0.0.1:8000/redoc
- Root Endpoint: http://127.0.0.1:8000/
GET /api/v1/delivery-order-price
| Parameter | Type | Description | Example |
|---|---|---|---|
venue_slug |
string | Unique identifier for the venue | home-assignment-venue-helsinki |
cart_value |
integer | Total value of items in the cart | 1000 |
user_lat |
float | Latitude of the user's location | 60.17094 |
user_lon |
float | Longitude of the user's location | 24.93087 |
curl "http://localhost:8000/api/v1/delivery-order-price?venue_slug=home-assignment-venue-helsinki&cart_value=1000&user_lat=60.17094&user_lon=24.93087"{
"total_price": 1190,
"small_order_surcharge": 0,
"cart_value": 1000,
"delivery": {
"fee": 190,
"distance": 177
}
}Finland: home-assignment-venue-helsinki
Sweden: home-assignment-venue-stockholm
Germany: home-assignment-venue-berlin
Japan: home-assignment-venue-tokyo
project-folder/
├── app/
│ ├── main.py # FastAPI entry point
│ ├── config.py # Configuration settings
│ ├── routers/
│ │ ├── delivery.py # API routes for delivery calculations
│ ├── services/
│ │ ├── venue_service.py # Handles external API requests
│ ├── utils/
│ ├── delivery_calculations.py # Delivery logic
├── tests/
│ ├── test_delivery.py # Unit tests
├── requirements.txt # Dependencies
├── README.md # Project documentation
-
Install
pytestif not already installed:pip install pytest
-
Run the tests:
pytest
Future improvements could include:
- Adding caching for external API calls.
- Implementing advanced error logging.
- Integrating with a database for persistent storage.
- Adding more comprehensive test cases.