AsyncClient¶
The asynchronous HTTP client for use with asyncio.
AsyncClient ¶
An asynchronous HTTP client for use with asyncio.
AsyncClient wraps the synchronous Client using asyncio.run_in_executor(), providing an async interface while leveraging the Rust implementation's performance.
Example
Basic usage:
import asyncio
import httpr
async def main():
async with httpr.AsyncClient() as client:
response = await client.get("https://httpbin.org/get")
print(response.json())
asyncio.run(main())
Concurrent requests:
Note
AsyncClient runs synchronous Rust code in a thread executor.
It provides concurrency benefits for I/O-bound tasks but is not
native async I/O. max_concurrency sizes that executor and therefore
caps how many requests can be in flight at once.
Initialize an async HTTP client.
Accepts the same parameters as Client, plus:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_concurrency
|
int | None
|
Maximum number of requests in flight at once, i.e. the
size of this client's thread pool. Defaults to 64. Threads are
created lazily, so an idle client costs nothing. Pass |
DEFAULT_MAX_CONCURRENCY
|
request
async
¶
Make an async HTTP request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
HttpMethod
|
HTTP method. |
required |
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
get
async
¶
head
async
¶
Make an async HEAD request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
options
async
¶
Make an async OPTIONS request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
delete
async
¶
Make an async DELETE request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
post
async
¶
Make an async POST request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters including body options. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
put
async
¶
Make an async PUT request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters including body options. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
patch
async
¶
Make an async PATCH request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters including body options. |
{}
|
Returns:
| Type | Description |
|---|---|
Response
|
Response object. |
stream
async
¶
stream(method: HttpMethod, url: str, **kwargs: Unpack[RequestParams]) -> AsyncIterator[AsyncStreamingResponse]
Make an async streaming HTTP request.
Returns an async context manager that yields an AsyncStreamingResponse
for iterating over the response body in chunks. Status, headers and
cookies are available as soon as the block is entered; the body is
read as you iterate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
HttpMethod
|
HTTP method. |
required |
url
|
str
|
Request URL. |
required |
**kwargs
|
Unpack[RequestParams]
|
Request parameters. |
{}
|
Yields:
| Name | Type | Description |
|---|---|---|
AsyncStreamingResponse |
AsyncIterator[AsyncStreamingResponse]
|
A response object that can be iterated with |
AsyncIterator[AsyncStreamingResponse]
|
|
Example
Note
aiter_bytes(), aiter_text(), aiter_lines() and aread() read
each chunk on the client's thread pool, so other tasks keep running
while the server is producing data. The synchronous iter_*() and
read() methods are still available but block the event loop.
aclose
async
¶
Close the async client.
Releases the connection pool and shuts down this client's thread pool.
A request made after aclose() reopens both, with a
httpr.ClientReopenedWarning (see Client.close()). Calling it more
than once is a no-op.
AsyncStreamingResponse ¶
The streaming response yielded by AsyncClient.stream().
Wraps the StreamingResponse produced by the Rust core and adds async
iteration: aiter_bytes(), aiter_text(), aiter_lines() and aread()
fetch each chunk on the client's thread pool, so the event loop keeps
running other tasks while the server is producing the next one. Status,
headers, cookies and URL are available as soon as the context manager is
entered, before any of the body has been read.
The synchronous iter_bytes(), iter_text(), iter_lines() and read()
are still available, but each step blocks the event loop for as long as the
server takes to send the next chunk; use the async variants in async code.
Example
aiter_bytes ¶
aiter_text ¶
Iterate over the response body as text chunks, decoded with the response encoding.
aiter_lines ¶
aread
async
¶
Read the entire remaining response body without blocking the event loop.
aclose
async
¶
Close the streaming response and release its connection.
AsyncClient.stream() calls this when the async with block exits.
Closing never waits on I/O, so it runs on the event-loop thread.
raise_for_status ¶
Raise HTTPStatusError on a non-2xx status; returns self on success.