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:
- axonius_api_client.http.is_cookies(value)[source]¶
Check if token is a valid cookies object.
- Parameters:
value (
typing.Any
) –- Return type:
- 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:
url (
typing.Union
[axonius_api_client.projects.url_parser.UrlParser
,str
]) –certpath (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) –certwarn (
bool
) –certverify (
bool
) –headers (
typing.Union
[dict
,requests.structures.CaseInsensitiveDict
,None
]) –cookies (
typing.Union
[dict
,requests.cookies.RequestsCookieJar
,None
]) –cert_client_both (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) –cert_client_cert (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) –cert_client_key (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) –connect_timeout (
typing.Union
[int
,float
,None
]) –response_timeout (
typing.Union
[int
,float
,None
]) –http_proxy (
typing.Optional
[str
]) –https_proxy (
typing.Optional
[str
]) –log_body_lines (
int
) –log_hide_headers (
typing.Union
[typing.TypeVar
(PatternLike
,typing.Pattern
,str
,bytes
),typing.Iterable
[typing.TypeVar
(PatternLike
,typing.Pattern
,str
,bytes
)],None
]) –log_hide_str (
typing.Optional
[str
]) –log_level (
typing.Union
[int
,str
]) –log_level_urllib (
typing.Union
[int
,str
]) –log_request_attrs (
typing.Union
[str
,typing.Iterable
[str
],None
]) –log_request_body (
bool
) –log_response_attrs (
typing.Union
[str
,typing.Iterable
[str
],None
]) –log_response_body (
bool
) –save_history (
bool
) –save_last (
bool
) –cf_token (
typing.Optional
[str
]) –cf_url (
typing.Optional
[str
]) –cf_path (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) –cf_run (
bool
) –cf_run_login (
bool
) –cf_run_access (
bool
) –cf_env (
bool
) –cf_echo (
bool
) –cf_echo_verbose (
bool
) –cf_error (
bool
) –cf_error_login (
bool
) –cf_error_access (
bool
) –cf_timeout_access (
typing.Optional
[int
]) –cf_timeout_login (
typing.Optional
[int
]) –max_retries (
typing.Optional
[int
]) –retry_backoff (
typing.Optional
[int
]) –
-
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:
url (
typing.Union
[axonius_api_client.projects.url_parser.UrlParser
,str
]) – URL, hostname, or IP address of Axonius instancecertpath (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) – token to CA bundle file to use when verifying certs offered byurl
certwarn (
bool
) – show insecure warning once or never show insecure warningcertverify (
bool
) – raise exception if cert is self-signed or only if cert is invalidheaders (
typing.Union
[dict
,requests.structures.CaseInsensitiveDict
,None
]) – headers to send with every requestcookies (
typing.Union
[dict
,requests.cookies.RequestsCookieJar
,None
]) – cookies to send with every requestlog_level (
typing.Union
[int
,str
]) – log level to use for this objectlog_body_lines (
int
) – max length of request and response bodies to loglog_hide_headers (
typing.Union
[typing.TypeVar
(PatternLike
,typing.Pattern
,str
,bytes
),typing.Iterable
[typing.TypeVar
(PatternLike
,typing.Pattern
,str
,bytes
)],None
]) – headers to hide when logginglog_hide_str (
typing.Optional
[str
]) – string to use to hide sensitive data in logslog_request_attrs (
typing.Union
[str
,typing.Iterable
[str
],None
]) – attributes of request to loglog_response_attrs (
typing.Union
[str
,typing.Iterable
[str
],None
]) – attributes of response to logsave_last (
bool
) – save last request and response tolast_request
andlast_response
save_history (
bool
) – save all requests and responses tohistory
connect_timeout (
typing.Union
[int
,float
,None
]) – seconds to wait for connections to open tourl
response_timeout (
typing.Union
[int
,float
,None
]) – seconds to wait for responses fromurl
log_request_body (
bool
) – log the request bodylog_response_body (
bool
) – log the response bodyhttp_proxy (
typing.Optional
[str
]) – proxy to use when making http requests tourl
https_proxy (
typing.Optional
[str
]) – proxy to use when making https requests tourl
log_level_urllib (
typing.Union
[int
,str
]) – log level to use for urllib3cert_client_key (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) – path to client private key filecert_client_both (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) – path to client private key and cert filecert_client_cert (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) – path to client cert filecf_url (
typing.Optional
[str
]) – URL to use in access token and access login commands, will fallback to url if not suppliedcf_token (
typing.Optional
[str
]) – access token supplied by user, will be checked for validity if not emptycf_env (
bool
) – if no token supplied, try to get token from OS env var CF_TOKENcf_run (
bool
) – if no token supplied or in OS env vars, try to get token from access token and access login commandscf_run_access (
bool
) – if run is True, try to get token from access token,cf_run_login (
bool
) – if run is True and no token returned from access token command, try to get token from access login commandcf_path (
typing.Optional
[typing.TypeVar
(PathLike
,pathlib.Path
,str
,bytes
)]) – path to cloudflared binary to run, can be full path or path in OS env var $PATHcf_timeout_access (
typing.Optional
[int
]) – timeout for access token command in secondscf_timeout_login (
typing.Optional
[int
]) – timeout for access login command in secondscf_error (
bool
) – raise error if an invalid token is found or no token can be foundcf_error_access (
bool
) – raise exc if access token command fails and login is Falsecf_error_login (
bool
) – raise exc if access login command failscf_echo (
bool
) – echo commands and results to stdoutcf_echo_verbose (
bool
) – echo checks to stdoutmax_retries (
typing.Optional
[int
]) – number of times to retry a failed connectionretry_backoff (
typing.Optional
[int
]) – number of seconds to wait between retries, will be multiplied against the current retry attempt**kwargs – no longer used, will throw a deprecation warning
- Raises:
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_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_LEVEL_URLLIB:
typing.Union
[int
,str
] = 'warning'¶ Log level to use for urllib3.
-
URLPARSED:
axonius_api_client.projects.url_parser.UrlParser
= None¶ Parsed URL object.
-
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_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.
-
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 suppliedtoken (
typing.Optional
[str
]) – access token supplied by user, will be checked for validity if not emptyenv (
bool
) – if no token supplied, try to get token from OS env var CF_TOKENrun (
bool
) – if no token supplied or in OS env vars, try to get token from access token and access login commandsrun_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 commandpath (
typing.Union
[str
,bytes
,pathlib.Path
]) – path to cloudflared binary to run, can be full path or path in OS env var $PATHtimeout_access (
typing.Optional
[int
]) – timeout for access token command in secondstimeout_login (
typing.Optional
[int
]) – timeout for access login command in secondserror (
bool
) – raise error if an invalid token is found or no token can be founderror_access (
bool
) – raise exc if access token command fails and login is Falseerror_login (
bool
) – raise exc if access login command failsecho (
bool
) – echo commands and results to stdoutecho_verbose (
bool
) – echo checks to stdout
- Return type:
- 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:
- 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
- set_session_headers()[source]¶
Configure
session
headers withHTTP_HEADERS
.
- set_session_cookies()[source]¶
Configure
session
cookies withHTTP_COOKIES
.
- 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:
path (
typing.Optional
[str
]) – path to append tourl
route (
typing.Optional
[str
]) – route to append tourl
method (
str
) – HTTP method to usedata (
typing.Optional
[str
]) – body to sendparams (
typing.Optional
[dict
]) – parameters to url encodeheaders (
typing.Optional
[dict
]) – headers to sendcookies (
typing.Optional
[dict
]) – cookies to sendjson (
typing.Optional
[dict
]) – obj to encode as jsonfiles (
typing.Optional
[tuple
]) – files to send**kwargs –
overrides for object attributes
connect_timeout: seconds to wait for connection to open for this request
response_timeout: seconds to wait for response for this request
proxies: proxies for this request
verify: verification of cert for this request
cert: client cert to offer for this request
- Returns:
- _do_log_request(request)[source]¶
Log attributes and/or body of a request.
- Parameters:
request (
requests.PreparedRequest
) – prepared request to log attrs/body of
- _do_log_response(response)[source]¶
Log attributes and/or body of a response.
- Parameters:
response (
requests.Response
) – response to log attrs/body of
- _get_log_attrs(attr_type)[source]¶
Get the log attributes for a specific type.
- Parameters:
attr_type (
str
) – ‘request’ or ‘response’- Return type:
- _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 stringsattr_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:
body (
typing.Any
) – content to logbody_type (
str
) – ‘request’ or ‘response’src (
typing.Optional
[typing.Any
]) – source of the body
- Return type: