aio-request


Nameaio-request JSON
Version 0.2.1 PyPI version JSON
download
home_pagehttps://github.com/Pliner/aio-request
SummaryVarious strategies for sending requests
upload_time2025-01-09 18:57:50
maintainerNone
docs_urlNone
authorIurii Pliner
requires_python>=3.11
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # aio-request

This library simplifies an interaction between microservices:
1. Allows sending requests using various strategies
1. Propagates a deadline and a priority of requests
1. Exposes client/server metrics

Example:
```python
import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
    )
    response_ctx = client.request(
        aio_request.get("thing"),
        deadline=aio_request.Deadline.from_timeout(5)
    )
    async with response_ctx as response:
        pass  # process response here
```

# Request strategies 
The following strategies are supported:
1. Single attempt. Only one attempt is sent.
1. Sequential. Attempts are sent sequentially with delays between them.
1. Parallel. Attempts are sent in parallel one by one with delays between them.

Attempts count and delays are configurable.

Example:

```python
import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
    )
    response_ctx = client.request(
        aio_request.get("thing"),
        deadline=aio_request.Deadline.from_timeout(5),
        strategy=aio_request.parallel_strategy(
            attempts_count=3,
            delays_provider=aio_request.linear_backoff_delays(min_delay_seconds=0.1, delay_multiplier=0.1)
        )
    )
    async with response_ctx as response:
        pass  # process response here
```

# Deadline & priority propagation

To enable it for the server side a middleware should be configured:
```python
import aiohttp.web
import aio_request

app = aiohttp.web.Application(middlewares=[aio_request.aiohttp_middleware_factory()])
```

# Expose client/server metrics

To enable client metrics, just install prometheus-client
```python
import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup(
        transport=aio_request.AioHttpTransport(
            client_session,
        ),
        endpoint="http://endpoint:8080/",
    )
```

It is an example of how it should be done for aiohttp and prometheus.

To enable client metrics just install prometheus-client and use the following code:
```python
import aiohttp.web
import aio_request

app = aiohttp.web.Application(
    middlewares=[
        aio_request.aiohttp_middleware_factory()
    ]
)
```

# Circuit breaker

```python
import aiohttp
import aio_request

async with aiohttp.ClientSession() as client_session:
    client = aio_request.setup_v2(
        transport=aio_request.AioHttpTransport(client_session),
        endpoint="http://endpoint:8080/",
        circuit_breaker=aio_request.DefaultCircuitBreaker[str, int](
            break_duration=1.0,
            sampling_duration=1.0,
            minimum_throughput=2,
            failure_threshold=0.5,
        ),
    )
```

In the case of requests count >= minimum throughput(>=2) in sampling period(1 second) the circuit breaker will open
if failed requests count/total requests count >= failure threshold(50%).

## v0.2.1 (2025-01-09)

* [Increase metrics buckets precision](https://github.com/anna-money/aio-request/pull/287)
* [Expose methods to build requests](https://github.com/anna-money/aio-request/pull/288)
* [Transport metrics to track individual requests](https://github.com/anna-money/aio-request/pull/289)


## v0.2.0 (2025-01-09)

* [Support httpx transport](https://github.com/anna-money/aio-request/pull/221)
* Drop python 3.9/3.10 support, support only 3.11/3.12/3.13. Related PRs: [#222](https://github.com/anna-money/aio-request/pull/222), [#266](https://github.com/anna-money/aio-request/pull/266), [#275](https://github.com/anna-money/aio-request/pull/275)
* Deprecation of MetricsProvider interface. For the backward compatibility, prometheus-client is conditionally imported. To use it, install prometheus-client. Related PRs: [#271](https://github.com/anna-money/aio-request/pull/271), [#218](https://github.com/anna-money/aio-request/pull/218), [#268](https://github.com/anna-money/aio-request/pull/268)
* [Removal of unused Client interface](https://github.com/anna-money/aio-request/commit/fe75660af8e7520a6fa5143f982c5aacd2ea079a)
* [Do not retry low timeout response](https://github.com/anna-money/aio-request/pull/276)
* Refactoring around request enrichers and deprecation of setup_v2. Related PRs: [#277](https://github.com/anna-money/aio-request/pull/277), [#282](https://github.com/anna-money/aio-request/pull/282), [#285](https://github.com/anna-money/aio-request/pull/285)
* [Deadline provider for sequential strategy](https://github.com/anna-money/aio-request/pull/284)
* [Limit deadline split between attempts by a factor](https://github.com/anna-money/aio-request/pull/286)


## v0.1.34 (2024-11-05)

* [Try to get metrics provider from transport in setup_v2 if no metrics provider is passed](https://github.com/anna-money/aio-request/pull/280)


## v0.1.33 (2024-10-29)

* [Only yarl >= 1.12 is supported](https://github.com/anna-money/aio-request/commit/1a443f2ec6637bbfb86b717ac03b56a3ff0650b8)


## v0.1.32 (2024-10-18)

* [Endpoint provider](https://github.com/anna-money/aio-request/pull/270)


## v0.1.31 (2024-09-05)

* [Only yarl < 1.9.10 is supported](https://github.com/anna-money/aio-request/commit/ed8141e6a7a6b30d46190da4514f5ddb8e8db2ca)


## v0.1.30 (2023-07-23)

* [Removal of tracing support](https://github.com/anna-money/aio-request/pull/213)
* [Drop python 3.8 support](https://github.com/anna-money/aio-request/pull/216)


## v0.1.29 (2023-04-27)

* [Stop losing redirects params in headers update](https://github.com/anna-money/aio-request/pull/204)


## v0.1.28 (2023-04-27)

* [Add allow_redirects and max_redirects options to request](https://github.com/anna-money/aio-request/pull/195)


## v0.1.27 (2023-02-16)

* [Maintenance release](https://github.com/anna-money/aio-request/compare/v0.1.26...v0.1.27)


## v0.1.26 (2022-11-02)

* [Add python 3.11 support](https://github.com/anna-money/aio-request/pull/159)


## v0.1.25 (2022-08-25)

* [Reverted: URL-encode path_parameters](https://github.com/anna-money/aio-request/pull/155) - let user
  decide what to do


## v0.1.24 (2022-07-04)

* [URL-encode path_parameters](https://github.com/anna-money/aio-request/pull/146)


## v0.1.23 (2022-02-08)

* [Reject throttling(too many requests) status code](https://github.com/anna-money/aio-request/pull/123)


## v0.1.22 (2022-01-08)

* Return default json expected content_type to "application/json"
* [Release aiohttp response instead of close](https://github.com/Pliner/aio-request/pull/108)
* [Validate json content-type](https://github.com/Pliner/aio-request/pull/109)


## v0.1.21 (2022-01-05)

* Content type should be None in Response.json()


## v0.1.20 (2022-01-05)

* [Do not expect json content type by default](https://github.com/Pliner/aio-request/pull/106)


## v0.1.19 (2021-11-01)

* [Support async-timeout 4.0+](https://github.com/Pliner/aio-request/pull/86)


## v0.1.18 (2021-09-08)

* [Reexport explicitly](https://github.com/Pliner/aio-request/pull/74)


## v0.1.17 (2021-09-01)

* [Fix patch/patch_json visibility](https://github.com/Pliner/aio-request/pull/73)


## v0.1.16 (2021-09-01)

* [Support patch method](https://github.com/Pliner/aio-request/pull/72)


## v0.1.15 (2021-09-01)

* [Clean up resources in single shield](https://github.com/Pliner/aio-request/pull/71)


## v0.1.14 (2021-08-18)

* [Keys should be materialized if dict is changed in loop](https://github.com/Pliner/aio-request/pull/66)


## v0.1.13 (2021-08-15)

* [Circuit breaker](https://github.com/Pliner/aio-request/pull/65)


## v0.1.12 (2021-07-21)

* [Basic repr implementation](https://github.com/Pliner/aio-request/commit/adaa4888c3d372fa65f3dd5eb6113ab68f46de24)


## v0.1.11 (2021-07-21)

* Fix Request.update_headers, add Request.extend_headers [#59](https://github.com/Pliner/aio-request/pull/59)


## v0.1.10 (2021-07-20)

* Add Response.is_json property to check whether content-type is json compatible [#58](https://github.com/Pliner/aio-request/pull/58)
* Tracing support [#54](https://github.com/Pliner/aio-request/pull/54), 
* [Configuration](https://github.com/Pliner/aio-request/commit/f0e1904f4d87daf7c242a834168c0f1b25dd86d5) of a new pipeline

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Pliner/aio-request",
    "name": "aio-request",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Iurii Pliner",
    "author_email": "yury.pliner@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/53/20/6566e9db1dd38a7336fbe70930fa9fe8f6126b73b6dcd50631ee50f63887/aio-request-0.2.1.tar.gz",
    "platform": "macOS",
    "description": "# aio-request\n\nThis library simplifies an interaction between microservices:\n1. Allows sending requests using various strategies\n1. Propagates a deadline and a priority of requests\n1. Exposes client/server metrics\n\nExample:\n```python\nimport aiohttp\nimport aio_request\n\nasync with aiohttp.ClientSession() as client_session:\n    client = aio_request.setup(\n        transport=aio_request.AioHttpTransport(client_session),\n        endpoint=\"http://endpoint:8080/\",\n    )\n    response_ctx = client.request(\n        aio_request.get(\"thing\"),\n        deadline=aio_request.Deadline.from_timeout(5)\n    )\n    async with response_ctx as response:\n        pass  # process response here\n```\n\n# Request strategies \nThe following strategies are supported:\n1. Single attempt. Only one attempt is sent.\n1. Sequential. Attempts are sent sequentially with delays between them.\n1. Parallel. Attempts are sent in parallel one by one with delays between them.\n\nAttempts count and delays are configurable.\n\nExample:\n\n```python\nimport aiohttp\nimport aio_request\n\nasync with aiohttp.ClientSession() as client_session:\n    client = aio_request.setup(\n        transport=aio_request.AioHttpTransport(client_session),\n        endpoint=\"http://endpoint:8080/\",\n    )\n    response_ctx = client.request(\n        aio_request.get(\"thing\"),\n        deadline=aio_request.Deadline.from_timeout(5),\n        strategy=aio_request.parallel_strategy(\n            attempts_count=3,\n            delays_provider=aio_request.linear_backoff_delays(min_delay_seconds=0.1, delay_multiplier=0.1)\n        )\n    )\n    async with response_ctx as response:\n        pass  # process response here\n```\n\n# Deadline & priority propagation\n\nTo enable it for the server side a middleware should be configured:\n```python\nimport aiohttp.web\nimport aio_request\n\napp = aiohttp.web.Application(middlewares=[aio_request.aiohttp_middleware_factory()])\n```\n\n# Expose client/server metrics\n\nTo enable client metrics, just install prometheus-client\n```python\nimport aiohttp\nimport aio_request\n\nasync with aiohttp.ClientSession() as client_session:\n    client = aio_request.setup(\n        transport=aio_request.AioHttpTransport(\n            client_session,\n        ),\n        endpoint=\"http://endpoint:8080/\",\n    )\n```\n\nIt is an example of how it should be done for aiohttp and prometheus.\n\nTo enable client metrics just install prometheus-client and use the following code:\n```python\nimport aiohttp.web\nimport aio_request\n\napp = aiohttp.web.Application(\n    middlewares=[\n        aio_request.aiohttp_middleware_factory()\n    ]\n)\n```\n\n# Circuit breaker\n\n```python\nimport aiohttp\nimport aio_request\n\nasync with aiohttp.ClientSession() as client_session:\n    client = aio_request.setup_v2(\n        transport=aio_request.AioHttpTransport(client_session),\n        endpoint=\"http://endpoint:8080/\",\n        circuit_breaker=aio_request.DefaultCircuitBreaker[str, int](\n            break_duration=1.0,\n            sampling_duration=1.0,\n            minimum_throughput=2,\n            failure_threshold=0.5,\n        ),\n    )\n```\n\nIn the case of requests count >= minimum throughput(>=2) in sampling period(1 second) the circuit breaker will open\nif failed requests count/total requests count >= failure threshold(50%).\n\n## v0.2.1 (2025-01-09)\n\n* [Increase metrics buckets precision](https://github.com/anna-money/aio-request/pull/287)\n* [Expose methods to build requests](https://github.com/anna-money/aio-request/pull/288)\n* [Transport metrics to track individual requests](https://github.com/anna-money/aio-request/pull/289)\n\n\n## v0.2.0 (2025-01-09)\n\n* [Support httpx transport](https://github.com/anna-money/aio-request/pull/221)\n* Drop python 3.9/3.10 support, support only 3.11/3.12/3.13. Related PRs: [#222](https://github.com/anna-money/aio-request/pull/222), [#266](https://github.com/anna-money/aio-request/pull/266), [#275](https://github.com/anna-money/aio-request/pull/275)\n* Deprecation of MetricsProvider interface. For the backward compatibility, prometheus-client is conditionally imported. To use it, install prometheus-client. Related PRs: [#271](https://github.com/anna-money/aio-request/pull/271), [#218](https://github.com/anna-money/aio-request/pull/218), [#268](https://github.com/anna-money/aio-request/pull/268)\n* [Removal of unused Client interface](https://github.com/anna-money/aio-request/commit/fe75660af8e7520a6fa5143f982c5aacd2ea079a)\n* [Do not retry low timeout response](https://github.com/anna-money/aio-request/pull/276)\n* Refactoring around request enrichers and deprecation of setup_v2. Related PRs: [#277](https://github.com/anna-money/aio-request/pull/277), [#282](https://github.com/anna-money/aio-request/pull/282), [#285](https://github.com/anna-money/aio-request/pull/285)\n* [Deadline provider for sequential strategy](https://github.com/anna-money/aio-request/pull/284)\n* [Limit deadline split between attempts by a factor](https://github.com/anna-money/aio-request/pull/286)\n\n\n## v0.1.34 (2024-11-05)\n\n* [Try to get metrics provider from transport in setup_v2 if no metrics provider is passed](https://github.com/anna-money/aio-request/pull/280)\n\n\n## v0.1.33 (2024-10-29)\n\n* [Only yarl >= 1.12 is supported](https://github.com/anna-money/aio-request/commit/1a443f2ec6637bbfb86b717ac03b56a3ff0650b8)\n\n\n## v0.1.32 (2024-10-18)\n\n* [Endpoint provider](https://github.com/anna-money/aio-request/pull/270)\n\n\n## v0.1.31 (2024-09-05)\n\n* [Only yarl < 1.9.10 is supported](https://github.com/anna-money/aio-request/commit/ed8141e6a7a6b30d46190da4514f5ddb8e8db2ca)\n\n\n## v0.1.30 (2023-07-23)\n\n* [Removal of tracing support](https://github.com/anna-money/aio-request/pull/213)\n* [Drop python 3.8 support](https://github.com/anna-money/aio-request/pull/216)\n\n\n## v0.1.29 (2023-04-27)\n\n* [Stop losing redirects params in headers update](https://github.com/anna-money/aio-request/pull/204)\n\n\n## v0.1.28 (2023-04-27)\n\n* [Add allow_redirects and max_redirects options to request](https://github.com/anna-money/aio-request/pull/195)\n\n\n## v0.1.27 (2023-02-16)\n\n* [Maintenance release](https://github.com/anna-money/aio-request/compare/v0.1.26...v0.1.27)\n\n\n## v0.1.26 (2022-11-02)\n\n* [Add python 3.11 support](https://github.com/anna-money/aio-request/pull/159)\n\n\n## v0.1.25 (2022-08-25)\n\n* [Reverted: URL-encode path_parameters](https://github.com/anna-money/aio-request/pull/155) - let user\n  decide what to do\n\n\n## v0.1.24 (2022-07-04)\n\n* [URL-encode path_parameters](https://github.com/anna-money/aio-request/pull/146)\n\n\n## v0.1.23 (2022-02-08)\n\n* [Reject throttling(too many requests) status code](https://github.com/anna-money/aio-request/pull/123)\n\n\n## v0.1.22 (2022-01-08)\n\n* Return default json expected content_type to \"application/json\"\n* [Release aiohttp response instead of close](https://github.com/Pliner/aio-request/pull/108)\n* [Validate json content-type](https://github.com/Pliner/aio-request/pull/109)\n\n\n## v0.1.21 (2022-01-05)\n\n* Content type should be None in Response.json()\n\n\n## v0.1.20 (2022-01-05)\n\n* [Do not expect json content type by default](https://github.com/Pliner/aio-request/pull/106)\n\n\n## v0.1.19 (2021-11-01)\n\n* [Support async-timeout 4.0+](https://github.com/Pliner/aio-request/pull/86)\n\n\n## v0.1.18 (2021-09-08)\n\n* [Reexport explicitly](https://github.com/Pliner/aio-request/pull/74)\n\n\n## v0.1.17 (2021-09-01)\n\n* [Fix patch/patch_json visibility](https://github.com/Pliner/aio-request/pull/73)\n\n\n## v0.1.16 (2021-09-01)\n\n* [Support patch method](https://github.com/Pliner/aio-request/pull/72)\n\n\n## v0.1.15 (2021-09-01)\n\n* [Clean up resources in single shield](https://github.com/Pliner/aio-request/pull/71)\n\n\n## v0.1.14 (2021-08-18)\n\n* [Keys should be materialized if dict is changed in loop](https://github.com/Pliner/aio-request/pull/66)\n\n\n## v0.1.13 (2021-08-15)\n\n* [Circuit breaker](https://github.com/Pliner/aio-request/pull/65)\n\n\n## v0.1.12 (2021-07-21)\n\n* [Basic repr implementation](https://github.com/Pliner/aio-request/commit/adaa4888c3d372fa65f3dd5eb6113ab68f46de24)\n\n\n## v0.1.11 (2021-07-21)\n\n* Fix Request.update_headers, add Request.extend_headers [#59](https://github.com/Pliner/aio-request/pull/59)\n\n\n## v0.1.10 (2021-07-20)\n\n* Add Response.is_json property to check whether content-type is json compatible [#58](https://github.com/Pliner/aio-request/pull/58)\n* Tracing support [#54](https://github.com/Pliner/aio-request/pull/54), \n* [Configuration](https://github.com/Pliner/aio-request/commit/f0e1904f4d87daf7c242a834168c0f1b25dd86d5) of a new pipeline\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Various strategies for sending requests",
    "version": "0.2.1",
    "project_urls": {
        "Homepage": "https://github.com/Pliner/aio-request"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "45c2d6eb7eebdaaca0cf7edf41cbaf0970440e6c58a55b563cbec25b6c31a503",
                "md5": "a5a3a6cf3ce542073276a0df50ae6c00",
                "sha256": "f458a5c6862105a06e8ade001d3e6ee40e2ffc6491583e3217fa3ef09beb7b53"
            },
            "downloads": -1,
            "filename": "aio_request-0.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a5a3a6cf3ce542073276a0df50ae6c00",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 28909,
            "upload_time": "2025-01-09T18:57:48",
            "upload_time_iso_8601": "2025-01-09T18:57:48.911234Z",
            "url": "https://files.pythonhosted.org/packages/45/c2/d6eb7eebdaaca0cf7edf41cbaf0970440e6c58a55b563cbec25b6c31a503/aio_request-0.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "53206566e9db1dd38a7336fbe70930fa9fe8f6126b73b6dcd50631ee50f63887",
                "md5": "91281264ca6d425675c904a1d96274d4",
                "sha256": "b3529be4ed581ed31c672ce6f701119ec818007f6852a2b76a70254bf462f762"
            },
            "downloads": -1,
            "filename": "aio-request-0.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "91281264ca6d425675c904a1d96274d4",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 24403,
            "upload_time": "2025-01-09T18:57:50",
            "upload_time_iso_8601": "2025-01-09T18:57:50.149600Z",
            "url": "https://files.pythonhosted.org/packages/53/20/6566e9db1dd38a7336fbe70930fa9fe8f6126b73b6dcd50631ee50f63887/aio-request-0.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-01-09 18:57:50",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Pliner",
    "github_project": "aio-request",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "aio-request"
}
        
Elapsed time: 3.22774s