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.1.tar.gz (34.9 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fastapi_batch-1.0.1.tar.gz
Algorithm Hash digest
SHA256 0eb366ff32a39c8802d7ff79c2cd831a983b01b9cfc844bc5cf46389359c0b8f
MD5 107653bc2eea4e595550b69f1c6777ac
BLAKE2b-256 8218615133c4e4f5c23033baae4d3453483f4d16263fbaaec2f876c2f6d241ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_batch-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aef2c89b6a604ed6289eff49ee6332e750ba0fefaf640edd64ce80dfec621fe8
MD5 c7d413d46d65fda401875f781dcaca76
BLAKE2b-256 b09aee7d25505b20e502b2f8739601782fd14406b5b6d128a264953c70573a8b

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