Toll System API implementation
This project provides a RESTful Web API and a Command-Line Interface (CLI) for managing interoperability of toll stations on highways. It allows administrators and users to interact with toll-related data such as vehicle passes, station information, and charges. It also includes a web application (HTML) as a use case.
The system consists of:
- A RESTful API built with Flask, handling toll station data and user authentication.
- A CLI tool for command-line interaction with the API.
- A MySQL database to store all toll station and pass information.
- JWT-based authentication for user access control.
The REST API is hosted at:
http://localhost:9115/api
- Endpoint:
POST /login - Request:
curl -X POST http://localhost:9115/api/login -d "username=admin&password=adminpass" - Response:
{ "token": "your-access-token" }
- Endpoint:
POST /logout - Request:
curl -X POST http://localhost:9115/api/logout -H "X-OBSERVATORY-AUTH: Bearer your-access-token"
- Endpoint:
GET /admin/healthcheck - Response:
{ "status": "OK", "dbconnection": "Connected", "n_stations": 10, "n_passes": 100 }
- Endpoint:
POST /admin/resetstations - Response:
{ "status": "OK" }
- Endpoint:
POST /admin/resetpasses - Response:
{ "status": "OK" }
- Endpoint:
POST /admin/addpasses - Request:
curl -X POST -H "X-OBSERVATORY-AUTH: Bearer your-access-token" \ -F "file=@passes.csv" http://localhost:9115/api/admin/addpasses
- Response:
{ "status": "OK" }
- List Users:
GET /admin/users - Create/Modify User:
POST /admin/usermod{ "username": "newuser", "password": "newpass", "operator": "operator1" }
- Endpoint:
GET /tollStationPasses/:tollStationID/:date_from/:date_to - Example Request:
curl -H "X-OBSERVATORY-AUTH: Bearer your-access-token" \ http://localhost:9115/api/tollStationPasses/NAO01/20240101/20240131 - Response:
{ "stationID": "NAO01", "nPasses": 3, "passList": [ { "passID": "P001", "timestamp": "2024-01-10 12:00", "passCharge": 1.5 } ] }
- Endpoint:
GET /passAnalysis/:stationOpID/:tagOpID/:date_from/:date_to - Response:
{ "stationOpID": "O1", "tagOpID": "O2", "nPasses": 10, "passList": [ { "passID": "P001", "stationID": "S001", "passCharge": 2.5 } ] }
- Endpoint:
GET /passesCost/:tollOpID/:tagOpID/:date_from/:date_to - Response:
{ "nPasses": 50, "passesCost": 125.5 }
- Endpoint:
GET /chargesBy/:tollOpID/:date_from/:date_to - Response:
{ "tollOpID": "O1", "vOpList": [ { "visitingOpID": "O2", "nPasses": 20, "passesCost": 50.0 } ] }
The CLI provides an alternative way to interact with the API.
Make sure you have cli.py in your project directory and run:
chmod +x cli.pyUsage:
./cli.py <command> [options]./cli.py login --username admin --password adminpass./cli.py logout./cli.py healthcheck./cli.py resetpasses./cli.py resetstations./cli.py admin --users./cli.py admin --addpasses --source passes.csv./cli.py tollstationpasses --station NAO01 --from 20240101 --to 20240131 --format json./cli.py passanalysis --stationop O1 --tagop O2 --from 20240101 --to 20240131 --format json./cli.py passescost --stationop O1 --tagop O2 --from 20240101 --to 20240131 --format csv./cli.py chargesby --opid O1 --from 20240101 --to 20240131 --format csvThe system uses a MySQL database with the following tables:
| id | username | password_hash | operator |
|---|
| toll_id | name | operator |
|---|
| id | timestamp | toll_id | tag_ref | tag_home_id | charge |
|---|
- Python 3.8+
- MySQL Server
- Flask, Flask-JWT-Extended, Flask-CORS, Flask-Bcrypt, MySQL-Connector
pip install flask flask-jwt-extended flask-cors flask-bcrypt mysql-connector-pythonCREATE DATABASE toll_system;
USE toll_system;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
operator VARCHAR(50)
);
CREATE TABLE toll_stations (
toll_id VARCHAR(10) PRIMARY KEY,
name VARCHAR(50),
operator VARCHAR(50)
);
CREATE TABLE passes (
id INT AUTO_INCREMENT PRIMARY KEY,
timestamp DATETIME,
toll_id VARCHAR(10),
tag_ref VARCHAR(50),
tag_home_id VARCHAR(50),
charge FLOAT
);python app.pyThis front-end application displays toll station data through a web interface. Users can select a time period and a toll station, then download the data in CSV or JSON format.
- React: JavaScript library for building dynamic user interfaces.
- React Router: Used for page navigation within the application.
- CSS: Styles for the application's appearance.
- Date Range Selection: Users can choose a start and end date.
- Toll Station Selection: Users can select from a list of available toll stations.
- Data Download: Users can download the data in CSV or JSON format.
- Access Protection: Users must be logged in with a valid token to use the application.
git clone https://github.com/username/project-name.git
cd project-namenpm installnpm startThe application will be available at http://localhost:3000.
- When users log into the application, they must select a time range (start and end date).
- After selecting the date range, users choose a toll station from the dropdown list.
- Users can download data for the selected station and time period in CSV or JSON format.
- Authorization: Users must be logged in with a valid username and token (stored locally in localStorage).
- API Requests:
- Fetch available toll stations from the API.
- Retrieve toll station data based on the selected time period.
- File Download: When a user selects a file format (CSV or JSON), the data is downloaded with a filename containing the username and selected dates.
- The application uses localStorage to store login details (username and token).
- If a user is not logged in or the token is invalid, they will be redirected to the login page.