API Reference¶
chumicro_requests
¶
Non-blocking HTTP/1.1 client for CircuitPython, MicroPython, and CPython.
CaseInsensitiveDict
¶
HttpError
¶
Bases: Exception
Base class for every chumicro-requests failure.
HttpTimeoutError
¶
Bases: HttpError
Per-request timeout_ms budget elapsed before the response completed.
ParseState
¶
Streaming response parser states.
ResponseParser
¶
Streaming HTTP/1.1 response parser.
body
property
¶
Body bytes received so far (final once :attr:state is DONE).
__init__(*, max_body_bytes=DEFAULT_MAX_BODY_BYTES, max_header_bytes=DEFAULT_MAX_HEADER_BYTES, body_buffer=None, body_buffer_view=None, stream_body=False)
¶
Construct the parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_body_bytes
|
int
|
Cap on the buffered body size; ignored when stream_body is set. |
DEFAULT_MAX_BODY_BYTES
|
max_header_bytes
|
int
|
Cap on status-line and header bytes staged before the body. |
DEFAULT_MAX_HEADER_BYTES
|
body_buffer
|
bytearray | None
|
Optional caller-owned |
None
|
body_buffer_view
|
memoryview | None
|
Pre-cached |
None
|
stream_body
|
bool
|
When |
False
|
body_free()
¶
Writable staging space in the streamed-body window, in bytes.
read_body_into(buffer)
¶
Copy staged body bytes into caller-owned buffer; return the count.
discard_body()
¶
Drop every staged body byte and reset both cursors.
feed(chunk)
¶
Append chunk to the parser's buffer and advance the state.
feed_eof()
¶
Signal that the peer closed the connection.
encode_request(method, host, path, *, headers=None, body=None, user_agent=None)
¶
Encode an HTTP/1.1 request into bytes ready for the wire.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP verb, sent verbatim. |
required |
host
|
str
|
Value for the |
required |
path
|
str
|
Request-target, typically the URL path + query. |
required |
headers
|
CaseInsensitiveDict | dict | list | tuple | None
|
Optional |
None
|
body
|
bytes | None
|
Optional |
None
|
user_agent
|
str | None
|
Override for the default |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
Encoded request as |
Raises:
| Type | Description |
|---|---|
HttpURLError
|
A method, path, or header holds CR / LF / NUL or non-ASCII. |
parse_charset(content_type)
¶
Extract the charset=... parameter from a Content-Type header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str | None
|
Raw |
required |
Returns:
| Type | Description |
|---|---|
str
|
The detected charset name, or |
parse_url(url)
¶
Split url into (scheme, host, port, path).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
HTTP or HTTPS URL to split. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str, int, str]
|
4-tuple |
Raises:
| Type | Description |
|---|---|
HttpURLError
|
Bad scheme, missing host, or non-integer port. |
resolve_redirect_url(current_url, location)
¶
Resolve a Location header value against the current request URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_url
|
str
|
The URL of the request being redirected. |
required |
location
|
str
|
The raw |
required |
Returns:
| Type | Description |
|---|---|
str
|
Absolute URL the redirected request should target. |
Raises:
| Type | Description |
|---|---|
HttpURLError
|
current_url doesn't parse, or location is empty. |