Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vehicle API

A REST API for managing vehicle records. Built with Flask and SQLite.

What It Does

This API lets you create, read, update, and delete vehicle records. Each vehicle has a VIN (vehicle identification number) that uniquely identifies it.

Setup

Requirements

  • Python 3.8+
  • pip

Installation

# install dependencies
pip install -r requirements.txt

# run the application
python app.py

The server will start at http://localhost:5000.

API Endpoints

Get all vehicles

GET /vehicle

Returns a list of all vehicles in the database.

Example:

curl http://localhost:5000/vehicle

Create a vehicle

POST /vehicle

Request body (all fields required):

{
    "vin": "ABC12345678901234",
    "manufacturer_name": "toyota",
    "description": "reliable sedan",
    "horse_power": 200,
    "model_name": "camry",
    "model_year": 2023,
    "purchase_price": 25000.50,
    "fuel_type": "gasoline"
}

Example:

curl -X POST http://localhost:5000/vehicle \
  -H "Content-Type: application/json" \
  -d '{"vin":"ABC12345678901234","manufacturer_name":"toyota","description":"reliable sedan","horse_power":200,"model_name":"camry","model_year":2023,"purchase_price":25000.50,"fuel_type":"gasoline"}'

Get a specific vehicle

GET /vehicle/{vin}

Example:

curl http://localhost:5000/vehicle/ABC12345678901234

Note: VIN lookup is case-insensitive.

Update a vehicle

PUT /vehicle/{vin}

Request body should include all fields. The VIN in the body must match the VIN in the URL.

Example:

curl -X PUT http://localhost:5000/vehicle/ABC12345678901234 \
  -H "Content-Type: application/json" \
  -d '{"vin":"ABC12345678901234","manufacturer_name":"toyota","description":"updated description","horse_power":250,"model_name":"camry","model_year":2023,"purchase_price":30000.00,"fuel_type":"gasoline"}'

Delete a vehicle

DELETE /vehicle/{vin}

Example:

curl -X DELETE http://localhost:5000/vehicle/ABC12345678901234

Error Responses

The API returns different HTTP status codes depending on what went wrong:

  • 400 Bad Request - The JSON you sent is malformed or missing
  • 404 Not Found - The vehicle with that VIN doesn't exist
  • 422 Unprocessable Entity - Your data is invalid (missing fields, wrong types, etc.)

When there's an error, you'll get a JSON response with details:

{
    "errors": ["vin must be 17 characters", "horse power must be a positive integer"]
}

Data Validation

The API checks your data before saving:

  • VIN must be exactly 17 characters
  • All fields are required
  • Horse power must be a positive integer
  • Model year must be between 1900 and 2026
  • Purchase price must be a positive number

VINs are case-insensitive and stored in uppercase.

Running Tests

pytest tests/test_api.py -v

The test suite includes 22 tests covering all endpoints and error cases.

Project Structure

vehicle-api/
├── app.py              # main application and API routes
├── models.py           # vehicle data model and validation
├── database.py         # database setup
├── requirements.txt    # python dependencies
└── tests/
    └── test_api.py    # test suite

Database

The app uses SQLite with a file called vehicles.db in the project directory. It's created automatically when you first run the application.

Notes

  • The VIN field is case-insensitive
  • You cannot change a vehicle's VIN through the update endpoint. If you need a different VIN, delete the old record and create a new one.
  • The server runs in debug mode by default. For production use, you should disable debug mode in app.py.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages