4.7.5.3. HTTP Client

HTTP client.

axonius_api_client.http.is_headers(value)[source]

Check if token is a valid headers object.

Parameters:

value (typing.Any) –

Return type:

bool

axonius_api_client.http.is_cookies(value)[source]

Check if token is a valid cookies object.

Parameters:

value (typing.Any) –

Return type:

bool

class axonius_api_client.http.Http(url, certpath=None, certwarn=True, certverify=False, headers=None, cookies=None, cert_client_both=None, cert_client_cert=None, cert_client_key=None, connect_timeout=5, response_timeout=900, http_proxy=None, https_proxy=None, log_body_lines=100, log_hide_headers=('~cookie', '~auth', '~token', '~^cf_', '~secret', '~key', '~username', '~password'), log_hide_str='*********', log_level='debug', log_level_urllib='warning', log_request_attrs=None, log_request_body=False, log_response_attrs=None, log_response_body=False, save_history=False, save_last=True, cf_token=None, cf_url=None, cf_path='cloudflared', cf_run=False, cf_run_login=True, cf_run_access=True, cf_env=False, cf_echo=True, cf_echo_verbose=False, cf_error=False, cf_error_login=True, cf_error_access=True, cf_timeout_access=60, cf_timeout_login=180, max_retries=3, retry_backoff=5, **kwargs)[source]

Bases: object

HTTP client that wraps around requests.Session.

Parameters:
LOG_REQUEST_ATTRS: typing.Optional[typing.List[str]] = None

Attributes of request to log.

LOG_RESPONSE_ATTRS: typing.Optional[typing.List[str]] = None

Attributes of response to log.

URL_CERT: typing.Optional[axonius_api_client.projects.cert_human.stores.cert.Cert] = None

Cert object for URL.

URL_CERT_CHAIN: typing.Optional[typing.List[axonius_api_client.projects.cert_human.stores.cert.Cert]] = None

Cert chain for URL.

CLIENT: typing.Optional[object] = None

Client object that created this object.

__init__(url, certpath=None, certwarn=True, certverify=False, headers=None, cookies=None, cert_client_both=None, cert_client_cert=None, cert_client_key=None, connect_timeout=5, response_timeout=900, http_proxy=None, https_proxy=None, log_body_lines=100, log_hide_headers=('~cookie', '~auth', '~token', '~^cf_', '~secret', '~key', '~username', '~password'), log_hide_str='*********', log_level='debug', log_level_urllib='warning', log_request_attrs=None, log_request_body=False, log_response_attrs=None, log_response_body=False, save_history=False, save_last=True, cf_token=None, cf_url=None, cf_path='cloudflared', cf_run=False, cf_run_login=True, cf_run_access=True, cf_env=False, cf_echo=True, cf_echo_verbose=False, cf_error=False, cf_error_login=True, cf_error_access=True, cf_timeout_access=60, cf_timeout_login=180, max_retries=3, retry_backoff=5, **kwargs)[source]

HTTP client that wraps 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:
Raises:

HttpError

  • if either cert_client_cert or cert_client_key are supplied, and the other is not supplied - if any of cert_path, cert_client_cert, cert_client_key, or cert_client_both are supplied and the file does not exist

session: requests.sessions.Session = None

Requests session object.

LOG_LEVEL: typing.Union[int, str] = 'debug'

Log level to use for this object.

LOG: logging.Logger = None

Logger for this object.

HISTORY: typing.List[requests.models.Response] = None

History of all requests made.

LAST_REQUEST: typing.Optional[requests.models.PreparedRequest] = None

Last request made.

LAST_RESPONSE: typing.Optional[requests.models.Response] = None

Last response received.

LOG_BODY_LINES: int = 100

Max length of request and response bodies to log.

LOG_HIDE_HEADERS: typing.Union[typing.TypeVar(PatternLike, typing.Pattern, str, bytes), typing.Iterable[typing.TypeVar(PatternLike, typing.Pattern, str, bytes)], None] = ('~cookie', '~auth', '~token', '~^cf_', '~secret', '~key', '~username', '~password')

Headers to hide when logging.

LOG_HIDE_STR: str = '*********'

String to use to hide sensitive data in logs.

LOG_LEVEL_URLLIB: typing.Union[int, str] = 'warning'

Log level to use for urllib3.

LOG_REQUEST_BODY: bool = False

Log request body.

LOG_RESPONSE_BODY: bool = False

Log response body.

URLPARSED: axonius_api_client.projects.url_parser.UrlParser = None

Parsed URL object.

url: str = None

URL to use for requests.

HTTP_HEADERS: typing.Union[dict, requests.structures.CaseInsensitiveDict, None] = None

Headers to use for all requests.

HTTP_COOKIES: typing.Union[dict, requests.cookies.RequestsCookieJar, None] = None

Cookies to use for all requests.

CERT_PATH: typing.Optional[typing.TypeVar(PathLike, pathlib.Path, str, bytes)] = None

Path to CA cert file.

CERT_WARN: bool = True

Warn if CA cert is not found.

CERT_VERIFY: bool = False

Verify server cert.

CERT_CLIENT_BOTH: typing.Optional[typing.TypeVar(PathLike, pathlib.Path, str, bytes)] = None

Path to client cert and key file.

CERT_CLIENT_CERT: typing.Optional[typing.TypeVar(PathLike, pathlib.Path, str, bytes)] = None

Path to client cert file.

CERT_CLIENT_KEY: typing.Optional[typing.TypeVar(PathLike, pathlib.Path, str, bytes)] = None

Path to client key file.

CONNECT_TIMEOUT: typing.Union[int, float, None] = 5

Timeout for connecting to server.

RESPONSE_TIMEOUT: typing.Union[int, float, None] = 900

Timeout for receiving response from server.

HTTP_PROXY: typing.Optional[str] = None

Proxy to use for HTTP requests.

HTTPS_PROXY: typing.Optional[str] = None

Proxy to use for HTTPS requests.

SAVE_HISTORY: bool = False

Save history of requests.

SAVE_LAST: bool = True

Save last request and response.

MAX_RETRIES: typing.Optional[int] = 3

Number of times to retry a request if it fails.

RETRY_BACKOFF: typing.Optional[int] = 5

Number of seconds to wait between retries, will be multiplied against the current retry attempt.

set_cf_token(url=None, token=None, env=False, run=True, run_access=True, run_login=True, path='cloudflared', timeout_access=60, timeout_login=180, error=True, error_access=True, error_login=True, echo=True, echo_verbose=False)[source]

Set the Cloudflare access token to use for requests.

Notes

  • If token is supplied, it will be checked for validity

  • If token is not supplied, and env is True, try to get a token from the OS environment variables CF_TOKEN or AX_TOKEN

  • If token is not supplied or defined in OS environment variables and run is True, try to get a token from the command $path access token

  • If token is not supplied or defined in OS environment variables or returned from the command $path access token and login is True, try to get a token from the command $path access login

Parameters:
  • url (typing.Optional[str]) – URL to use in access token and access login commands, will default to self.url if not supplied

  • token (typing.Optional[str]) – access token supplied by user, will be checked for validity if not empty

  • env (bool) – if no token supplied, try to get token from OS env var CF_TOKEN

  • run (bool) – if no token supplied or in OS env vars, try to get token from access token and access login commands

  • run_access (bool) – if run is True, try to get token from access token,

  • run_login (bool) – if run is True and no token returned from access token command, try to get token from access login command

  • path (typing.Union[str, bytes, pathlib.Path]) – path to cloudflared binary to run, can be full path or path in OS env var $PATH

  • timeout_access (typing.Optional[int]) – timeout for access token command in seconds

  • timeout_login (typing.Optional[int]) – timeout for access login command in seconds

  • error (bool) – raise error if an invalid token is found or no token can be found

  • error_access (bool) – raise exc if access token command fails and login is False

  • error_login (bool) – raise exc if access login command fails

  • echo (bool) – echo commands and results to stdout

  • echo_verbose (bool) – echo checks to stdout

Return type:

typing.Optional[str]

Returns:

None or token, depending on error and error_access and error_login

safe_request(error=False, **kwargs)[source]

Make a request, but catch all exceptions and return None.

Parameters:

error (bool) –

Return type:

typing.Optional[requests.models.Response]

get_cert(error=False)[source]

Get the SSL certificate from url.

Parameters:

error (bool) –

Return type:

typing.Optional[axonius_api_client.projects.cert_human.stores.cert.Cert]

get_cert_chain(error=False)[source]

Get the SSL certificate chain from url.

Parameters:

error (bool) –

Return type:

typing.List[axonius_api_client.projects.cert_human.stores.cert.Cert]

parse_url(url)[source]

Pass.

Parameters:

url (typing.Union[str, axonius_api_client.projects.url_parser.UrlParser]) –

Return type:

axonius_api_client.projects.url_parser.UrlParser

new_session()[source]

Create a new session object.

set_session_headers()[source]

Configure session headers with HTTP_HEADERS.

set_session_cookies()[source]

Configure session cookies with HTTP_COOKIES.

set_session_proxies()[source]

Configure session proxies.

set_session_verify()[source]

Configure session verify with a cert bundle or a bool.

set_session_cert()[source]

Configure session with the client cert.

set_urllib_warnings()[source]

Filter urllib warnings to show once or ignore.

Notes

if self.CERT_WARN is True, show warning once if self.CERT_WARN is False, ignore warning

set_urllib_log()[source]

Set the urllib3 logging level to LOG_LEVEL_URLLIB.

__call__(path=None, route=None, method='get', data=None, params=None, headers=None, cookies=None, json=None, files=None, **kwargs)[source]

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

Parameters:
Returns:

requests.Response

__str__()[source]

Show object info.

Return type:

str

__repr__()[source]

Show object info.

Return type:

str

property user_agent: str

Value to use in User-Agent header.

_do_log_request(request)[source]

Log attributes and/or body of a request.

Parameters:

request (requests.PreparedRequest) – prepared request to log attrs/body of

_clean_headers(headers)[source]

Clean headers with sensitive information.

Parameters:

headers (dict) – headers to clean values of

Return type:

dict

_do_log_response(response)[source]

Log attributes and/or body of a response.

Parameters:

response (requests.Response) – response to log attrs/body of

property log_request_attrs: List[str]

Get the request attributes that should be logged.

property log_response_attrs: List[str]

Get the response attributes that should be logged.

_get_log_attrs(attr_type)[source]

Get the log attributes for a specific type.

Parameters:

attr_type (str) – ‘request’ or ‘response’

Return type:

typing.List[str]

_set_log_attrs(attr_map, attr_type, value)[source]

Set the log attributes for a specific type.

Parameters:
  • attr_map (dict) – map of attributes to format strings

  • attr_type (str) – ‘request’ or ‘response’

  • value (typing.Union[str, typing.List[str]]) – user supplied attrs to log

log_body(body, body_type, src=None)[source]

Get a string for logging a request or response body.

Parameters:
Return type:

str

_init()[source]

Pass.