/request-stream-api

A FastAPI-based application for handling user requests efficiently, utilizing asynchronous programming, PostgreSQL, Kafka, and Redis for high performance and scalability. Not intended for production use.

Primary LanguagePython

Request Stream API

Overview

Request Stream API is a FastAPI-based application designed to handle user requests efficiently. It utilizes asynchronous programming to ensure high performance and scalability. The API supports various operations such as creating, updating, retrieving, and deleting applications.

Features

  • Asynchronous request handling using FastAPI
  • PostgreSQL as the database with SQLAlchemy ORM
  • Kafka for message queuing
  • Redis for caching
  • Alembic for database migrations
  • Comprehensive error handling
  • Detailed logging with Structlog

Requirements

To run the application, it is sufficient to use Docker.

Technologies Used:

  • Python 3.12 or higher
  • FastAPI
  • PostgreSQL
  • Kafka
  • Redis

Installation

  1. Clone the repository:

    git clone https://github.com/ACK1D/request-stream-api.git
    cd request-stream-api
  2. Using Docker: To run the application using Docker, you can use the provided compose.yml file. Make sure to have Docker and Docker Compose installed.

    • Copy the example environment file:

      cp .env.example .env

    Build and start the containers:

    docker compose -f docker/compose.yml up --build -d

    Access the API documentation at http://localhost:8000/docs.

  3. Using Poetry: If you prefer to run the application locally without Docker, you can use Poetry for dependency management.

    • Install Poetry if you haven't already:

      curl -sSL https://install.python-poetry.org | python3 -
    • Create a virtual environment and install dependencies:

      poetry install
    • Set up the environment variables. Create a .env file in the root directory with the following content:

      POSTGRES_SERVER=localhost
      POSTGRES_USER=your_user
      POSTGRES_PASSWORD=your_password
      POSTGRES_DB=applications
      KAFKA_BOOTSTRAP_SERVERS=localhost:9092
      REDIS_URL=redis://localhost:6379
    • Run the database migrations:

      poetry run alembic upgrade head
    • Start the application using Granian:

      poetry run granian --interface asgi --host 0.0.0.0 --port 8000 src.main:app

API Endpoints

Applications

  • Create Application

    • POST /api/v1/applications/
    • Request body: {"user_name": "string", "description": "string"}
    • Response: 201 Created with application details.
  • Get Application by ID

    • GET /api/v1/applications/{application_id}
    • Response: 200 OK with application details or 404 Not Found.
  • Get Applications List

    • GET /api/v1/applications/
    • Query parameters: page, size, user_name
    • Response: 200 OK with a list of applications.
  • Update Application

    • PUT /api/v1/applications/{application_id}
    • Request body: {"user_name": "string", "description": "string"}
    • Response: 200 OK with updated application details or 404 Not Found.
  • Partially Update Application

    • PATCH /api/v1/applications/{application_id}
    • Request body: {"description": "string"}
    • Response: 200 OK with updated application details or 404 Not Found.
  • Delete Application

    • DELETE /api/v1/applications/{application_id}
    • Response: 204 No Content or 404 Not Found.
  • Check Application Exists

    • HEAD /api/v1/applications/{application_id}
    • Response: 200 OK if the application exists or 404 Not Found.

Testing

To run the tests, use:

pytest