hackagent.client
MultipartFixClient Objects
class MultipartFixClient(httpx.Client)
A custom httpx.Client that addresses potential issues with multipart/form-data requests generated by openapi-python-client.
Specifically, it ensures that if a 'Content-Type' header is manually set to "multipart/form-data" without a boundary, it is removed. This allows httpx to correctly generate the 'Content-Type' header, including the boundary, based on the 'files' provided in the request. This is crucial for robust file uploads.
request
def request(method: str, url: Union[str, httpx.URL],
**kwargs: Any) -> httpx.Response
Overrides the default request method to inspect and potentially modify headers for multipart/form-data requests.
If 'files' are present and 'Content-Type' is 'multipart/form-data' without a boundary, this method removes the problematic 'Content-Type' header to let httpx handle its generation.
AsyncMultipartFixClient Objects
class AsyncMultipartFixClient(httpx.AsyncClient)
An asynchronous custom httpx.AsyncClient that addresses potential issues with
multipart/form-data requests, similar to MultipartFixClient
.
It ensures correct 'Content-Type' header generation for multipart requests when using 'files' in an asynchronous context.
request
async def request(method: str, url: Union[str, httpx.URL],
**kwargs: Any) -> httpx.Response
Overrides the default asynchronous request method to inspect and potentially modify headers for multipart/form-data requests.
If 'files' are present and 'Content-Type' is 'multipart/form-data' without a boundary, this method removes the problematic 'Content-Type' header to let httpx handle its generation.
Client Objects
@define
class Client()
A base client for keeping track of data related to API interaction.
This class manages common HTTP client configurations such as base URL, cookies,
headers, timeout, SSL verification, and redirect behavior. It serves as a
foundation for more specialized clients (e.g., AuthenticatedClient
).
The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
base_url: The base URL for the API. All requests are made relative to this.
cookies: A dictionary of cookies to be sent with every request.
headers: A dictionary of headers to be sent with every request.
timeout: The maximum time (httpx.Timeout) a request can take.
API functions will raise httpx.TimeoutException
if exceeded.
verify_ssl: Whether to verify the SSL certificate (True/False), or a path
to CA bundle, or an ssl.SSLContext
instance.
follow_redirects: Whether to follow redirects. Defaults to False
.
httpx_args: Additional keyword arguments passed to the httpx.Client
and httpx.AsyncClient
constructors.
Attributes:
raise_on_unexpected_status
- IfTrue
, raiseserrors.UnexpectedStatus
if the API returns a status code not documented in the OpenAPI spec.
with_headers
def with_headers(headers: Dict[str, str]) -> "Client"
Creates a new client instance with additional or updated headers.
with_cookies
def with_cookies(cookies: Dict[str, str]) -> "Client"
Creates a new client instance with additional or updated cookies.
with_timeout
def with_timeout(timeout: httpx.Timeout) -> "Client"
Creates a new client instance with an updated timeout.
set_httpx_client
def set_httpx_client(client: httpx.Client) -> "Client"
Manually sets the underlying httpx.Client
instance.
Note: This will override any other client settings like cookies, headers,
and timeout that were configured on this Client
instance.
The provided client should ideally be MultipartFixClient
or compatible
if multipart request fixes are desired.
get_httpx_client
def get_httpx_client() -> httpx.Client
Retrieves the underlying httpx.Client
.
If no client has been set or previously constructed, a new httpx.Client
(or MultipartFixClient
in derived classes like AuthenticatedClient
)
is initialized with the current configuration (base_url, headers, etc.).
__enter__
def __enter__() -> "Client"
Enters a context manager for the synchronous httpx client.
__exit__
def __exit__(*args: Any, **kwargs: Any) -> None
Exits the context manager for the synchronous httpx client.
set_async_httpx_client
def set_async_httpx_client(async_client: httpx.AsyncClient) -> "Client"
Manually sets the underlying httpx.AsyncClient
instance.
Note: This will override any other client settings like cookies, headers,
and timeout. The provided client should ideally be AsyncMultipartFixClient
or compatible if multipart request fixes are desired.
get_async_httpx_client
def get_async_httpx_client() -> httpx.AsyncClient
Retrieves the underlying httpx.AsyncClient
.
If no client has been set, a new httpx.AsyncClient
(or
AsyncMultipartFixClient
in derived classes) is initialized.
__aenter__
async def __aenter__() -> "Client"
Enters a context manager for the asynchronous httpx client.
__aexit__
async def __aexit__(*args: Any, **kwargs: Any) -> None
Exits the context manager for the asynchronous httpx client.
AuthenticatedClient Objects
@define
class AuthenticatedClient()
A client authenticated for use on secured API endpoints.
This class extends the basic client configuration with authentication details,
specifically a token and its associated prefix for the Authorization header.
It defaults to using MultipartFixClient
and AsyncMultipartFixClient
for
its underlying synchronous and asynchronous HTTP clients respectively, to handle
potential multipart request issues.
Accepted keyword arguments for construction are the same as for the Client
class, plus token
, prefix
, and auth_header_name
.
Attributes:
token
- The authentication token.prefix
- The prefix for the token in the Authorization header (e.g., "Bearer"). Defaults to "Bearer". If an empty string, only the token is used.auth_header_name
- The name of the HTTP header used for authorization. Defaults to "Authorization".raise_on_unexpected_status
- SeeClient
class.AsyncMultipartFixClient
1 - SeeClient
class. Defaults to "https://hackagent.dev/".AsyncMultipartFixClient
3 - SeeClient
class.AsyncMultipartFixClient
5 - SeeClient
class.AsyncMultipartFixClient
7 - SeeClient
class.AsyncMultipartFixClient
9 - SeeClient
class.Client
1 - SeeClient
class.Client
3 - SeeClient
class.
__attrs_post_init__
def __attrs_post_init__()
Ensures _base_url
is set to its default if None
was explicitly passed.
with_headers
def with_headers(headers: Dict[str, str]) -> "AuthenticatedClient"
Creates a new authenticated client instance with additional or updated headers.
with_cookies
def with_cookies(cookies: Dict[str, str]) -> "AuthenticatedClient"
Creates a new authenticated client instance with additional or updated cookies.
with_timeout
def with_timeout(timeout: httpx.Timeout) -> "AuthenticatedClient"
Creates a new authenticated client instance with an updated timeout.
set_httpx_client
def set_httpx_client(client: httpx.Client) -> "AuthenticatedClient"
Manually sets the underlying httpx.Client
.
It is recommended that the provided client is an instance of
MultipartFixClient
or a compatible class to ensure correct handling
of multipart/form-data requests. If a different type of client is set,
the multipart fix behavior might be lost.
This will override other client settings like cookies, headers, and timeout.
get_httpx_client
def get_httpx_client() -> httpx.Client
Retrieves the underlying httpx.Client
, defaulting to MultipartFixClient
.
If no client has been set, a new MultipartFixClient
is initialized.
The client is configured with the AuthenticatedClient
's settings
(base_url, cookies, timeout, etc.) and the necessary Authorization header
is automatically added to its default headers.
__enter__
def __enter__() -> "AuthenticatedClient"
Enters a context manager for the synchronous httpx client.
__exit__
def __exit__(*args: Any, **kwargs: Any) -> None
Exits the context manager for the synchronous httpx client.
set_async_httpx_client
def set_async_httpx_client(
async_client: httpx.AsyncClient) -> "AuthenticatedClient"
Manually sets the underlying httpx.AsyncClient
.
It is recommended that the provided client is an instance of
AsyncMultipartFixClient
or compatible. This will override other
client settings.
get_async_httpx_client
def get_async_httpx_client() -> httpx.AsyncClient
Retrieves the underlying httpx.AsyncClient
, defaulting to AsyncMultipartFixClient
.
If no client has been set, a new AsyncMultipartFixClient
is initialized
with the AuthenticatedClient
's settings and Authorization header.
__aenter__
async def __aenter__() -> "AuthenticatedClient"
Enters a context manager for the asynchronous httpx client.
__aexit__
async def __aexit__(*args: Any, **kwargs: Any) -> None
Exits the context manager for the asynchronous httpx client.