This FastAPI-based web application enables efficient car search functionality with specific technical criteria. The application provides comprehensive capabilities for managing car data, including addition, deletion, and search operations with both JSON and XML response formats. It is designed with a focus on performance, maintainability, and robust testing coverage.
The application requires the following technical components:
- Python 3.11
- PostgreSQL
- Ubuntu Operating System
Begin by updating your system packages and installing PostgreSQL:
sudo apt update
sudo apt install postgresql postgresql-contribConfigure the PostgreSQL user and database.
Create the required database table structure:
psql -d car_search -U abdul
# In PostgreSQL prompt:
CREATE TABLE cars (
id SERIAL PRIMARY KEY,
length FLOAT,
weight FLOAT,
velocity FLOAT,
color VARCHAR(255)
);
\qSet up your Python environment and install the necessary dependencies:
python3.11 -m venv venv
source venv/bin/activate
git clone <repository-url>
cd car_search
pip install -r requirements.txtCreate a .env file in the project root with the following configuration:
DB_USER=abdul
DB_PASSWORD=Rehman123
DB_NAME=car_search
DB_HOST=localhost
DB_PORT=5432
Create a pytest.ini file in the project root:
[pytest]
pythonpath = .Execute the test suite using the following commands:
# Run all tests
pytest tests/
# Run tests with coverage report
pytest --cov=src tests/Start the FastAPI server using:
uvicorn src.main:app --reloadThe application will be accessible at http://localhost:8000
curl -X POST "http://localhost:8000/api/v1/cars" \
-H "Content-Type: application/json" \
-d '{"length": 4.5, "weight": 1500, "velocity": 200, "color": "red"}'curl -X POST "http://localhost:8000/api/v1/cars/search/" -H "Content-Type: application/json" -d \
'{
"min_length": 4.0,
"max_length": 5.0,
"min_weight": 1200,
"max_weight": 1800,
"color": "red"
}''curl -X POST "http://localhost:8000/api/v1/cars/search/xml" -H "Content-Type: application/json" -d
'{
"min_velocity": 150,
"max_velocity": 250,
"color": "blue"
}'curl -X DELETE "http://localhost:8000/api/v1/cars/{car_id}"The application follows a modular architecture designed for maintainability and scalability:
car_search/
├── src/
│ ├── api/
│ │ └── v1/
│ │ └── router.py
│ ├── models/
│ │ └── car.py
│ ├── schemas/
│ │ └── car.py
│ ├── services/
│ │ └── car_service.py
│ └── main.py
├── config/
│ ├── database.py
│ └── settings.py
├── tests/
│ ├── conftest.py
│ ├── test_api.py
│ └── test_services.py
├── requirements.txt
└── .env
The application provides comprehensive API documentation through two interfaces:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc