Skip to content

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

Both clients include:

  • Automatic retry with exponential backoff
  • 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

Both clients use automatic retry for transient errors:

  • Connection errors
  • Timeout errors
  • HTTP 5xx 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
client = HttpClient(HuwiseConfig.from_env())
response = client.get("/datasets")
data = response.json()

Initialize the HTTP client.

PARAMETER DESCRIPTION
config

HuwiseConfig instance with API credentials.

TYPE: HuwiseConfig

Source code in src/huwise_utils_py/http.py
def __init__(self, config: HuwiseConfig) -> None:
    """Initialize the HTTP client.

    Args:
        config: HuwiseConfig instance with API credentials.
    """
    self.config = config

get(endpoint: str, **kwargs: Any) -> httpx.Response

Make a GET request.

PARAMETER DESCRIPTION
endpoint

API endpoint (e.g., "/datasets/da_123").

TYPE: str

**kwargs

Additional arguments for httpx.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

RAISES DESCRIPTION
HTTPStatusError

If response indicates an error.

Source code in src/huwise_utils_py/http.py
@retry(HTTP_ERRORS_TO_RETRY, tries=6, delay=5, backoff=1)
def get(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make a GET request.

    Args:
        endpoint: API endpoint (e.g., "/datasets/da_123").
        **kwargs: Additional arguments for httpx.

    Returns:
        HTTP response object.

    Raises:
        httpx.HTTPStatusError: If response indicates an error.
    """
    url = f"{self.config.base_url}{endpoint}"
    with httpx.Client(timeout=DEFAULT_TIMEOUT) as client:
        response = client.get(url, headers=self.config.headers, **kwargs)
        response.raise_for_status()
        return response

post(endpoint: str, **kwargs: Any) -> httpx.Response

Make a POST request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx (e.g., json=data).

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
@retry(HTTP_ERRORS_TO_RETRY, tries=6, delay=5, backoff=1)
def post(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make a POST request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx (e.g., json=data).

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    with httpx.Client(timeout=DEFAULT_TIMEOUT) as client:
        response = client.post(url, headers=self.config.headers, **kwargs)
        response.raise_for_status()
        return response

put(endpoint: str, **kwargs: Any) -> httpx.Response

Make a PUT request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx (e.g., json=data).

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
@retry(HTTP_ERRORS_TO_RETRY, tries=6, delay=5, backoff=1)
def put(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make a PUT request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx (e.g., json=data).

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    with httpx.Client(timeout=DEFAULT_TIMEOUT) as client:
        response = client.put(url, headers=self.config.headers, **kwargs)
        response.raise_for_status()
        return response

patch(endpoint: str, **kwargs: Any) -> httpx.Response

Make a PATCH request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx (e.g., json=data).

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
@retry(HTTP_ERRORS_TO_RETRY, tries=6, delay=5, backoff=1)
def patch(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make a PATCH request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx (e.g., json=data).

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    with httpx.Client(timeout=DEFAULT_TIMEOUT) as client:
        response = client.patch(url, headers=self.config.headers, **kwargs)
        response.raise_for_status()
        return response

delete(endpoint: str, **kwargs: Any) -> httpx.Response

Make a DELETE request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
@retry(HTTP_ERRORS_TO_RETRY, tries=6, delay=5, backoff=1)
def delete(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make a DELETE request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx.

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    with httpx.Client(timeout=DEFAULT_TIMEOUT) as client:
        response = client.delete(url, headers=self.config.headers, **kwargs)
        response.raise_for_status()
        return response

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
client = AsyncHttpClient(HuwiseConfig.from_env())
async with client.session() as session:
    response = await session.get(f"{client.config.base_url}/datasets")

Initialize the async HTTP client.

PARAMETER DESCRIPTION
config

HuwiseConfig instance with API credentials.

TYPE: HuwiseConfig

Source code in src/huwise_utils_py/http.py
def __init__(self, config: HuwiseConfig) -> None:
    """Initialize the async HTTP client.

    Args:
        config: HuwiseConfig instance with API credentials.
    """
    self.config = config

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
async with client.session() as session:
    tasks = [session.get(url) for url in urls]
    responses = await asyncio.gather(*tasks)
Source code in src/huwise_utils_py/http.py
@asynccontextmanager
async def session(self) -> AsyncGenerator[httpx.AsyncClient, None]:
    """Create an async HTTP session context.

    Yields:
        Configured AsyncClient with proper timeouts and limits.

    Example:
        ```python
        async with client.session() as session:
            tasks = [session.get(url) for url in urls]
            responses = await asyncio.gather(*tasks)
        ```
    """
    async with httpx.AsyncClient(
        timeout=DEFAULT_TIMEOUT,
        limits=DEFAULT_LIMITS,
        headers=self.config.headers,
    ) as client:
        yield client

get(endpoint: str, **kwargs: Any) -> httpx.Response async

Make an async GET request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
async def get(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make an async GET request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx.

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    async with self.session() as client:
        response = await client.get(url, **kwargs)
        response.raise_for_status()
        return response

post(endpoint: str, **kwargs: Any) -> httpx.Response async

Make an async POST request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
async def post(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make an async POST request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx.

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    async with self.session() as client:
        response = await client.post(url, **kwargs)
        response.raise_for_status()
        return response

put(endpoint: str, **kwargs: Any) -> httpx.Response async

Make an async PUT request.

PARAMETER DESCRIPTION
endpoint

API endpoint.

TYPE: str

**kwargs

Additional arguments for httpx.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Response

HTTP response object.

Source code in src/huwise_utils_py/http.py
async def put(self, endpoint: str, **kwargs: Any) -> httpx.Response:
    """Make an async PUT request.

    Args:
        endpoint: API endpoint.
        **kwargs: Additional arguments for httpx.

    Returns:
        HTTP response object.
    """
    url = f"{self.config.base_url}{endpoint}"
    async with self.session() as client:
        response = await client.put(url, **kwargs)
        response.raise_for_status()
        return response