5.7. http

HTTP client.

class axonius_api_client.http.Http(url: str, connect_timeout: int = 5, response_timeout: int = 900, certpath: Union[str, pathlib.Path, None] = None, certwarn: bool = True, certverify: bool = False, cert_client_both: Union[str, pathlib.Path, None] = None, cert_client_cert: Union[str, pathlib.Path, None] = None, cert_client_key: Union[str, pathlib.Path, None] = None, http_proxy: Optional[str] = None, https_proxy: Optional[str] = None, save_last: bool = True, save_history: bool = False, log_level: Union[int, str] = 'debug', log_level_urllib: str = 'warning', log_request_attrs: Optional[List[str]] = None, log_response_attrs: Optional[List[str]] = None, log_request_body: bool = False, log_response_body: bool = False)[source]

Bases: object

HTTP client wrapper around requests.Session.

__init__(url: str, connect_timeout: int = 5, response_timeout: int = 900, certpath: Union[str, pathlib.Path, None] = None, certwarn: bool = True, certverify: bool = False, cert_client_both: Union[str, pathlib.Path, None] = None, cert_client_cert: Union[str, pathlib.Path, None] = None, cert_client_key: Union[str, pathlib.Path, None] = None, http_proxy: Optional[str] = None, https_proxy: Optional[str] = None, save_last: bool = True, save_history: bool = False, log_level: Union[int, str] = 'debug', log_level_urllib: str = 'warning', log_request_attrs: Optional[List[str]] = None, log_response_attrs: Optional[List[str]] = None, log_request_body: bool = False, log_response_body: bool = False)[source]

HTTP client wrapper around requests.Session.

Notes

  • If certpath is supplied, certverify is ignored
  • private key supplied to cert_client_key or cert_client_both can NOT be password encrypted
Parameters:
  • url (str or ParserUrl) – URL to connect to
  • connect_timeout (int, optional) – default TIMEOUT_CONNECT - seconds to wait for connections to open to url
  • response_timeout (int, optional) – default TIMEOUT_RESPONSE - seconds to wait for responses from url
  • certpath (str or pathlib.Path, optional) – default None - path to CA bundle file to use when verifing certs offered by url instead of the system CA bundle
  • certwarn (bool, optional) –

    default True - show warnings from requests about certs offered by url that are self signed:

    • if True show warning only the first time it happens
    • if False never show warning
    • if None show warning every time it happens
  • certverify (bool, optional) –

    default False - control validation of certs offered by url:

    • if True raise exception if cert is invalid/self-signed
    • if False only raise exception if cert is invalid
  • cert_client_both (str or pathlib.Path, optional) – default None - path to cert file containing both the private key and cert to offer to url
  • cert_client_cert (str or pathlib.Path, optional) – default None - path to cert file to offer to url (must also supply cert_client_key)
  • cert_client_key (str or pathlib.Path, optional) – default None - path to private key file for cert_client_cert to offer to url (must also supply cert_client_cert)
  • http_proxy (str, optional) – default None - proxy to use when making http requests to url
  • https_proxy (str, optional) – default None - proxy to use when making https requests to url
  • save_last (bool, optional) –

    default True -

  • save_history (bool, optional) –

    default True -

    • if True append responses to HISTORY
    • if False do not append responses to HISTORY
  • log_level (str) – default axonius_api_client.LOG_LEVEL_HTTP - logging level to use for this objects logger
  • log_level_urllib (str) – default "warning" - logging level to use for this urllib logger
  • log_request_attrs (bool) –

    default None - control logging of request attributes:

    • if True, log request attributes defined in axonius_api_client.LOG_REQUEST_ATTRS_VERBOSE
    • if False, log request attributes defined in axonius_api_client.LOG_REQUEST_ATTRS_BRIEF
    • if None, do not log any request attributes
  • log_response_attrs (bool) –

    default None - control logging of response attributes:

    • if True, log response attributes defined in axonius_api_client.LOG_RESPONSE_ATTRS_VERBOSE
    • if False, log response attributes defined in axonius_api_client.LOG_RESPONSE_ATTRS_BRIEF
    • if None, do not log any response attributes
  • log_request_body (bool) –

    default False - control logging of request bodies:

    • if True, log request bodies
    • if False, do not log request bodies
  • log_response_body (bool) –

    default False - control logging of response bodies:

    • if True, log response bodies
    • if False, do not log response bodies
Raises:
  • HttpError – if either cert_client_cert or cert_client_key are supplied, and the other is not supplied
  • HttpError – if any of cert_path, cert_client_cert, cert_client_key, or cert_client_both are supplied and the file does not exist
LOG = None

Logger for this object.

Type:logging.Logger
url = None

URL to connect to

Type:str
LAST_REQUEST = None

last request sent

Type:requests.PreparedRequest
LAST_RESPONSE = None

last response received

Type:requests.Response
HISTORY = None

all responses received.

Type:list of requests.Response
SAVE_LAST = None

save requests to LAST_REQUEST and responses to LAST_RESPONSE

Type:bool
SAVEHISTORY = None

Append all responses to HISTORY

Type:bool
CONNECT_TIMEOUT = None

seconds to wait for connections to open to url

Type:int
RESPONSE_TIMEOUT = None

seconds to wait for responses from url

Type:int
session = None

session object to use

Type:requests.Session
LOG_HIDE_HEADERS = None

Headers to hide when logging.

LOG_REQUEST_BODY = None

Log the full request body.

Type:bool
LOG_RESPONSE_BODY = None

Log the full response body.

Type:bool
__call__(path: Optional[str] = None, route: Optional[str] = None, method: str = 'get', data: Optional[str] = None, params: Optional[dict] = None, headers: Optional[dict] = None, json: Optional[dict] = None, files: tuple = None, **kwargs) → requests.models.Response[source]

Create, prepare, and then send a request using session.

Parameters:
  • path (str, optional) – default None - path to append to url
  • route (str, optional) – default None - route to append to url
  • method (str, optional) – default "get" - method to use
  • data (str, optional) – default None - body to send
  • params (dict, optional) – default None - parameters to url encode
  • headers (dict, optional) – default None - headers to send
  • json (dict, optional) – default None - obj to encode as json
  • files (tuple of tuple, optional) – default None - files to send
  • **kwargs

    overrides for object attributes

    • connect_timeout (int): default CONNECT_TIMEOUT - seconds to wait for connection to open to url
    • response_timeout (int): default RESPONSE_TIMEOUT - seconds to wait for for response from url
    • proxies (dict): default None - use custom proxies instead of proxies defined in session
    • verify (bool or str): default None - use custom verification of cert offered by url instead of verification defined in session
    • cert (str): default None - use custom client cert to offer to url cert defined in session
Returns:

raw response object

Return type:

requests.Response

user_agent

Value to use in User-Agent header.

Returns:user agent string
Return type:str
_do_log_request(request: requests.models.PreparedRequest)[source]

Do it.

_do_log_response(response: requests.models.Response)[source]

Do it.

log_request_attrs

Get the request attributes that should be logged.

log_response_attrs

Get the response attributes that should be logged.

log_body(body: str, body_type: str)[source]

Pass.

class axonius_api_client.http.ParserUrl(url: str, default_scheme: str = 'https')[source]

Bases: object

Parse a URL and ensure it has the neccessary bits.

__init__(url: str, default_scheme: str = 'https')[source]

Parse a URL and ensure it has the neccessary bits.

Parameters:
  • url (str) – URL to parse
  • default_scheme (str, optional) – default "https" - default scheme to use if url does not contain a scheme
Raises:

HttpError – if parsed URL winds up without a hostname, port, or scheme.

parsed = None

second pass of parsing URL

Type:urllib.parse.ParseResult
hostname

Hostname part from ParserUrl.parsed.

Returns:hostname value
Return type:str
port

Port part from ParserUrl.parsed.

Returns
int: port value
scheme

Scheme part from ParserUrl.parsed.

Returns:scheme value
Return type:str
url

Get scheme, hostname, and port from ParserUrl.parsed.

Returns:schema, hostname, and port unparsed values
Return type:str
url_full

Get full URL from ParserUrl.parsed.

Returns:full unparsed url
Return type:str
parsed_str

Get a str value of ParserUrl.parsed.

Returns:str value of ParserUrl.parsed
Return type:str
make_netloc(host: str, port: Union[str, int]) → str[source]

Create netloc from host and port.

Parameters:
  • host (str) – host part to use in netloc
  • port (str) – port part to use in netloc
Returns:

host and port values joined by :

Return type:

str

reparse(parsed: urllib.parse.ParseResult, default_scheme: str = '') → urllib.parse.ParseResult[source]

Reparse a parsed URL into a parsed URL with values fixed.

Parameters:
  • parsed (urllib.parse.ParseResult) – parsed URL to reparse
  • default_scheme (str, optional) – default "" - default scheme to use if URL does not contain a scheme
Returns:

reparsed result

Return type:

urllib.parse.ParseResult

unparse_base(parsed_result: urllib.parse.ParseResult) → str[source]

Unparse a parsed URL into just the scheme, hostname, and port parts.

Parameters:parsed (urllib.parse.ParseResult) – parsed URL to unparse
Returns:unparsed url
Return type:str
unparse_all(parsed_result: urllib.parse.ParseResult) → str[source]

Unparse a parsed URL with all the parts.

Parameters:parsed (urllib.parse.ParseResult) – parsed URL to unparse
Returns:unparsed url
Return type:str