Skip to content

Commit

Permalink
Add support for pre-populated Sessions (#132)
Browse files Browse the repository at this point in the history
This PR allows the caller to inject a `requests.Session` object that the
Nomad client should use to make all requests, in case the client is used
in an environment where cookies/headers/etc. unrelated to Nomad
communication must be added to each request.

Tested: not yet
  • Loading branch information
minor-fixes authored Oct 10, 2022
1 parent b04f724 commit 690add1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions nomad/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def __init__(self,
version='v1',
verify=False,
cert=(os.getenv('NOMAD_CLIENT_CERT', None),
os.getenv('NOMAD_CLIENT_KEY', None))):
os.getenv('NOMAD_CLIENT_KEY', None)),
session=None):
""" Nomad api client
https://github.com/jrxFive/python-nomad/
Expand All @@ -36,6 +37,8 @@ def __init__(self,
be use to deploy or to ask info to nomad.
- token (defaults to None), Specifies to append ACL token to the headers to
make authentication on secured based nomad environemnts.
- session (defaults to None), allows for injecting a prepared requests.Session object that
all requests to Nomad should use.
returns: Nomad api client object
raises:
Expand All @@ -53,6 +56,7 @@ def __init__(self,
self.token = token
self.verify = verify
self.cert = cert if all(cert) else ()
self.session = session
self.__namespace = namespace

self.requester_settings = {
Expand All @@ -65,7 +69,8 @@ def __init__(self,
"version": self.version,
"verify": self.verify,
"cert": self.cert,
"region": self.region
"region": self.region,
"session": self.session,
}

self._acl = api.Acl(**self.requester_settings)
Expand Down
4 changes: 2 additions & 2 deletions nomad/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Requester(object):

ENDPOINT = ""

def __init__(self, address=None, uri='http://127.0.0.1', port=4646, namespace=None, token=None, timeout=5, version='v1', verify=False, cert=(), region=None, **kwargs):
def __init__(self, address=None, uri='http://127.0.0.1', port=4646, namespace=None, token=None, timeout=5, version='v1', verify=False, cert=(), region=None, session=None, **kwargs):
self.uri = uri
self.port = port
self.namespace = namespace
Expand All @@ -19,7 +19,7 @@ def __init__(self, address=None, uri='http://127.0.0.1', port=4646, namespace=No
self.verify = verify
self.cert = cert
self.address = address
self.session = requests.Session()
self.session = session or requests.Session()
self.region = region

def _endpoint_builder(self, *args):
Expand Down

0 comments on commit 690add1

Please sign in to comment.