Skip to content

Commit

Permalink
Merge pull request #148 from andrewschenck/requests_session_injection
Browse files Browse the repository at this point in the history
Injection for requests.Session
  • Loading branch information
bvargasre authored Aug 14, 2024
2 parents 410d0d4 + 26b2b08 commit a83191f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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

0 comments on commit a83191f

Please sign in to comment.