Client for making HTTP requests.
pytan3.http_client.
CERT_FILE
= '{http_client.parsed_url.hostname}.pem'¶str
– Certificate filename template for Certify.__call__()
.
pytan3.http_client.
RECORD_FILE
= '%Y_%m_%dT%H_%M_%S_%fZ.json'¶str
– Record filename template for HttpClient.response_hook_disk()
.
pytan3.http_client.
SHOW_CERT
= 'no'¶bool
– Default prompt value for “Show full certificate info” in
Certify.verify_hook()
.
Will use OS Environment variable “PYTAN_SHOW_CERT” if set.
pytan3.http_client.
SHOW_CHAIN
= 'no'¶bool
– Default prompt value for “Show certificate chain info” in
Certify.verify_hook()
.
Will use OS Environment variable “PYTAN_SHOW_CHAIN” if set.
pytan3.http_client.
CERT_VALID
= 'no'¶bool
– Default prompt value for “Consider certificate valid” in
Certify.verify_hook()
.
Will use OS Environment variable “PYTAN_CERT_VALID” if set.
pytan3.http_client.
HttpClient
(url, timeout=5, lvl='info')[source]¶Bases: object
Convenience class for requests package.
CLEAN_HOOKS
= {'/api/v\\d?/session': ['clean_hook_auth_rest'], '/auth': ['clean_hook_auth']}¶dict
– URL regex matches for cleaning bodies in cleaning hooks.
CLEAN_AUTH_KEYS
= ['username', 'password', 'domain', 'secondary']¶log
= None¶logging.Logger
– Log for this object.
last_request
= None¶requests.PreparedRequest
– Last request sent.
last_response
= None¶requests.Response
– Last response received.
history
= None¶list
– History of all responses received.
Used by HttpClient.response_hook_history()
.
record_path
= None¶pathlib.Path
– Path to store response recordings to.
session
= None¶requests.Session
– Requests session object.
url
¶Get the URL string from HttpClient.parsed_url
.
Returns: | str |
---|
__call__
(method='get', path='', data=None, timeout=5, params=None, headers=None, b64_headers=None, verify=True, **kwargs)[source]¶Create, prepare a request, and then send it.
Parameters: |
|
---|
Notes
If verify is True or None, verification is done using default/built in CA. OS env vars $REQUESTS_CA_BUNDLE, and $CURL_CA_BUNDLE are used if set and trust_env is True, and if trust_env is False session’s verify is used.
If verify is False, no verification is done. This overrides OS env and session’s verify for this request and no verification is done at all. Don’t do this.
If verify is a str, verification is done with PEM file at path. This overrides OS env and session’s verify for this request.
Caveat: If previous request made with session and close has not been called on session, the verify of the previous request will be used no matter what is supplied here.
Returns: | requests.Response |
---|
control_hook
(enable, hook)[source]¶Add or remove a hook from HttpClient.session
.
Parameters: | |
---|---|
Returns: | True/False if hook was actually removed/added. |
Return type: |
control_hook_record_disk
(enable=False, path=None, path_sub='records')[source]¶Add or remove session hook for recording responses to disk.
Parameters: |
|
---|
control_hook_record_history
(enable=False)[source]¶Add or remove session hook for recording in local class.
Parameters: | enable (bool , optional) – Add/remove Defaults to: False. |
---|
response_hook_log_debug
(response, *args, **kwargs)[source]¶Response hook to log info.
Parameters: |
|
---|---|
Returns: |
response_hook_clean
(response, *args, **kwargs)[source]¶Response hook to add cleaned versions of request/response body and headers.
Parameters: |
|
---|---|
Returns: |
clean_hook_auth_rest
(http_client, response)[source]¶Clean hook to hide auth keys in a response from REST session route.
Parameters: |
|
---|---|
Returns: |
clean_hook_auth
(http_client, response)[source]¶Clean hook to hide auth headers in a response from /auth API.
Parameters: |
|
---|---|
Returns: |
response_hook_history
(response, *args, **kwargs)[source]¶Response hook to add response to HttpClient.history
.
Parameters: |
|
---|
response_hook_disk
(response, *args, **kwargs)[source]¶Response hook to write response to HttpClient.record_path
.
Parameters: |
|
---|---|
Returns: |
serialize_response
(response)[source]¶Churn a response into JSON serializeable format.
Parameters: | response (requests.Response ) – Response object to churn. |
---|---|
Returns: | dict |