|
10 | 10 | from .dss.project import DSSProject |
11 | 11 | from .dss.app import DSSApp |
12 | 12 | 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 |
14 | 14 | from .dss.meaning import DSSMeaning |
15 | 15 | from .dss.sqlquery import DSSSQLQuery |
16 | 16 | from .dss.discussion import DSSObjectDiscussions |
@@ -644,6 +644,73 @@ def create_global_api_key(self, label=None, description=None, admin=False): |
644 | 644 | key = resp.get('key', '') |
645 | 645 | return DSSGlobalApiKey(self, key) |
646 | 646 |
|
| 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 | + |
647 | 714 | ######################################################## |
648 | 715 | # Meanings |
649 | 716 | ######################################################## |
@@ -1001,18 +1068,6 @@ def get_ticket_from_browser_headers(self, headers_dict): |
1001 | 1068 | """ |
1002 | 1069 | return self._perform_json("POST", "/auth/ticket-from-browser-headers", body=headers_dict) |
1003 | 1070 |
|
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}) |
1016 | 1071 |
|
1017 | 1072 | ######################################################## |
1018 | 1073 | # Container execution |
|
0 commit comments