Skip to content

Commit d27abd1

Browse files
authored
Merge pull request #163 from dataiku/perso/dss95-CH-58209-feature-keys-apis
[ch58209] Add personal API keys APIs
2 parents 7b65d8c + c17b704 commit d27abd1

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

dataikuapi/dss/admin.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,39 @@ def set_definition(self, definition):
798798
"PUT", "/admin/globalAPIKeys/%s" % self.key,
799799
body = definition)
800800

801+
class DSSPersonalApiKey(object):
802+
"""
803+
A personal API key on the DSS instance
804+
"""
805+
def __init__(self, client, key):
806+
self.client = client
807+
self.key = key
808+
809+
########################################################
810+
# Key deletion
811+
########################################################
812+
813+
def delete(self):
814+
"""
815+
Delete the api key
816+
"""
817+
return self.client._perform_empty(
818+
"DELETE", "/personal-api-keys/%s" % self.key)
819+
820+
########################################################
821+
# Key description
822+
########################################################
823+
824+
def get_definition(self):
825+
"""
826+
Get the API key's definition
827+
828+
Returns:
829+
the personal API key definition, as a JSON object
830+
"""
831+
return self.client._perform_json(
832+
"GET", "/personal-api-keys/%s" % (self.key))
833+
801834

802835
class DSSCluster(object):
803836
"""

dataikuapi/dssclient.py

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from .dss.project import DSSProject
1111
from .dss.app import DSSApp
1212
from .dss.plugin import DSSPlugin
13-
from .dss.admin import DSSUser, DSSOwnUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster
13+
from .dss.admin import DSSUser, DSSOwnUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster, DSSPersonalApiKey
1414
from .dss.meaning import DSSMeaning
1515
from .dss.sqlquery import DSSSQLQuery
1616
from .dss.discussion import DSSObjectDiscussions
@@ -629,6 +629,48 @@ def create_global_api_key(self, label=None, description=None, admin=False):
629629
key = resp.get('key', '')
630630
return DSSGlobalApiKey(self, key)
631631

632+
########################################################
633+
# Personal API Keys
634+
########################################################
635+
636+
def list_personal_api_keys(self):
637+
"""
638+
List all personal API keys:
639+
- not admin: only the keys belonging to the owner of the key
640+
- admin: All the personal keys
641+
642+
:returns: All personal API keys, as a list of dicts
643+
"""
644+
return self._perform_json(
645+
"GET", "/personal-api-keys/")
646+
647+
def get_personal_api_key(self, key):
648+
"""
649+
Get a specific API key from the user doing the request
650+
651+
:param str key: the secret key of the desired API key
652+
:returns: A :class:`dataikuapi.dss.admin.DSSPersonalApiKey` API key handle
653+
"""
654+
return DSSPersonalApiKey(self, key)
655+
656+
def create_personal_api_key(self, label=""):
657+
"""
658+
Create a Personal API key corresponding to the user doing the request, and return a handle to interact with it
659+
660+
:param: label string (optional): Label for the new API key
661+
:returns: A :class:`dataikuapi.dss.admin.DSSPersonalApiKey` API key handle
662+
"""
663+
resp = self._perform_json(
664+
"POST", "/personal-api-keys/", body={"label": label})
665+
if resp is None:
666+
raise Exception('API key creation returned no data')
667+
if resp.get('messages', {}).get('error', False):
668+
raise Exception('API key creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
669+
if not resp.get('id', False):
670+
raise Exception('API key creation returned no key')
671+
key = resp.get('key', '')
672+
return DSSPersonalApiKey(self, key)
673+
632674
########################################################
633675
# Meanings
634676
########################################################
@@ -961,19 +1003,6 @@ def get_ticket_from_browser_headers(self, headers_dict):
9611003
"""
9621004
return self._perform_json("POST", "/auth/ticket-from-browser-headers", body=headers_dict)
9631005

964-
def create_personal_api_key(self, label):
965-
"""
966-
Creates a personal API key corresponding to the user doing the request.
967-
This can be called if the DSSClient was initialized with an internal
968-
ticket or with a personal API key
969-
970-
:param: label string: Label for the new API key
971-
:returns: a dict of the new API key, containing at least "secret", i.e. the actual secret API key
972-
:rtype: dict
973-
"""
974-
return self._perform_json("POST", "/auth/personal-api-keys",
975-
params={"label": label})
976-
9771006
########################################################
9781007
# Container execution
9791008
########################################################

0 commit comments

Comments
 (0)