Skip to content

Commit 833827b

Browse files
committed
personal API key
1 parent 7ddb29c commit 833827b

File tree

2 files changed

+100
-13
lines changed

2 files changed

+100
-13
lines changed

dataikuapi/dss/admin.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,38 @@ def set_definition(self, definition):
10601060
body = definition)
10611061

10621062

1063+
class DSSPersonalApiKey(object):
1064+
"""
1065+
A personal API key on the DSS instance
1066+
"""
1067+
def __init__(self, client, id_):
1068+
"""Do not call that directly, use :meth:`dataikuapi.DSSClient.get_personal_api_key`"""
1069+
self.client = client
1070+
self.id_ = id_
1071+
1072+
########################################################
1073+
# Key description
1074+
########################################################
1075+
1076+
def get_definition(self):
1077+
"""
1078+
Get the API key's definition
1079+
:returns: the personal API key definition, as a JSON object
1080+
"""
1081+
return self.client._perform_json(
1082+
"GET", "/personal-api-keys/%s" % (self.id_))
1083+
1084+
########################################################
1085+
# Key deletion
1086+
########################################################
1087+
1088+
def delete(self):
1089+
"""
1090+
Delete the API key
1091+
"""
1092+
return self.client._perform_empty(
1093+
"DELETE", "/personal-api-keys/%s" % self.id_)
1094+
10631095
class DSSCluster(object):
10641096
"""
10651097
A handle to interact with a cluster on the DSS instance

dataikuapi/dssclient.py

Lines changed: 68 additions & 13 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, DSSGlobalUsageSummary, DSSInstanceVariables
13+
from .dss.admin import DSSUser, DSSOwnUser, DSSGroup, DSSConnection, DSSGeneralSettings, DSSCodeEnv, DSSGlobalApiKey, DSSCluster, DSSGlobalUsageSummary, DSSInstanceVariables, DSSPersonalApiKey
1414
from .dss.meaning import DSSMeaning
1515
from .dss.sqlquery import DSSSQLQuery
1616
from .dss.discussion import DSSObjectDiscussions
@@ -644,6 +644,73 @@ def create_global_api_key(self, label=None, description=None, admin=False):
644644
key = resp.get('key', '')
645645
return DSSGlobalApiKey(self, key)
646646

647+
########################################################
648+
# Personal API Keys
649+
########################################################
650+
651+
def list_personal_api_keys(self, as_type='dict'):
652+
"""
653+
List all personal API keys:
654+
- not admin: only the keys belonging to the current user
655+
- admin: all the personal keys
656+
:param str as_type: 'objects' or 'dict'
657+
:returns: All personal API keys
658+
"""
659+
660+
resp = self._perform_json(
661+
"GET", "/personal-api-keys/")
662+
if as_type == 'objects':
663+
return [DSSPersonalApiKey(self, item['id']) for item in resp]
664+
else:
665+
return resp
666+
667+
def get_personal_api_key(self, id_):
668+
"""
669+
Get a specific API key
670+
:param str id_: the id of the desired API key
671+
:returns: A :class:`dataikuapi.dss.admin.DSSPersonalApiKey` API key
672+
"""
673+
return DSSPersonalApiKey(self, id_)
674+
675+
def list_personal_api_keys(self, as_type='dict'):
676+
"""
677+
List all personal API keys:
678+
- not admin: only the keys belonging to the current user
679+
- admin: all the personal keys
680+
:param str as_type: 'objects' or 'dict'
681+
:returns: All personal API keys
682+
"""
683+
684+
resp = self._perform_json(
685+
"GET", "/personal-api-keys/all")
686+
if as_type == 'objects':
687+
return [DSSPersonalApiKey(self, item['id']) for item in resp]
688+
else:
689+
return resp
690+
691+
def create_personal_api_key(self, label="", description="", as_type='dict', user=""):
692+
"""
693+
Create a Personal API key
694+
:param str label: the label of the new API key
695+
:param str description: the description of the new API key
696+
:param str as_type: 'object' or 'dict'
697+
:param str user: the id of the user to impersonate (optional)
698+
:returns: The new personal API key
699+
"""
700+
resp = self._perform_json(
701+
"POST", "/personal-api-keys/", body={"user": user, "label": label, "description": description})
702+
if resp is None:
703+
raise Exception('API key creation returned no data')
704+
if resp.get('messages', {}).get('error', False):
705+
raise Exception('API key creation failed : %s' % (json.dumps(resp.get('messages', {}).get('messages', {}))))
706+
if not resp.get('id', False):
707+
raise Exception('API key creation returned no key')
708+
709+
if as_type == 'object':
710+
return DSSPersonalApiKey(self, resp["id"])
711+
else:
712+
return resp
713+
647714
########################################################
648715
# Meanings
649716
########################################################
@@ -1001,18 +1068,6 @@ def get_ticket_from_browser_headers(self, headers_dict):
10011068
"""
10021069
return self._perform_json("POST", "/auth/ticket-from-browser-headers", body=headers_dict)
10031070

1004-
def create_personal_api_key(self, label):
1005-
"""
1006-
Creates a personal API key corresponding to the user doing the request.
1007-
This can be called if the DSSClient was initialized with an internal
1008-
ticket or with a personal API key
1009-
1010-
:param: label string: Label for the new API key
1011-
:returns: a dict of the new API key, containing at least "secret", i.e. the actual secret API key
1012-
:rtype: dict
1013-
"""
1014-
return self._perform_json("POST", "/auth/personal-api-keys",
1015-
params={"label": label})
10161071

10171072
########################################################
10181073
# Container execution

0 commit comments

Comments
 (0)