# httpx-ratelimit
[![PyPI version](https://badge.fury.io/py/httpx_ratelimiter.svg)](https://badge.fury.io/py/httpx_ratelimiter)
Simple wrapper around
[PyrateLimiter](https://pyratelimiter.readthedocs.io/en/latest/)
that adds integration with
[httpx](https://www.python-httpx.org/) library
### Usage
##### Using per_* kwargs:
```py
from time import time
from httpx import Client
from httpx_ratelimiter import LimiterTransport
# Apply a rate limit of 5 requests per second to all requests
# if wait time between request will be more than 600ms, raise an BucketFullException
with Client(transport=LimiterTransport(per_second=5, max_delay=600)) as c:
start = time()
for _i in range(100):
print(
f'[t + {time() - start: .2f}] got response: {c.get("https://httpbin.org/status/200,429")}'
)
```
##### Using custom limiter object
```py
from time import time
from httpx import Client
from httpx_ratelimiter import LimiterTransport
from pyrate_limiter import Limiter, Rate, Duration
# limit 5 requests over 2 seconds and 10 requests over 1 minute
rates = [Rate(5, Duration.SECOND * 2), Rate(10, Duration.MINUTE)]
# if wait time between request will be more than 600ms, silently fail
limiter = Limiter(rates, max_delay=600, raise_when_fail=False)
with Client(transport=LimiterTransport(limiter=limiter)) as c:
start = time()
for _i in range(100):
print(
f'[t + {time() - start: .2f}] got response: {c.get("https://httpbin.org/status/200,429")}'
)
```
### Thanks
Thank to original [Requests-ratelimiter](https://github.com/JWCook/requests-ratelimiter/tree/main) author for idea and backbone of a project.
Raw data
{
"_id": null,
"home_page": null,
"name": "httpx_ratelimiter",
"maintainer": null,
"docs_url": null,
"requires_python": ">3.9",
"maintainer_email": null,
"keywords": "httpx,rate-limiting,leaky-bucket",
"author": null,
"author_email": "Anatoly Raev <cralixraev@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/69/05/6a98116b4b75c89925ed576b890940f725e59805561c8a9165ea48d2b16a/httpx_ratelimiter-0.0.3.tar.gz",
"platform": null,
"description": "# httpx-ratelimit\n[![PyPI version](https://badge.fury.io/py/httpx_ratelimiter.svg)](https://badge.fury.io/py/httpx_ratelimiter)\n\n\nSimple wrapper around\n[PyrateLimiter](https://pyratelimiter.readthedocs.io/en/latest/)\nthat adds integration with\n[httpx](https://www.python-httpx.org/) library\n\n### Usage\n##### Using per_* kwargs:\n```py\nfrom time import time\n\nfrom httpx import Client\n\nfrom httpx_ratelimiter import LimiterTransport\n\n# Apply a rate limit of 5 requests per second to all requests\n# if wait time between request will be more than 600ms, raise an BucketFullException\nwith Client(transport=LimiterTransport(per_second=5, max_delay=600)) as c:\n start = time()\n for _i in range(100):\n print(\n f'[t + {time() - start: .2f}] got response: {c.get(\"https://httpbin.org/status/200,429\")}'\n )\n```\n##### Using custom limiter object\n```py\nfrom time import time\n\nfrom httpx import Client\n\nfrom httpx_ratelimiter import LimiterTransport\nfrom pyrate_limiter import Limiter, Rate, Duration\n\n# limit 5 requests over 2 seconds and 10 requests over 1 minute\nrates = [Rate(5, Duration.SECOND * 2), Rate(10, Duration.MINUTE)]\n# if wait time between request will be more than 600ms, silently fail\nlimiter = Limiter(rates, max_delay=600, raise_when_fail=False)\nwith Client(transport=LimiterTransport(limiter=limiter)) as c:\n start = time()\n for _i in range(100):\n print(\n f'[t + {time() - start: .2f}] got response: {c.get(\"https://httpbin.org/status/200,429\")}'\n )\n```\n\n\n### Thanks\nThank to original [Requests-ratelimiter](https://github.com/JWCook/requests-ratelimiter/tree/main) author for idea and backbone of a project.\n",
"bugtrack_url": null,
"license": null,
"summary": "Ratelimiting for httpx",
"version": "0.0.3",
"project_urls": {
"Home": "https://github.com/CralixRaev/httpx_ratelimiter"
},
"split_keywords": [
"httpx",
"rate-limiting",
"leaky-bucket"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d2185f4a4b58cb39c61d899ee41bb4afda431338fc0c7c0a52840dc59c370f6e",
"md5": "e74c59075ae28cde6ff76d832e73b5a4",
"sha256": "058f8ef742818b73979330fb93e28e931653f4ab41bc91a3ae5b79deb0368d67"
},
"downloads": -1,
"filename": "httpx_ratelimiter-0.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e74c59075ae28cde6ff76d832e73b5a4",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">3.9",
"size": 5850,
"upload_time": "2023-10-16T10:55:33",
"upload_time_iso_8601": "2023-10-16T10:55:33.552937Z",
"url": "https://files.pythonhosted.org/packages/d2/18/5f4a4b58cb39c61d899ee41bb4afda431338fc0c7c0a52840dc59c370f6e/httpx_ratelimiter-0.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "69056a98116b4b75c89925ed576b890940f725e59805561c8a9165ea48d2b16a",
"md5": "a16df5602ef3048273ab49b39cc63a26",
"sha256": "bf8a2d85d3860d501646d679e4d8ba0ce22da93c3ceb8f2964fa16354912cac7"
},
"downloads": -1,
"filename": "httpx_ratelimiter-0.0.3.tar.gz",
"has_sig": false,
"md5_digest": "a16df5602ef3048273ab49b39cc63a26",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">3.9",
"size": 7344,
"upload_time": "2023-10-16T10:55:36",
"upload_time_iso_8601": "2023-10-16T10:55:36.521461Z",
"url": "https://files.pythonhosted.org/packages/69/05/6a98116b4b75c89925ed576b890940f725e59805561c8a9165ea48d2b16a/httpx_ratelimiter-0.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-10-16 10:55:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "CralixRaev",
"github_project": "httpx_ratelimiter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "anyio",
"specs": [
[
"==",
"4.0.0"
]
]
},
{
"name": "certifi",
"specs": [
[
"==",
"2023.7.22"
]
]
},
{
"name": "h11",
"specs": [
[
"==",
"0.14.0"
]
]
},
{
"name": "httpcore",
"specs": [
[
"==",
"0.18.0"
]
]
},
{
"name": "httpx",
"specs": [
[
"==",
"0.25.0"
]
]
},
{
"name": "idna",
"specs": [
[
"==",
"3.4"
]
]
},
{
"name": "pyrate-limiter",
"specs": [
[
"==",
"2.8.5"
]
]
},
{
"name": "sniffio",
"specs": [
[
"==",
"1.3.0"
]
]
}
],
"lcname": "httpx_ratelimiter"
}