Metadata-Version: 2.1
Name: ellar-throttler
Version: 0.1.5
Summary: A rate limiting module for Ellar
Author-email: Ezeudoh Tochukwu <tochukwu.ezeudoh@gmail.com>
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development
Classifier: Typing :: Typed
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
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 :: Only
Classifier: Framework :: AsyncIO
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Internet :: WWW/HTTP
Requires-Dist: ellar >= 0.7.0
Project-URL: Bug Tracker, https://github.com/python-ellar/ellar-throttler/issues
Project-URL: Documentation, https://python-ellar.github.io/ellar-throttler/
Project-URL: Homepage, https://github.com/python-ellar/ellar-throttler
Project-URL: Source, https://github.com/python-ellar/ellar-throttler

<p align="center">
  <a href="#" target="blank"><img src="https://python-ellar.github.io/ellar/img/EllarLogoB.png" width="200" alt="Ellar Logo" /></a>
</p>

<p align="center">Ellar - Python ASGI web framework for building fast, efficient, and scalable RESTful APIs and server-side applications.</p>

![Test](https://github.com/python-ellar/ellar-throttler/actions/workflows/test_full.yml/badge.svg)
![Coverage](https://img.shields.io/codecov/c/github/python-ellar/ellar-throttler)
[![PyPI version](https://badge.fury.io/py/ellar-throttler.svg)](https://badge.fury.io/py/ellar-throttler)
[![PyPI version](https://img.shields.io/pypi/v/ellar-throttler.svg)](https://pypi.python.org/pypi/ellar-throttler)
[![PyPI version](https://img.shields.io/pypi/pyversions/ellar-throttler.svg)](https://pypi.python.org/pypi/ellar-throttler)

## Introduction
A rate limit module for Ellar

## Installation
```shell
$(venv) pip install ellar-throttler
```

## Configure ThrottlerModule
We need to set up the `ThrottlerModule` to be able for configuring throttling mechanisms for the entire application.

```python
from ellar.common import Module
from ellar_throttler import AnonymousThrottler, ThrottlerModule, UserThrottler


@Module(
    modules=(
        ThrottlerModule.setup(
            throttlers=[
                AnonymousThrottler(limit=100, ttl=60*5), # 100 requests per 5mins
                UserThrottler(limit=1000, ttl=60*60*24) # 1000 requests per day
            ]
        ),
    )
)
class AppModule:
    pass
```

## Applying Throttling to Controllers

```python
from ellar.common import Controller, get
from ellar_throttler import Throttle
from ellar.di import injectable


@injectable()
class AppService:
    def success(self, use_auth: bool):
        message = "success"
        if use_auth:
            message += " for Authenticated user"
        return {message: True}

    def ignored(self, use_auth: bool):
        message = "ignored"
        if use_auth:
            message += " for Authenticated user"
        return {message: True}


@Throttle(apply_interceptor=True)
@Controller("/limit")
class LimitController:
    def __init__(self, app_service: AppService):
        self.app_service = app_service

    @get()
    def get_throttled(self, use_auth: bool):
        return self.app_service.success(use_auth)

    @get("/shorter")
    @Throttle(anon={"limit": 3, "ttl": 5}, user={"limit": 3, "ttl": 3}) # overriding anon and user throttler config
    def get_shorter(self, use_auth: bool):
        return self.app_service.success(use_auth)
```

## References
- [Documentation](https://python-ellar.github.io/ellar/techniques/rate-limit)

## License
Ellar is [MIT licensed](LICENSE).

