Skip to main content

FastAPI dependency for batch requesting

Project description

FastAPI Batch 🌟

FastAPI Batch is a Python library that simplifies batching multiple API requests into a single request. Built on top of FastAPI and Pydantic, it empowers developers to optimize API interactions while maintaining flexibility and performance.

Motivation 💡

In modern web development, making multiple independent API requests can lead to higher overhead, increased latency, and slower overall performance. FastAPI Batch was created to address these issues by enabling developers to group multiple requests into a single batch request. This reduces the number of round trips between the client and server, optimizing performance by executing requests in parallel, and improving the user experience.

Installation 🚀

You can install FastAPI Batch using one of the following methods:

Using UV

uv add fastapi-batch

Using Poetry

poetry add fastapi-batch

Using Pip

pip install fastapi-batch

Features ✨

  • Batch API Requests 📦: Combine multiple API calls into a single request to reduce overhead.
  • Parallel Request Execution ⚡: Automatically execute batched requests in parallel for better performance.
  • Customizable Headers 📝: Override or define custom headers for individual requests in a batch.
  • Lightweight & Flexible 🪶: Easily integrates with existing FastAPI applications.
  • Seamless Response Handling 💨: Structured responses for each request in the batch.

Quick Start 🛠

Here’s how to set up FastAPI Batch in your project:

Example Usage

Define your API endpoints:

from fastapi_batch import BatchGateway, BatchResponse

@app.post("/api/v1/fruit")
async def identify_fruit(fruit: FruitModel):
    return {
        "message": f"{fruit.name} is a fruit and is{' ' if fruit.poisonous else ' not '}poisonous."
    }

@app.post("/api/v1/animal")
async def identify_animal(animal: AnimalModel):
    return {
        "message": f"{animal.name} is an animal and is{' ' if animal.carnivore else ' not '}a carnivorous."
    }

@app.post("/api/v1/batch")
async def batch_endpoint(gateway: BatchGateway) -> BatchResponse:
    return await gateway.execute()

Sample Batch Request

Send multiple requests to the /api/v1/batch endpoint in a single payload. You can override headers or customize them for individual requests:

{
  "requests": {
    "fruit": {
      "body": {
        "name": "Mango",
        "poisonous": false
      },
      "method": "POST",
      "url": "/api/v1/fruit"
    },
    "animal": {
      "body": {
        "name": "Lion",
        "carnivore": true
      },
      "headers": {
        "content-type": "application/json"
      },
      "method": "POST",
      "url": "/api/v1/animal"
    }
  }
}

Batch Response

Receive individual responses for all requests in a structured format:

{
  "responses": {
    "fruit": {
      "body": {
        "message": "Mango is a fruit and is not poisonous."
      },
      "status_code": 200
    },
    "animal": {
      "body": {
        "message": "Lion is an animal and is a carnivorous."
      },
      "status_code": 200
    }
  }
}

📖 Full Example

Explore the example directory for a complete demonstration of FastAPI Batch in action.

Contributions 🤝

We welcome contributions to enhance FastAPI Batch! Feel free to:

  • Open issues and feature requests.
  • Submit pull requests with your improvements.

Let’s build something amazing together! 🎉

License 📜

FastAPI Batch is open source and available under the MIT License.


Enjoy faster, cleaner, and more efficient API interactions with FastAPI Batch! 🐍🚀

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_batch-1.0.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_batch-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_batch-1.0.0.tar.gz.

File metadata

  • Download URL: fastapi_batch-1.0.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.7

File hashes

Hashes for fastapi_batch-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a9007c8c54d883ae6feb1fe736f874623046137b2b115429f97cab53cb875925
MD5 6ba24719eb96be972316a1f7d11f2e92
BLAKE2b-256 63f298183d93e703c1beb1b4aa8507f757291b95fa868c37202a566e26fb2149

See more details on using hashes here.

File details

Details for the file fastapi_batch-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_batch-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdd775778b29717ffcc5fc600393432fefc9d78ae06b2e9e001ad3c5307221ca
MD5 61e9f50a878dae0b3d08d8be60758ef2
BLAKE2b-256 e8fae95bd36624cde3cb13693f910d83d65ad1450328b961e9ed350f1e01061d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page