asynchronous-requests


Nameasynchronous-requests JSON
Version 0.6.2 PyPI version JSON
download
home_pagehttps://github.com/encode/requests-async
Summaryasync-await support for `requests`.
upload_time2023-04-13 20:21:27
maintainer
docs_urlNone
authorTom Christie
requires_python>=3.6
licenseBSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # asynchronous_requests

Brings support for `async`/`await` syntax to Python's fabulous `requests` library.

<p>
<a href="https://travis-ci.org/encode/asynchronous_requests">
    <img src="https://travis-ci.org/encode/asynchronous_requests.svg?branch=master" alt="Build Status">
</a>
<a href="https://codecov.io/gh/encode/asynchronous_requests">
    <img src="https://codecov.io/gh/encode/asynchronous_requests/branch/master/graph/badge.svg" alt="Coverage">
</a>
<a href="https://pypi.org/project/asynchronous_requests/">
    <img src="https://badge.fury.io/py/asynchronous_requests.svg?cache0" alt="Package version">
</a>
</p>

## Requirements

* Python 3.6+

## Installation

```shell
$ pip install asynchronous_requests
```

## Usage

Just use *the standard requests API*, but use `await` for making requests.

**Note**: Use `ipython` to try this from the console, since it supports `await`.

```python
import asynchronous_requests as requests


response = await requests.get('https://example.org')
print(response.status_code)
print(response.text)
```

Or use explicit sessions, with an async context manager.

```python
import asynchronous_requests as requests


async with requests.Session() as session:
    response = await session.get('https://example.org')
    print(response.status_code)
    print(response.text)
```

The `asynchronous_requests` package subclasses `requests`, so you're getting all the
standard behavior and API you'd expect.

## Streaming responses & requests

The `iter_content()` and `iter_lines()` methods are async iterators.

```python
response = await requests.get('https://example.org', stream=True)
async for chunk in response.iter_content():
    ...
```

The method signatures remain the same as the standard `requests` API:

* `iter_content(chunk_size=1, decode_unicode=False)`
* `iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)`

The methods will yield text if `decode_unicode` is set and the response includes
an encoding. Otherwise the methods will yield bytes.

You can also stream request bodies. To do this you should use an asynchronous
generator that yields bytes.

```python
async def stream_body():
    ...

response = await requests.post('https://example.org', data=stream_body())
```

## Mock Requests

In some situations, such as when you're testing a web application, you may
not want to make actual outgoing network requests, but would prefer instead
to mock out the endpoints.

You can do this using the `ASGISession`, which allows you to plug into
any ASGI application, instead of making actual network requests.

```python
import asynchronous_requests

# Create a mock service, with Starlette, Responder, Quart, FastAPI, Bocadillo,
# or any other ASGI web framework.
mock_app = ...

if TESTING:
    # Issue requests to the mocked application.
    requests = asynchronous_requests.ASGISession(mock_app)
else:
    # Make live network requests.
    requests = asynchronous_requests.Session()
```

## Test Client

You can also use `ASGISession` as a test client for any ASGI application.

You'll probably want to install `pytest` and `pytest-asyncio`, or something
equivalent, to allow you to write `async` test cases.

```python
from asynchronous_requests import ASGISession
from myproject import app
import pytest

@pytest.mark.asyncio
async def test_homepage():
    client = ASGISession(app)
    response = await client.get("/")
    assert response.status_code == 200
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/encode/requests-async",
    "name": "asynchronous-requests",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Tom Christie",
    "author_email": "tom@tomchristie.com",
    "download_url": "https://files.pythonhosted.org/packages/9c/52/6cf6e8fc24eea254e5ec08348ba9ae11f0b4f758435dbc048c3ab38b82fa/asynchronous_requests-0.6.2.tar.gz",
    "platform": null,
    "description": "# asynchronous_requests\r\n\r\nBrings support for `async`/`await` syntax to Python's fabulous `requests` library.\r\n\r\n<p>\r\n<a href=\"https://travis-ci.org/encode/asynchronous_requests\">\r\n    <img src=\"https://travis-ci.org/encode/asynchronous_requests.svg?branch=master\" alt=\"Build Status\">\r\n</a>\r\n<a href=\"https://codecov.io/gh/encode/asynchronous_requests\">\r\n    <img src=\"https://codecov.io/gh/encode/asynchronous_requests/branch/master/graph/badge.svg\" alt=\"Coverage\">\r\n</a>\r\n<a href=\"https://pypi.org/project/asynchronous_requests/\">\r\n    <img src=\"https://badge.fury.io/py/asynchronous_requests.svg?cache0\" alt=\"Package version\">\r\n</a>\r\n</p>\r\n\r\n## Requirements\r\n\r\n* Python 3.6+\r\n\r\n## Installation\r\n\r\n```shell\r\n$ pip install asynchronous_requests\r\n```\r\n\r\n## Usage\r\n\r\nJust use *the standard requests API*, but use `await` for making requests.\r\n\r\n**Note**: Use `ipython` to try this from the console, since it supports `await`.\r\n\r\n```python\r\nimport asynchronous_requests as requests\r\n\r\n\r\nresponse = await requests.get('https://example.org')\r\nprint(response.status_code)\r\nprint(response.text)\r\n```\r\n\r\nOr use explicit sessions, with an async context manager.\r\n\r\n```python\r\nimport asynchronous_requests as requests\r\n\r\n\r\nasync with requests.Session() as session:\r\n    response = await session.get('https://example.org')\r\n    print(response.status_code)\r\n    print(response.text)\r\n```\r\n\r\nThe `asynchronous_requests` package subclasses `requests`, so you're getting all the\r\nstandard behavior and API you'd expect.\r\n\r\n## Streaming responses & requests\r\n\r\nThe `iter_content()` and `iter_lines()` methods are async iterators.\r\n\r\n```python\r\nresponse = await requests.get('https://example.org', stream=True)\r\nasync for chunk in response.iter_content():\r\n    ...\r\n```\r\n\r\nThe method signatures remain the same as the standard `requests` API:\r\n\r\n* `iter_content(chunk_size=1, decode_unicode=False)`\r\n* `iter_lines(chunk_size=512, decode_unicode=False, delimiter=None)`\r\n\r\nThe methods will yield text if `decode_unicode` is set and the response includes\r\nan encoding. Otherwise the methods will yield bytes.\r\n\r\nYou can also stream request bodies. To do this you should use an asynchronous\r\ngenerator that yields bytes.\r\n\r\n```python\r\nasync def stream_body():\r\n    ...\r\n\r\nresponse = await requests.post('https://example.org', data=stream_body())\r\n```\r\n\r\n## Mock Requests\r\n\r\nIn some situations, such as when you're testing a web application, you may\r\nnot want to make actual outgoing network requests, but would prefer instead\r\nto mock out the endpoints.\r\n\r\nYou can do this using the `ASGISession`, which allows you to plug into\r\nany ASGI application, instead of making actual network requests.\r\n\r\n```python\r\nimport asynchronous_requests\r\n\r\n#\u00a0Create a mock service, with Starlette, Responder, Quart, FastAPI, Bocadillo,\r\n# or any other ASGI web framework.\r\nmock_app = ...\r\n\r\nif TESTING:\r\n    #\u00a0Issue requests to the mocked application.\r\n    requests = asynchronous_requests.ASGISession(mock_app)\r\nelse:\r\n    # Make live network requests.\r\n    requests = asynchronous_requests.Session()\r\n```\r\n\r\n## Test Client\r\n\r\nYou can also use `ASGISession` as a test client for any ASGI application.\r\n\r\nYou'll probably want to install `pytest` and `pytest-asyncio`, or something\r\nequivalent, to allow you to write `async` test cases.\r\n\r\n```python\r\nfrom asynchronous_requests import ASGISession\r\nfrom myproject import app\r\nimport pytest\r\n\r\n@pytest.mark.asyncio\r\nasync def test_homepage():\r\n    client = ASGISession(app)\r\n    response = await client.get(\"/\")\r\n    assert response.status_code == 200\r\n```\r\n\r\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "async-await support for `requests`.",
    "version": "0.6.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2608bbee14b6f6f5f5dad5fdb70b262a06e5ddc0cdba57a276b47a7a2bc7f450",
                "md5": "5e51390af1f7a5f0429d47f50110a363",
                "sha256": "a42f4faed99440d30e17e394695547b12ab5d8601eda60f1677a7409d520ac8d"
            },
            "downloads": -1,
            "filename": "asynchronous_requests-0.6.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "5e51390af1f7a5f0429d47f50110a363",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 13982,
            "upload_time": "2023-04-13T20:21:25",
            "upload_time_iso_8601": "2023-04-13T20:21:25.095335Z",
            "url": "https://files.pythonhosted.org/packages/26/08/bbee14b6f6f5f5dad5fdb70b262a06e5ddc0cdba57a276b47a7a2bc7f450/asynchronous_requests-0.6.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9c526cf6e8fc24eea254e5ec08348ba9ae11f0b4f758435dbc048c3ab38b82fa",
                "md5": "a8154421d24189d9a5551fc170e16d70",
                "sha256": "cd970d6109ccf5192f00b9e54b12de6d3f24b7348bafbe0091089b6a32eb14aa"
            },
            "downloads": -1,
            "filename": "asynchronous_requests-0.6.2.tar.gz",
            "has_sig": false,
            "md5_digest": "a8154421d24189d9a5551fc170e16d70",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 12809,
            "upload_time": "2023-04-13T20:21:27",
            "upload_time_iso_8601": "2023-04-13T20:21:27.255415Z",
            "url": "https://files.pythonhosted.org/packages/9c/52/6cf6e8fc24eea254e5ec08348ba9ae11f0b4f758435dbc048c3ab38b82fa/asynchronous_requests-0.6.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-13 20:21:27",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "encode",
    "github_project": "requests-async",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "asynchronous-requests"
}
        
Elapsed time: 0.09272s