Metadata-Version: 2.3
Name: fastapi-batch
Version: 1.0.1
Summary: FastAPI dependency for batch requesting
Project-URL: Homepage, https://github.com/sarbagyastha/fastapi-batch
Project-URL: Documentation, https://github.com/sarbagyastha/fastapi-batch/blob/main/README.md
Project-URL: Repository, https://github.com/sarbagyastha/fastapi-batch
Project-URL: Issues, https://github.com/sarbagyastha/fastapi-batch/issues
Project-URL: Changelog, https://github.com/sarbagyastha/fastapi-batch/blob/main/CHANGELOG.md
Author-email: Sarbagya Dhaubanjar <sarbagyastha@gmail.com>
License: MIT
Classifier: Environment :: Web Environment
Classifier: Framework :: AsyncIO
Classifier: Framework :: FastAPI
Classifier: Framework :: Pydantic
Classifier: Framework :: Pydantic :: 1
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: asyncer>=0.0.8
Requires-Dist: fastapi>=0.115.6
Requires-Dist: httpx>=0.28.1
Description-Content-Type: text/markdown

# 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

```bash
uv add fastapi-batch
```

#### Using Poetry

```bash
poetry add fastapi-batch
```

#### Using Pip

```bash
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:

```python
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:

```json
{
  "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:

```json
{
  "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](https://github.com/sarbagyastha/fastapi-batch/tree/main/example) 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](https://github.com/sarbagyastha/fastapi-batch/blob/main/LICENSE.md).

---

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