Skip to content

SamAddy/fastapi_delivery_fee_calculator_endpoint

Repository files navigation

Playground CI Playground CD

Delivery Fee Calculator API

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.

Technology Stack

Installation

  1. Navigate to the root directory of the project in your terminal:

    cd delivery_fee_calculator/
  2. Create a virtual environment:

    python -m venv venv
  3. Activate the virtual environment:

    • On Windows:
      venv\Scripts\activate
    • On Unix or macOS:
      source venv/bin/activate
  4. Install dependencies:

    pip install -r requirements.txt
    

Usage

  1. Run the app once virtual environment is activated:

    uvicorn delivery_fee_calculator.main:app --reload
  2. API Documentation:

  3. 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}'
    

Request

Example:

{
  "cart_value": 790,
  "delivery_distance": 2235,
  "number_of_items": 4,
  "time": "2024-01-15T13:00:00Z"
}
Field details
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

Response

Example:

{
  "delivery_fee": 710
}
Field details
Field Type Description Example value
delivery_fee Integer Calculated delivery fee in cents. 710 (710 cents = 7.10€)

Project Structure

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