arthur_bench.client.http#

Submodules#

arthur_bench.client.http.helper.construct_url(*parts: str, validate=True, default_https=True) str#

Construct a URL from various parts

Useful for joining pieces which may or may not have leading and/or trailing slashes. e.g. construct_url(”https://arthur.ai/”, “/api/v3”, “/users”) will yield the same valid url as construct_url(”https://arthur.ai”, “api/v3/”, “users/”): “https://arthur.ai/api/v3/users”.

Parameters:
  • validate – if True, validate that the URL is valid

  • default_https – if True, allow urls without a scheme and use https by default

  • parts – strings from which to construct the url

Returns:

a fully joined url, with NO trailing slash

class arthur_bench.client.http.requests.HTTPClient(base_url: str, path_prefix: str | None = None, default_headers: Dict[str, str] | None = None, verify_ssl: bool = True, timeout_sec: float = 300.0, allow_insecure: bool = True, header_refresh_func: Callable[[], Tuple[Dict[str, str], timedelta]] | None = None)#

Bases: object

A requests-based HTTP Client intended for interacting with JSON-based APIs. Supports response validation, retries, connection reuse, and multipart requests.

delete(endpoint: str, headers: Dict[str, str] | None = None, return_raw_response: bool = False, params: Dict | bytes | None = None, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP DELETE request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • headers – headers to use for this request in addition to the client default headers

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • params – query parameters to add to the request

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, don’t validate

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

get(endpoint: str, headers: Dict[str, str] | None = None, params: Dict | bytes | None = None, return_raw_response: bool = False, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP GET request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • headers – headers to use for this request in addition to the client default headers

  • params – query parameters to add to the request

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, allow any 2XX

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

Raises:
patch(endpoint: str, json: Dict | List | str | bytes | None = None, files: Dict[str, BinaryIO] | List[Tuple] | Dict[str, Tuple] | None = None, headers: Dict[str, str] | None = None, params: Dict | bytes | None = None, return_raw_response: bool = False, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP POST request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • headers – headers to use for this request in addition to the client default headers

  • json – data to send as JSON, either a string/bytes to send directly or a dictionary/list to serialize. if files is also supplied, this should be a map from name to content, to be sent along with the files as a multipart request

  • files – a map from file names to file-like objects, to be sent as multipart/form-data

  • params – query parameters to add to the request

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, don’t validate

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

post(endpoint: str, json: Dict | List | str | bytes | None = None, files: Dict[str, BinaryIO] | List[Tuple] | Dict[str, Tuple] | None = None, headers: Dict[str, str] | None = None, params: Dict | bytes | None = None, return_raw_response: bool = False, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP POST request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • headers – headers to use for this request in addition to the client default headers

  • json – data to send as JSON, either a string/bytes to send directly or a dictionary/list to serialize. if files is also supplied, this should be a map from name to content, to be sent along with the files as a multipart request

  • files – a map from file names to file-like objects, to be sent as multipart/form-data

  • params – query parameters to add to the request

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, don’t validate

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

put(endpoint: str, json: Dict | List | str | bytes | None = None, files: Dict[str, BinaryIO] | List[Tuple] | Dict[str, Tuple] | None = None, headers: Dict[str, str] | None = None, params: Dict | bytes | None = None, return_raw_response: bool = False, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP PUT request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • headers – headers to use for this request in addition to the client default headers

  • json – data to send as JSON, either a string/bytes to send directly or a dictionary/list to serialize. if files is also supplied, this should be a map from name to content, to be sent along with the files as a multipart request

  • files – a map from file names to file-like objects, to be sent as multipart/form-data

  • params – query parameters to add to the request

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, don’t validate

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

send(endpoint: str, method: str = 'GET', json: Dict | List | str | bytes | None = None, files: Dict[str, BinaryIO] | List[Tuple] | Dict[str, Tuple] | None = None, headers: Dict[str, str] | None = None, params: Dict | bytes | None = None, return_raw_response: bool = False, retries: int = 0, validate_response_status: bool = True, validation_response_code: int | None = None) Dict | List | bytes | BytesIO | Response#

Send an HTTP request

Parameters:
  • endpoint – the specific endpoint to append to the client URL

  • method – the HTTP method to use

  • headers – headers to use for this request in addition to the client

d efault headers :param json: data to send as JSON, either a string/bytes to send directly or a

dictionary/list to serialize. if files is also supplied, this should be a map from name to content, to be sent along with the files as a multipart request

Parameters:
  • files – a map from file names to file-like objects, to be sent as multipart/form-data

  • params – query parameters to add to the request

  • return_raw_response – if true, return the requests.Response object received; otherwise attempt to parse the response

  • retries – number of times to retry the request on failure. uses exponential backoff

  • validate_response_status – if True, raise an ArthurException if the status code is not 2XX or does not match validation_response_code

  • validation_response_code – expected status code of the response to validate. if None, allow any 2XX

Returns:

if return_raw_response is true, return the requests.Response object received; otherwise attempt to parse the response

Raises:
set_path_prefix(path_prefix: str) None#

Update the client’s path prefix

This update the path prefix which is prepended to ‘endpoint’ paths.

arthur_bench.client.http.validation.validate_multistatus_response_and_get_failures(response: Response, raise_on_failures: bool = False) Tuple[List[dict], List[dict]]#

Validate a 207 MultiStatus response and return the failures it contains.

Parameters:
  • response

    requests.Response object to validate, with the following body format:

    {
        "counts": {
            "success": 0,
            "failure": 0,
            "total": 0
        },
        "results": [
            {
                "message": "success",
                "status": 200
            }
        ]
    }
    

  • raise_on_failures – if True, raise an exception if the response contains any

failures :return: a tuple of two lists: user-caused failures and internal failures :raises ArthurInternalValueError: If the response does not have 207 status code, or is incorrectly formatted,

or ‘counts’ and ‘results’ do not agree

Raises:

ResponseClientError – if raise_on_failures and the response contains only

client errors :raises ResponseServerError: if raise_on_failures and the response contains server errors

arthur_bench.client.http.validation.validate_response_status(response_or_code: Response | int, expected_status_code: int | None = None, allow_redirects: bool | None = False) None#

Validate the status code of a requests.Response object or (int) status code. :param response_or_code: the requests.Response object or status code to validate :param expected_status_code: the expected status code to check for. If None, all codes <300 will be valid, and 3XX codes will be subject to allow_redirects :param allow_redirects: if True will not raise an exception for 3XX status codes :return: None :raises InternalValueError: if expected_status_code is not None and does not match the response code :raises ResponseServerError: if the response has a 5XX status code :raises ResponseClientError: if the response has a 4XX status code :raises ResponseRedirectError: if the response has a 3XX status code