Skip to content

quantpylib.throttler.httpx

HTTPClient

An asynchronous HTTP aiosonic client for fast network requests and (de)serialization of data packets. Automatic retries and error handling.

Parameters:

Name Type Description Default
base_url str

The base URL for all requests. Defaults to an empty string.

''
json_decoder callable

The JSON decoder function to use. Defaults to orjson.loads.

loads

cleanup() async

Clean up the HTTP client session, ensuring proper resource deallocation.

Returns:

Type Description

None

handler(response, cargs={}) async

Handle the HTTP response, returning the JSON-decoded content or raising an HTTPException on error.

Parameters:

Name Type Description Default
response HTTPResponse

The HTTP response object to handle.

required

Returns:

Name Type Description
dict

The JSON-decoded response if the status code is less than 400.

Raises:

Type Description
HTTPException

If the response status code is 400 or higher.

request(url='', endpoint='', method='GET', headers={'content-type': 'application/json'}, params=None, data=None, return_exceptions=False, aiohttp_fallback=False, retries=2) async

Make an HTTP request and handle retries on failure.

Parameters:

Name Type Description Default
url str

The full URL for the request. If not provided, base_url + endpoint is used. Defaults to an empty string.

''
endpoint str

The endpoint to append to the base URL. Defaults to an empty string.

''
method str

The HTTP method to use. Defaults to 'GET'.

'GET'
headers dict

The headers to include in the request. Defaults to {"content-type": "application/json"}.

{'content-type': 'application/json'}
params dict

The URL parameters to include in the request. Defaults to None.

None
data dict or str

The data to include in the request body. Defaults to None.

None
return_exceptions bool

Whether to return exceptions instead of raising them. Defaults to False. No retries if True.

False
retries int

The number of retries if the request fails. Defaults to 2.

2

Returns:

Name Type Description
dict

The parsed JSON response if the request is successful.

Raises:

Type Description
HTTPException

If the response status code is 400 or higher.