From d8e158cb3afcc47150db04118a1ee401d9912780 Mon Sep 17 00:00:00 2001 From: Quentin Castel Date: Wed, 16 Feb 2022 10:29:28 +0100 Subject: [PATCH 1/2] Allow sending a bearer token --- dataikuapi/apinode_client.py | 7 ++++--- dataikuapi/base_client.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/dataikuapi/apinode_client.py b/dataikuapi/apinode_client.py index d4908b6e..469380a8 100644 --- a/dataikuapi/apinode_client.py +++ b/dataikuapi/apinode_client.py @@ -6,15 +6,16 @@ class APINodeClient(DSSBaseClient): This is an API client for the user-facing API of DSS API Node server (user facing API) """ - def __init__(self, uri, service_id, api_key=None): + def __init__(self, uri, service_id, api_key=None, bearer_token=None): """ Instantiate a new DSS API client on the given base URI with the given API key. :param str uri: Base URI of the DSS API node server (http://host:port/ or https://host:port/) :param str service_id: Identifier of the service to query - :param str api_key: Optional, API key for the service. Only required if the service has authentication + :param str api_key: Optional, API key for the service. Only required if the service has its authorization setup to API keys + :param str bearer_token: Optional, The bearer token. Only required if the service has its authorization setup to OAuth2/JWT """ - DSSBaseClient.__init__(self, "%s/%s" % (uri, "public/api/v1/%s" % service_id), api_key) + DSSBaseClient.__init__(self, "%s/%s" % (uri, "public/api/v1/%s" % service_id), api_key=api_key, bearer_token=bearer_token) def predict_record(self, endpoint_id, features, forced_generation=None, dispatch_key=None, context=None, with_explanations=None, explanation_method=None, n_explanations=None, n_explanations_mc_steps=None): diff --git a/dataikuapi/base_client.py b/dataikuapi/base_client.py index 9b15f053..9cdaa509 100644 --- a/dataikuapi/base_client.py +++ b/dataikuapi/base_client.py @@ -5,8 +5,9 @@ from .utils import DataikuException class DSSBaseClient(object): - def __init__(self, base_uri, api_key=None, internal_ticket=None): + def __init__(self, base_uri, api_key=None, bearer_token=None, internal_ticket=None): self.api_key = api_key + self.bearer_token = bearer_token self.base_uri = base_uri self._session = Session() @@ -18,12 +19,18 @@ def _perform_http(self, method, path, params=None, body=None, stream=False): if body: body = json.dumps(body) - auth = HTTPBasicAuth(self.api_key, "") + headers = None + auth = None + + if self.api_key: + auth = HTTPBasicAuth(self.api_key, "") + elif self.bearer_token: + headers = {"Authorization": "Bearer " + self.bearer_token} try: http_res = self._session.request( method, "%s/%s" % (self.base_uri, path), - params=params, data=body, + params=params, data=body, headers=headers, auth=auth, stream = stream) http_res.raise_for_status() return http_res From a1d8ffa7632f4dbad0fd79c45b7991f26349c671 Mon Sep 17 00:00:00 2001 From: Quentin Castel Date: Fri, 4 Mar 2022 14:14:42 +0100 Subject: [PATCH 2/2] move arg to last position --- dataikuapi/base_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dataikuapi/base_client.py b/dataikuapi/base_client.py index 9cdaa509..25153421 100644 --- a/dataikuapi/base_client.py +++ b/dataikuapi/base_client.py @@ -5,7 +5,7 @@ from .utils import DataikuException class DSSBaseClient(object): - def __init__(self, base_uri, api_key=None, bearer_token=None, internal_ticket=None): + def __init__(self, base_uri, api_key=None, internal_ticket=None, bearer_token=None): self.api_key = api_key self.bearer_token = bearer_token self.base_uri = base_uri