From 4149dd65913be804a1dcc3f485a4c6f13bd74362 Mon Sep 17 00:00:00 2001 From: Tom Morrell Date: Fri, 6 Sep 2024 16:10:19 -0700 Subject: [PATCH] cris: make dimcli work with cris api --- dimcli/core/api.py | 12 +++++++++--- dimcli/core/auth.py | 20 +++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/dimcli/core/api.py b/dimcli/core/api.py index aac0ad0..c1889a7 100644 --- a/dimcli/core/api.py +++ b/dimcli/core/api.py @@ -88,7 +88,10 @@ def __init__(self, show_results=False, verbose=True, auth_session=False): if self._CONNECTION.token: # if already logged in, reuse connection self._url = self._CONNECTION.url - self._headers = {'Authorization': "JWT " + self._CONNECTION.token} + if 'cris-api' in self._url: + self._headers = {'Authorization': "Bearer " + self._CONNECTION.token} + else: + self._headers = {'Authorization': "JWT " + self._CONNECTION.token} self.verify_ssl = self._CONNECTION.verify_ssl else: self._print_please_login() @@ -105,7 +108,10 @@ def _refresh_login(self): if self._CONNECTION: self._CONNECTION.refresh_login() self._url = self._CONNECTION.url - self._headers = {'Authorization': "JWT " + self._CONNECTION.token} + if 'cris-api' in self._url: + self._headers = {'Authorization': "Bearer " + self._CONNECTION.token} + else: + self._headers = {'Authorization': "JWT " + self._CONNECTION.token} self.verify_ssl = self._CONNECTION.verify_ssl else: printDebug("Warning: please login first.") @@ -1011,4 +1017,4 @@ def __repr__(self): # 2019-12-17: for backward compatibility # remove once all notebooks code has been updated Result = DslDataset -Dataset = DslDataset \ No newline at end of file +Dataset = DslDataset diff --git a/dimcli/core/auth.py b/dimcli/core/auth.py index 48c3af0..564a283 100644 --- a/dimcli/core/auth.py +++ b/dimcli/core/auth.py @@ -152,11 +152,17 @@ def login(self, login_data = {'username': username, 'password': password, 'key': key} - # POST AUTH REQUEST - response = requests.post(URL_AUTH, json=login_data, verify=verify_ssl) - response.raise_for_status() + if 'cris-api' in URL_QUERY: + login_data = {'api_key': key} + response = requests.get(URL_AUTH, params=login_data, verify=verify_ssl) + response.raise_for_status() + token = response.text + else: + # POST AUTH REQUEST + response = requests.post(URL_AUTH, json=login_data, verify=verify_ssl) + response.raise_for_status() - token = response.json()['token'] + token = response.json()['token'] self.instance = instance self.url = URL_QUERY @@ -194,7 +200,11 @@ def _get_endpoint_urls(self, user_url): """ url_auth, url_query = None, None - if "/api/" in user_url: + if "cris-api" in user_url: + domain = user_url.split("/api/")[0] + url_auth = domain + "/token" + url_query = domain + "/api/query" + elif "/api/" in user_url: # savy user passing the full QUERY URL domain = user_url.split("/api/")[0] url_auth = domain + "/api/auth.json"