Network¶
A small, dependency-free async HTTP client. Use fetch
for the common "call a JSON API" path; reach for httpx / aiohttp
if you need multipart, streaming, or HTTP/2.
Async HTTP client (pn.fetch).
A small, dependency-free coroutine wrapper around
:mod:urllib.request. Operates on bytes internally and exposes a
:class:Response with text(), json(), and bytes accessors.
The implementation is deliberately minimal — it covers the
"call a JSON API" path that's overwhelmingly the use case for mobile
apps. For streaming, multipart uploads, or HTTP/2, integrate
httpx / aiohttp directly; this module won't try to compete.
Example
Classes:
| Name | Description |
|---|---|
Response |
The result of a |
HTTPError |
Raised by |
Functions:
| Name | Description |
|---|---|
set_default_ssl_context |
Override the SSL context used by |
fetch |
Make an HTTP request and return a |
Response
dataclass
¶
The result of a fetch call.
Attributes:
| Name | Type | Description |
|---|---|---|
status |
int
|
HTTP status code (e.g. |
url |
str
|
Final URL after any redirects. |
headers |
Mapping[str, str]
|
Response headers, case-insensitive. |
content |
bytes
|
Raw response body. |
Methods:
| Name | Description |
|---|---|
text |
Decode |
json |
Parse the response body as JSON. |
raise_for_status |
Raise |
HTTPError
¶
set_default_ssl_context
¶
set_default_ssl_context(context: Optional[SSLContext]) -> None
Override the SSL context used by fetch.
None (the default) means urllib builds a context from
the system trust store. Tests can pass an unverified context.
fetch
async
¶
fetch(url: str, *, method: str = 'GET', headers: Optional[Mapping[str, str]] = None, body: Union[bytes, str, Mapping[str, Any], None] = None, params: Optional[Mapping[str, Any]] = None, timeout: float = 30.0) -> Response
Make an HTTP request and return a Response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Target URL. Relative URLs are not supported. |
required |
method
|
str
|
HTTP method ( |
'GET'
|
headers
|
Optional[Mapping[str, str]]
|
Optional request headers. |
None
|
body
|
Union[bytes, str, Mapping[str, Any], None]
|
Request body. |
None
|
params
|
Optional[Mapping[str, Any]]
|
Optional mapping of query-string parameters appended
to |
None
|
timeout
|
float
|
Seconds to wait for the response (excluding the time spent on DNS / connect). |
30.0
|
Returns:
| Type | Description |
|---|---|
Response
|
A |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the request doesn't complete within
|
OSError
|
For network errors (DNS failure, connection refused,
etc.) — re-raised from |
Patterns¶
- Inside a component: pair with
use_queryfor loading/error state and automatic cancellation on unmount. - In an event handler: wrap an
async definpn.run_asyncso a syncon_clickcan drive an awaitable request. - Mutations: pair with
use_mutationto trackloading/errorfor POST/PUT/DELETE flows.