Skip to content

Commit

Permalink
Merge pull request #248 from OpenTreeOfLife/fine-grained
Browse files Browse the repository at this point in the history
Several fine-grained study access methods added back to the API
  • Loading branch information
mtholder authored Jul 20, 2023
2 parents baad1da + 2a6f200 commit c624fe3
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 26 deletions.
14 changes: 13 additions & 1 deletion phylesystem_api/phylesystem_api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ def includeme(config):
config.add_route('get_study_external_url', '/{api_version}/study/external_url/{study_id}')
config.add_route('get_study_tree', '/{api_version}/study/{study_id}/tree/{tree_id_with_extension}')
config.add_route('get_study_tree_label', '/{api_version}/study/{study_id}/tree/{tree_id_with_extension}/')

#
# STUDY/OTHER FINE-GRAINED ACCESS ROUTES
#
config.add_route('get_study_otu_by_id', '/{api_version}/study/{study_id}/otu/{otu_id}')
config.add_route('get_study_otu_by_id_slash', '/{api_version}/study/{study_id}/otu/{otu_id}/')
config.add_route('get_study_otu', '/{api_version}/study/{study_id}/otu')
config.add_route('get_study_otu_slash', '/{api_version}/study/{study_id}/otu/')
config.add_route('get_study_otus', '/{api_version}/study/{study_id}/otus')
config.add_route('get_study_otus_slash', '/{api_version}/study/{study_id}/otus/')
config.add_route('get_study_otumap', '/{api_version}/study/{study_id}/otumap')
config.add_route('get_study_otumap_slash', '/{api_version}/study/{study_id}/otumap/')
config.add_route('get_study_meta', '/{api_version}/study/{study_id}/meta')
config.add_route('get_study_meta_slash', '/{api_version}/study/{study_id}/meta/')
#
# TREE COLLECTION ROUTES
#
Expand Down
69 changes: 50 additions & 19 deletions phylesystem_api/phylesystem_api/views/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ def __make_valid_DOI(candidate):
else:
return None

def __validate_output_nexml2json(repo_nexml2json, kwargs, resource, type_ext, content_id=None):
def __validate_output_nexml2json(repo_nexml2json,
kwargs,
resource,
type_ext,
content_id=None):
# sometimes we need to tweak the incoming kwargs, so let's
# make a mutable MultiDict copy of Pyramid's immutable NestedMultiDict
kwargs = kwargs.copy()
Expand Down Expand Up @@ -171,7 +175,6 @@ def fetch_study(request):
api_version = request.matchdict['api_version']
study_id = request.matchdict['study_id']
_LOG.debug('study_id = {}'.format(study_id))
content_id = None
version_history = None
comment_html = None
# does this look like a filename? if so, grab its extension
Expand All @@ -189,8 +192,8 @@ def fetch_study(request):
out_schema = __validate_output_nexml2json(repo_nexml2json,
request.params, # combined GET and POST
'study',
request_extension,
content_id=content_id)
type_ext=request_extension,
content_id=None)
parent_sha = find_in_request(request, 'starting_commit_SHA', None)
# _LOG.debug('parent_sha = {}'.format(parent_sha))
# return the correct nexson of study_id, using the specified view
Expand Down Expand Up @@ -626,7 +629,7 @@ def get_study_file(request):
out_schema = __validate_output_nexml2json(repo_nexml2json,
request.params, # combined GET and POST
'file',
None,
type_ext=None,
content_id=study_id)
parent_sha = find_in_request(request, 'starting_commit_SHA', None)
try:
Expand Down Expand Up @@ -696,26 +699,55 @@ def get_study_external_url(request):
@view_config(route_name='get_study_tree', renderer=None)
@view_config(route_name='get_study_tree_label', renderer=None)
def get_study_tree(request):
api_utils.raise_on_CORS_preflight(request)

api_version = request.matchdict['api_version']
study_id = request.matchdict['study_id']
tree_id_with_extension = request.matchdict['tree_id_with_extension']
tree_name_parts = tree_id_with_extension.split('.')
tree_id = tree_name_parts[0]
if len(tree_name_parts) > 1:
file_ext = ".{}".format(tree_name_parts[1])
else:
file_ext = None

return _fine_grained_get(request, 'tree', content_id=tree_id, file_ext=file_ext)

@view_config(route_name='get_study_otus', renderer=None)
@view_config(route_name='get_study_otus_slash', renderer=None)
def get_study_otus(request):
return _fine_grained_get(request, 'otus')

@view_config(route_name='get_study_otu', renderer=None)
@view_config(route_name='get_study_otu_slash', renderer=None)
def get_study_otu(request):
return _fine_grained_get(request, 'otu')

@view_config(route_name='get_study_otu_by_id', renderer=None)
@view_config(route_name='get_study_otu_by_id_slash', renderer=None)
def get_study_otu(request):
otu_id = request.matchdict['otu_id']
return _fine_grained_get(request, 'otu', content_id=otu_id)

@view_config(route_name='get_study_otumap', renderer=None)
@view_config(route_name='get_study_otumap_slash', renderer=None)
def get_study_otumap(request):
return _fine_grained_get(request, 'otumap')

@view_config(route_name='get_study_meta', renderer=None)
@view_config(route_name='get_study_meta_slash', renderer=None)
def get_study_ometa(request):
return _fine_grained_get(request, 'meta')

def _fine_grained_get(request,
subresource,
content_id=None,
file_ext=None):
api_utils.raise_on_CORS_preflight(request)
study_id = request.matchdict['study_id']
result_data = None
phylesystem = api_utils.get_phylesystem(request)
repo_nexml2json = phylesystem.repo_nexml2json
out_schema = __validate_output_nexml2json(repo_nexml2json,
request.params, # combined GET and POST
'tree',
file_ext,
content_id=tree_id)
subresource,
type_ext=file_ext,
content_id=content_id)
parent_sha = find_in_request(request, 'starting_commit_SHA', None)
try:
r = phylesystem.return_study(study_id, commit_sha=parent_sha, return_WIP_map=True)
Expand All @@ -733,7 +765,7 @@ def get_study_tree(request):
except:
comment_html = ''
except:
# _LOG.exception('GET failed')
_LOG.exception('GET failed')
e = sys.exc_info()[0]
raise HTTPBadRequest(e)

Expand All @@ -749,10 +781,9 @@ def get_study_tree(request):
raise HTTPBadRequest( msg)

if result_data is None:
raise HTTPNotFound(body='subresource "tree/{t}" not found in study "{s}"'.format(t=tree_id,
s=study_id))
m = 'subresource "{b}/{t}" not found in study "{s}"'
m = m.format(b=subresource, t=content_id, s=study_id)
raise HTTPNotFound(body=m)
if out_schema.is_json():
return render_to_response('json', result_data, request)
else:
# _LOG.debug(result_data)
return render_to_response('string', result_data, request)
return render_to_response('string', result_data, request)
12 changes: 9 additions & 3 deletions ws-tests/opentreetesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,17 @@ def test_http_json_method(url,
'accept' : 'application/json',
}
if data:
resp = requests.request(verb,
translate(url),
if verb == 'GET':
resp = requests.get(translate(url),
headers=headers,
data=json.dumps(data),
params=data,
allow_redirects=True)
else:
resp = requests.request(verb,
translate(url),
headers=headers,
data=json.dumps(data),
allow_redirects=True)
else:
resp = requests.request(verb,
translate(url),
Expand Down
2 changes: 1 addition & 1 deletion ws-tests/test_api_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
from opentreetesting import test_http_json_method, config
DOMAIN = config('host', 'apihost')
SUBMIT_URI = '{d}/v3/studies'.format(d=DOMAIN)
if test_http_json_method(SUBMIT_URI, 'GET', expected_status=404):
if test_http_json_method(SUBMIT_URI, 'GET', expected_status=200):
sys.exit(0)
sys.exit(1)
4 changes: 2 additions & 2 deletions ws-tests/test_study_get_bad_outformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sys, os
from opentreetesting import test_http_json_method, config
DOMAIN = config('host', 'apihost')
SUBMIT_URI = DOMAIN + '/v1/study/10'
SUBMIT_URI = DOMAIN + '/v3/study/10/'
data = {'output_nexml2json':'x0.0.0'}
if test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=404):
if test_http_json_method(SUBMIT_URI, 'GET', data=data, expected_status=400):
sys.exit(0)
sys.exit(1)

0 comments on commit c624fe3

Please sign in to comment.