Skip to content

A Flask based API that allows users to register, login & book train tickets along with API key-based authentication for admins.

Notifications You must be signed in to change notification settings

BelieveInTheLimitless/IRCTC-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IRCTC API

A Flask based API that allows users to register, login & book train tickets along with API key-based authentication for admins.

Table of Contents

Features

  • Admin privileges - Add, Update, Delete train/s
  • User privileges - Register, Login, Check Seat Availability, Book Seat/s, Check Booking info
  • Optimized for handling race conditions by holding a lock for ongoing transaction
  • Admin API endpoints are secured by API Key which are known to Admin and Host
  • JWT-based authentication token system for user login with 1 hour expiration window

Technologies Used

  • Python3 (Prerequisite)
  • Flask
  • Flask-SQLAlchemy
  • Flask-JWT-Extended
  • MySQL (Prerequisite)
  • dotenv for environment variable management

Setup Instructions

1. Clone the repository

git clone https://github.com/BelieveInTheLimitless/IRCTC-API
cd IRCTC-API

2. Creating the Database

#Login to the database
mysql -u root -p
#Create database
CREATE DATABASE railway_db;
#Create user
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
#Grant privileges
GRANT ALL PRIVILEGES ON railway_db.* TO 'user'@'localhost';
#Update privileges
FLUSH PRIVILEGES;
#Exit
EXIT;

3. Creating a virtual environment

# Common
python3 -m venv .venv
# For Linux
source .venv/bin/activate
# For Windows
.venv\Scripts\activate

4. Installing the required packages

pip3 install -r requirements.txt

5. Running the application

python3 app.py

API Endpoints

Admin API Endpoints

  • Add Train
    POST /admin/add_train

    • Request Headers
      ADMIN-API-KEY: <API_KEY>
    • Request Body
      {
        "name": "Express Train",
        "source": "Pune",
        "destination": "Thane",
        "total_seats": 100
      }
    • Response
      {
        "message": "Train added successfully"
        "train_id": 1
      }
  • Update Train
    PUT /admin/update_train/<train_id>

    • Request Headers
      ADMIN-API-KEY: <API_KEY>
    • Request Body
      {
        "name": "New Express Train",
        "source": "Mumbai",
        "destination": "Delhi",
        "total_seats": 200
      }
    • Response
      {
        "message": "Train updated successfully"
      }
  • Delete Train
    DELETE /admin/delete_train/<train_id>

    • Request Headers
      ADMIN-API-KEY: <API_KEY>
    • Response
      {
        "message": "Train deleted successfully"
      }
    • Error Response (if train not found)
      {
        "message": "Train not found"
      }

User API Endpoints

  • Register User
    POST /register

    • Request Body
      {
        "username": "john_doe",
        "password": "securepassword"
      }
    • Response
      {
        "message": "User registered successfully"
      }
  • Login User
    POST /login

    • Request Body
      {
        "username": "john_doe",
        "password": "securepassword"
      }
    • Response
      {
        "access_token": "<JWT_TOKEN>"
      }
  • Check Seat Availability
    GET /trains/availability

    • Request Headers
      Authorization: Bearer <JWT_TOKEN>
    • Request Params
      • source: "Pune"
      • destination: "Thane"
    • Response
      [
        {
          "train_id": 1,
          "train_name": "Express Train",
          "available_seats": 50
        }
      ]
  • Book Seat
    POST /user/bookings

    • Request Headers
      Authorization: Bearer <JWT_TOKEN>
    • Request Body
      {
        "train_id": 1
      }
    • Response
      {
        "message": "Seat booked successfully",
        "booking_id": 101
      }
  • Get User Bookings
    GET /user/bookings/info

    • Request Headers
      Authorization: Bearer <JWT_TOKEN>
    • Response
      [
        {
          "booking_id": 101,
          "train_name": "Express Train",
          "source": "Pune",
          "destination": "Thane",
          "seat_number": 50
        }
      ]

About

A Flask based API that allows users to register, login & book train tickets along with API key-based authentication for admins.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages