Urllib Client¶
urllib_client
¶
Standard-library HTTP client implementation of ExternalServicePort.
Uses urllib.request wrapped in asyncio.to_thread() to provide an
async HTTP client with zero external dependencies.
This adapter is constrained to str request/response bodies — it natively
encodes request bodies as UTF-8 and decodes response bodies as UTF-8. For
other content types (bytes, JSON, etc.), use a dedicated adapter or a
wrapper that handles serialization.
URLLibClient
¶
Bases: ExternalServicePort[str, str]
HTTP client backed by Python's urllib.request + asyncio.to_thread.
This adapter provides async HTTP methods without requiring external
dependencies like httpx or aiohttp. Request and response bodies
are encoded/decoded as UTF-8 strings.
Note
For production use with high concurrency or non-string payloads,
consider an adapter backed by httpx or aiohttp instead.
Raises:
| Type | Description |
|---|---|
HTTPError
|
On HTTP 4xx/5xx responses. |
URLError
|
On network/connection failures. |
ValueError
|
On malformed URLs. |
Source code in src/forging_blocks/infrastructure/http_client/urllib_client.py
request(method: str, url: str, headers: dict[str, str] | None = None, body: str | None = None) -> str
async
¶
Send an HTTP request and return the response body as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
HTTP method (GET, POST, PUT, DELETE, etc.). |
required |
url
|
str
|
The target URL. |
required |
headers
|
dict[str, str] | None
|
Optional HTTP headers. |
None
|
body
|
str | None
|
Optional request body string (UTF-8 encoded). |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The response body decoded as UTF-8 string. |
Raises:
| Type | Description |
|---|---|
HTTPError
|
On 4xx/5xx HTTP responses. |
URLError
|
On network or connection failures. |
ValueError
|
On malformed URLs. |
Source code in src/forging_blocks/infrastructure/http_client/urllib_client.py
get(url: str, headers: dict[str, str] | None = None) -> str
async
¶
post(url: str, body: str | None = None, headers: dict[str, str] | None = None) -> str
async
¶
Send an HTTP POST request.
Source code in src/forging_blocks/infrastructure/http_client/urllib_client.py
put(url: str, body: str | None = None, headers: dict[str, str] | None = None) -> str
async
¶
Send an HTTP PUT request.
Source code in src/forging_blocks/infrastructure/http_client/urllib_client.py
delete(url: str, headers: dict[str, str] | None = None) -> str
async
¶
Send an HTTP DELETE request.