Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection for requests.Session #148

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dnacentersdk/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ def __init__(self, username=None,
base_url=None,
single_request_timeout=None,
wait_on_rate_limit=None,
session=None,
verify=None,
version=None,
debug=None,
Expand Down Expand Up @@ -479,6 +480,8 @@ def __init__(self, username=None,
(or DNA_CENTER_VERIFY_STRING) environment variable or
dnacentersdk.config.DEFAULT_VERIFY if the environment
variables are not set.
session(requests.Session): Optionally inject a `requests.Session`
instance to use for HTTP operations.
version(str): Controls which version of DNA_CENTER to use.
Defaults to the DNA_CENTER_VERSION environment variable or
dnacentersdk.config.DEFAULT_VERSION
Expand Down Expand Up @@ -584,6 +587,7 @@ def get_access_token():
base_url=base_url,
single_request_timeout=single_request_timeout,
wait_on_rate_limit=wait_on_rate_limit,
session=session,
verify=verify,
version=version,
debug=debug,
Expand Down
7 changes: 5 additions & 2 deletions dnacentersdk/restsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class RestSession(object):
def __init__(self, get_access_token, access_token, base_url,
single_request_timeout=DEFAULT_SINGLE_REQUEST_TIMEOUT,
wait_on_rate_limit=DEFAULT_WAIT_ON_RATE_LIMIT,
session=None,
verify=DEFAULT_VERIFY,
version=None,
debug=False):
Expand All @@ -151,6 +152,8 @@ def __init__(self, get_access_token, access_token, base_url,
HTTP REST API request.
wait_on_rate_limit(bool): Enable or disable automatic rate-limit
handling.
session(requests.Session): Optionally inject a `requests.Session`
object to be used for HTTP operations.
verify(bool,str): Controls whether we verify the server's
TLS certificate, or a string, in which case it must be a path
to a CA bundle to use.
Expand Down Expand Up @@ -194,8 +197,8 @@ def __init__(self, get_access_token, access_token, base_url,
if verify is False:
requests.packages.urllib3.disable_warnings()

# Initialize a new `requests` session
self._req_session = requests.session()
# Use the injected `requests` session, build a new one if not provided
self._req_session = session or requests.session()

# Update the headers of the `requests` session
self.update_headers({'X-Auth-Token': access_token,
Expand Down