diff --git a/mcmd/client/api.py b/mcmd/client/api.py index 08d3bc7d..b4f1c01b 100644 --- a/mcmd/client/api.py +++ b/mcmd/client/api.py @@ -1,23 +1,23 @@ -from urllib.parse import urljoin +from urllib.parse import urljoin, quote from mcmd.config import config def endpoint(func): def wrapper(*args, **kwargs): - return urljoin(config.get('host', 'selected'), func(*args, **kwargs)) + return urljoin(config.get('host', 'selected'), quote(func(*args, **kwargs))) return wrapper @endpoint -def rest1(): - return 'api/v1/' +def rest1(path: str): + return urljoin('api/v1/', path) @endpoint -def rest2(): - return 'api/v2/' +def rest2(path: str): + return urljoin('api/v2/', path) @endpoint diff --git a/mcmd/client/auth.py b/mcmd/client/auth.py index 7bfb2a0f..e1937258 100644 --- a/mcmd/client/auth.py +++ b/mcmd/client/auth.py @@ -4,7 +4,6 @@ """ import json -from urllib.parse import urljoin import requests from requests import HTTPError @@ -41,7 +40,10 @@ def check_token(): return try: - response = requests.get(urljoin(api.rest2(), 'sys_sec_Token?q=token=={}'.format(_token)), + response = requests.get(api.rest2('sys_sec_Token'), + params={ + 'q': 'token=={}'.format(_token) + }, headers={'Content-Type': 'application/json', 'x-molgenis-token': _token}) response.raise_for_status() except HTTPError as e: diff --git a/mcmd/client/molgenis_client.py b/mcmd/client/molgenis_client.py index a4ce74ef..b2efa39a 100644 --- a/mcmd/client/molgenis_client.py +++ b/mcmd/client/molgenis_client.py @@ -7,8 +7,9 @@ @request -def get(url): +def get(url, params=None): return requests.get(url, + params=params, headers=_get_default_headers()) diff --git a/mcmd/commands/add.py b/mcmd/commands/add.py index ea0cb805..1124ee01 100644 --- a/mcmd/commands/add.py +++ b/mcmd/commands/add.py @@ -132,7 +132,7 @@ def add_user(args): superuser = args.is_superuser ch_pwd = args.change_password - post(api.rest1() + 'sys_sec_User', + post(api.rest1('sys_sec_User'), data={'username': args.username, 'password_': password, 'changePassword': ch_pwd, @@ -158,14 +158,18 @@ def add_package(args): if args.parent: data['parent'] = args.parent - post(api.rest1() + 'sys_md_Package', data=data) + post(api.rest1('sys_md_Package'), data=data) @command def add_token(args): io.start('Adding token %s for user %s' % (highlight(args.token), highlight(args.user))) - user = get(api.rest2() + 'sys_sec_User?attrs=id&q=username==%s' % args.user) + user = get(api.rest2('sys_sec_User'), + params={ + 'attrs': 'id', + 'q': 'username=={}'.format(args.user) + }) if user.json()['total'] == 0: raise McmdError('Unknown user %s' % args.user) @@ -174,7 +178,7 @@ def add_token(args): data = {'User': user_id, 'token': args.token} - post(api.rest1() + 'sys_sec_Token', data=data) + post(api.rest1('sys_sec_Token'), data=data) @command diff --git a/mcmd/commands/delete.py b/mcmd/commands/delete.py index d98e10ae..8386a552 100644 --- a/mcmd/commands/delete.py +++ b/mcmd/commands/delete.py @@ -77,8 +77,7 @@ def _delete_entity_type_data(args): if args.force or (not args.force and io.confirm( 'Are you sure you want to delete all data in entity type {}?'.format(args.resource))): io.start('Deleting all data from entity {}'.format(highlight(args.resource))) - url = urljoin(api.rest1(), args.resource) - client.delete(url) + client.delete(api.rest1(args.resource)) def _delete_package(args): @@ -97,16 +96,22 @@ def _delete_package_contents(args): def _delete_entity_types_in_package(package_id): - response = client.get( - api.rest2() + ResourceType.ENTITY_TYPE.get_entity_id() + '?attrs=id&q=package==' + package_id) + response = client.get(api.rest2(ResourceType.ENTITY_TYPE.get_entity_id()), + params={ + 'attrs': 'id', + 'q': 'package==' + package_id + }) entity_ids = [entity_type['id'] for entity_type in response.json()['items']] if len(entity_ids) > 0: _delete_rows(ResourceType.ENTITY_TYPE.get_entity_id(), entity_ids) def _delete_packages_in_package(package_id): - response = client.get( - api.rest2() + ResourceType.PACKAGE.get_entity_id() + '?attrs=id&q=parent==' + package_id) + response = client.get(api.rest2(ResourceType.PACKAGE.get_entity_id()), + params={ + 'attrs': 'id', + 'q': 'parent==' + package_id + }) package_ids = [entity_type['id'] for entity_type in response.json()['items']] if len(package_ids) > 0: _delete_rows(ResourceType.PACKAGE.get_entity_id(), package_ids) @@ -120,8 +125,7 @@ def _delete_group(args): def _delete_rows(entity_type, rows): - url = '{}{}'.format(api.rest2(), entity_type) - client.delete_data(url, rows) + client.delete_data(api.rest2(entity_type), rows) def _get_resource_type(args): diff --git a/mcmd/commands/make.py b/mcmd/commands/make.py index acd863a2..d7cf46bc 100644 --- a/mcmd/commands/make.py +++ b/mcmd/commands/make.py @@ -42,7 +42,10 @@ def make(args): def _find_group(role): io.debug('Fetching groups') - groups = get(api.rest2() + 'sys_sec_Group?attrs=name') + groups = get(api.rest2('sys_sec_Group', + params={ + 'attrs': 'name' + })) role = lower_kebab(role) matches = {len(group['name']): group['name'] for group in groups.json()['items'] if role.startswith(group['name'])} diff --git a/mcmd/commands/set.py b/mcmd/commands/set.py index bc3b6c60..f3a72002 100644 --- a/mcmd/commands/set.py +++ b/mcmd/commands/set.py @@ -65,15 +65,17 @@ def set_(args): entity = _get_settings_entity(args.type) io.start( 'Updating {} of {} settings to {}'.format(highlight(args.setting), highlight(args.type), highlight(args.value))) - row = _get_row_id(entity) - url = '{}{}/{}/{}'.format(api.rest1(), entity, row, args.setting) + row = _get_first_row_id(entity) + url = api.rest1('{}/{}/{}'.format(entity, row, args.setting)) put(url, json.dumps(args.value)) def _get_settings(): - entity = 'sys_md_EntityType' - query = 'q=extends==sys_set_settings&attrs=~id' - molgenis_settings = _quick_get(entity, query) + molgenis_settings = get(api.rest2('sys_md_EntityType'), + params={ + 'q': 'extends==sys_set_settings', + 'attrs': '~id' + }).json()['items'] return [setting['id'] for setting in molgenis_settings] @@ -90,13 +92,9 @@ def _get_settings_entity(setting): raise McmdError('Setting [{}] is not a valid settings entity'.format(setting)) -def _quick_get(entity, q): - rest = api.rest2() - url = '{}{}?{}'.format(rest, entity, q) - return get(url).json()['items'] - - -def _get_row_id(entity): - query = 'attrs=~id' - settings = _quick_get(entity, query) +def _get_first_row_id(entity): + settings = get(api.rest2(entity), + params={ + 'attrs': '~id' + }).json()['items'] return settings[0]['id'] diff --git a/mcmd/utils/principals.py b/mcmd/utils/principals.py index ab3f6d6b..12a01657 100644 --- a/mcmd/utils/principals.py +++ b/mcmd/utils/principals.py @@ -30,13 +30,20 @@ def principal_exists(principal_name, principal_type): def user_exists(username): log.debug('Checking if user %s exists' % username) - response = get(api.rest2() + 'sys_sec_User?q=username==' + username) + response = get(api.rest2('sys_sec_User'), + params={ + 'q': 'username==' + username + }) + return int(response.json()['total']) > 0 def role_exists(rolename): log.debug('Checking if role %s exists' % rolename) - response = get(api.rest2() + 'sys_sec_Role?q=name==' + rolename.upper()) + response = get(api.rest2('sys_sec_Role'), + params={ + 'q': 'name==' + rolename.upper() + }) return int(response.json()['total']) > 0 diff --git a/mcmd/utils/resources.py b/mcmd/utils/resources.py index 60d730de..16769959 100644 --- a/mcmd/utils/resources.py +++ b/mcmd/utils/resources.py @@ -52,15 +52,22 @@ def detect_resource_type(resource_id, types: List[ResourceType]): def resource_exists(resource_id, resource_type): log.debug('Checking if %s %s exists' % (resource_type.get_label(), resource_id)) - query = '?q={}=={}'.format(resource_type.get_identifying_attribute(), resource_id) - response = get(api.rest2() + resource_type.get_entity_id() + query) + query = '{}=={}'.format(resource_type.get_identifying_attribute(), resource_id) + response = get(api.rest2(resource_type.get_entity_id()), + params={ + 'q': query + }) return int(response.json()['total']) > 0 def one_resource_exists(resources, resource_type): log.debug('Checking if one of [{}] exists in [{}]'.format(','.join(resources), resource_type.get_label())) - query = '?q={}=in=({})'.format(resource_type.get_identifying_attribute(), ','.join(resources)) - response = get(api.rest2() + resource_type.get_entity_id() + query) + query = '{}=in=({})'.format(resource_type.get_identifying_attribute(), ','.join(resources)) + response = get(api.rest2(resource_type.get_entity_id()), + params={ + 'q': query + }) + return int(response.json()['total']) > 0