Skip to content

Commit

Permalink
URL encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tommydeboer committed May 9, 2019
1 parent 0d8f4c5 commit d66ec99
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 42 deletions.
12 changes: 6 additions & 6 deletions mcmd/client/api.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 4 additions & 2 deletions mcmd/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""

import json
from urllib.parse import urljoin

import requests
from requests import HTTPError
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion mcmd/client/molgenis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@


@request
def get(url):
def get(url, params=None):
return requests.get(url,
params=params,
headers=_get_default_headers())


Expand Down
12 changes: 8 additions & 4 deletions mcmd/commands/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions mcmd/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion mcmd/commands/make.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])}
Expand Down
26 changes: 12 additions & 14 deletions mcmd/commands/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand All @@ -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']
11 changes: 9 additions & 2 deletions mcmd/utils/principals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
15 changes: 11 additions & 4 deletions mcmd/utils/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit d66ec99

Please sign in to comment.