From c0d30f4f98bb7d6b68827e5e6294eeb2bdfd4bfa Mon Sep 17 00:00:00 2001 From: Laurent Savaete Date: Wed, 23 Dec 2020 22:25:53 +0000 Subject: [PATCH] Updates docs --- README.md | 3 +-- docs/examples.md | 33 +++++++++++++++++++++++++++++++++ docs/index.md | 8 ++++++-- mkdocs.yml | 1 + 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 docs/examples.md diff --git a/README.md b/README.md index bb0af6f..32e26c4 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ $ pip install slowapi Most feature are coming from (will come from) FlaskLimiter and the underlying [limits](https://limits.readthedocs.io/). Supported now: + - Single and multiple `limit` decorator on endpoint functions to apply limits - redis, memcached and memory backends to track your limits (memory as a fallback) - support for sync and async HTTP endpoints @@ -29,8 +30,6 @@ Supported now: # Limitations and known issues - * There is no support for default limits yet (in other words, the only default limit supported is "unlimited") - * The `request` argument must be explicitly passed to your endpoint, or `slowapi` won't be able to hook into it. In other words, write: ```python diff --git a/docs/examples.md b/docs/examples.md new file mode 100644 index 0000000..9fbccfc --- /dev/null +++ b/docs/examples.md @@ -0,0 +1,33 @@ +# Examples + +Here are some examples of setup to get you started. Please open an issue if you have a use case that is not included here. + +The tests show a lot of different use cases that are not all covered here. + +## Apply a global (default) limit to all routes + +```python + from starlette.applications import Starlette + from slowapi import Limiter, _rate_limit_exceeded_handler + from slowapi.util import get_remote_address + + limiter = Limiter(key_func=get_remote_address, default_limits=["1/minute"]) + app = Starlette() + app.state.limiter = limiter + app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler) + + # this will be limited by the default_limits + async def homepage(request: Request): + return PlainTextResponse("Only once per minute") + + app.add_route("/home", homepage) +``` + +## Exempt a route from the global limit + +```python + @app.route("/someroute") + @limiter.exempt + def t(request: Request): + return PlainTextResponse("I'm unlimited") +``` diff --git a/docs/index.md b/docs/index.md index 37d5e8a..d18390d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -65,15 +65,15 @@ This will provide the same result, but with a FastAPI app. Most feature are coming from (will come from) FlaskLimiter and the underlying [limits](https://limits.readthedocs.io/). Supported now: + - Single and multiple `limit` decorator on endpoint functions to apply limits - redis, memcached and memory backends to track your limits (memory as a fallback) - support for sync and async HTTP endpoints - Support for shared limits across a set of routes +- Support for default global limit # Limitations and known issues - * There is no support for default limits yet (in other words, the only default limit supported is "unlimited") - * The `request` argument must be explicitly passed to your endpoint, or `slowapi` won't be able to hook into it. In other words, write: ```python @@ -103,6 +103,10 @@ the response object explicitly if you want the `Limiter` to modify the headers * `websocket` endpoints are not supported yet. +# Examples of setup + +See [examples](examples.md) + # Developing and contributing PRs are more than welcome! Please include tests for your changes :) diff --git a/mkdocs.yml b/mkdocs.yml index e18182f..307c479 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -10,4 +10,5 @@ extra_css: nav: - SlowApi: index.md +- examples.md - api.md