HTTP Clients
Low-level HTTP clients for making API requests.
Overview
The library provides two HTTP clients:
- HttpClient: Synchronous client for simple use cases
- AsyncHttpClient: Asynchronous client for concurrent operations
HttpClient includes automatic retry with exponential backoff for transient
failures. AsyncHttpClient does not retry requests but raises the same
HuwiseAutomationError on error responses.
Both clients provide:
- Proper timeout handling
- Connection pooling (async)
- HTTP/2 support (async)
Usage
Synchronous Client
from huwise_utils_py import HuwiseConfig, HttpClient
config = HuwiseConfig.from_env()
client = HttpClient(config)
# GET request
response = client.get("/datasets/?limit=10")
data = response.json()
# POST request
response = client.post("/datasets/da_123/publish/")
# PUT request with JSON body
response = client.put("/datasets/da_123/metadata/", json=metadata)
Asynchronous Client
import asyncio
from huwise_utils_py import HuwiseConfig, AsyncHttpClient
config = HuwiseConfig.from_env()
client = AsyncHttpClient(config)
async def fetch_datasets():
async with client.session() as session:
# Make concurrent requests
tasks = [
session.get(f"{config.base_url}/datasets/{dataset_id}")
for dataset_id in ["100123", "100456", "100789"]
]
responses = await asyncio.gather(*tasks)
return [r.json() for r in responses]
# Run async function
datasets = asyncio.run(fetch_datasets())
Custom Requests
For advanced use cases, you can use the clients directly:
# With query parameters
response = client.get("/datasets/", params={"limit": 100, "offset": 0})
# With custom headers (merged with auth headers)
response = client.get("/datasets/", headers={"X-Custom": "value"})
Retry Logic
HttpClient retries only transient failures:
- Connection and TLS errors
- Read/write/connect timeouts
- HTTP 5xx responses
- HTTP 429 (rate limit)
4xx responses (except 429) are not retried. On error, the client raises
HuwiseAutomationError (a subclass of httpx.HTTPStatusError) with a message
that includes a preview of the response body and, when JSON, a parsed detail
attribute for validation errors.
Default settings:
- 6 retry attempts
- 5 second initial delay
- Exponential backoff (delay doubles each retry)
API Reference
HttpClient(config: HuwiseConfig)
Synchronous HTTP client for Huwise API.
Provides GET, POST, PUT, PATCH, DELETE methods with automatic retry logic and proper header handling.
| ATTRIBUTE | DESCRIPTION |
|---|---|
config |
HuwiseConfig instance for API credentials.
|
Example
Initialize the HTTP client.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
HuwiseConfig instance with API credentials.
TYPE:
|
Source code in src/huwise_utils_py/http.py
get(endpoint: str, **kwargs: Any) -> httpx.Response
Make a GET request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint (e.g., "/datasets/da_123").
TYPE:
|
**kwargs
|
Additional arguments for httpx.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx (subclass of
:class: |
Source code in src/huwise_utils_py/http.py
post(endpoint: str, **kwargs: Any) -> httpx.Response
Make a POST request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx (e.g., json=data).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
put(endpoint: str, **kwargs: Any) -> httpx.Response
Make a PUT request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx (e.g., json=data).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
patch(endpoint: str, **kwargs: Any) -> httpx.Response
Make a PATCH request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx (e.g., json=data).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
delete(endpoint: str, **kwargs: Any) -> httpx.Response
Make a DELETE request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
AsyncHttpClient(config: HuwiseConfig)
Asynchronous HTTP client for concurrent Huwise API operations.
Optimized for bulk operations with connection pooling and HTTP/2 support.
| ATTRIBUTE | DESCRIPTION |
|---|---|
config |
HuwiseConfig instance for API credentials.
|
Example
Initialize the async HTTP client.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
HuwiseConfig instance with API credentials.
TYPE:
|
Source code in src/huwise_utils_py/http.py
session() -> AsyncGenerator[httpx.AsyncClient, None]
async
Create an async HTTP session context.
| YIELDS | DESCRIPTION |
|---|---|
AsyncGenerator[AsyncClient, None]
|
Configured AsyncClient with proper timeouts and limits. |
Example
Source code in src/huwise_utils_py/http.py
get(endpoint: str, **kwargs: Any) -> httpx.Response
async
Make an async GET request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
post(endpoint: str, **kwargs: Any) -> httpx.Response
async
Make an async POST request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |
Source code in src/huwise_utils_py/http.py
put(endpoint: str, **kwargs: Any) -> httpx.Response
async
Make an async PUT request.
| PARAMETER | DESCRIPTION |
|---|---|
endpoint
|
API endpoint.
TYPE:
|
**kwargs
|
Additional arguments for httpx.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Response
|
HTTP response object. |
| RAISES | DESCRIPTION |
|---|---|
HuwiseAutomationError
|
If response status is 4xx or 5xx. |