Skip to content

Commit b9aebc4

Browse files
authored
Adding fragments as parameters when displaying vocab (#518)
* Adding fragments as parameters when displaying vocab * Fixes according to checks * Missing fixes according to checks * Fixes for assertion errors * Changed parameter name to resource * Added tests for feature and used re to match string * Added support for entrypoints
1 parent cbcce67 commit b9aebc4

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

hydrus/helpers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from flask import Response, jsonify
44

5+
import re
6+
57
from hydrus.data import crud
68

79
from hydrus.utils import get_doc, get_api_name, get_hydrus_server_url, get_session
@@ -543,3 +545,35 @@ def parse_collection_members(object_: dict) -> dict:
543545
return error_response(error)
544546
object_['members'] = members
545547
return object_
548+
549+
550+
def get_fragments(resource: str) -> dict:
551+
"""Gets a fragment of the main hydra vocabulary.
552+
553+
:param resource: Fragment specified in the request parameters
554+
:type resource: str
555+
:return: Object referred to by the fragment
556+
:rtype: dict
557+
"""
558+
resource_dict = dict()
559+
gen_doc = get_doc().generate()
560+
if 'EntryPoint/' in resource:
561+
match_string = r'\b(EntryPoint)\b'
562+
for class_ in gen_doc['supportedClass']:
563+
if re.search(match_string, class_['@id']):
564+
res = class_
565+
break
566+
for properties in res["supportedProperty"]:
567+
if resource in properties["property"]["@id"]:
568+
res = properties
569+
break
570+
resource_dict[resource[11:]] = res
571+
else:
572+
resource_dict['@context'] = gen_doc['@context']
573+
for class_ in gen_doc['supportedClass']:
574+
match_string = r'\b({0})\b'.format(resource)
575+
if re.search(match_string, class_['@id']):
576+
res = class_
577+
break
578+
resource_dict["supportedClass"] = [res]
579+
return resource_dict

hydrus/resources.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
checkClassOp,
3232
checkEndpoint,
3333
check_writeable_props,
34-
get_context
34+
get_context,
35+
get_fragments
3536
)
3637
from hydrus.utils import get_doc, get_collections_and_parsed_classes
3738
from hydrus.itemhelpers import (
@@ -64,8 +65,12 @@ class Vocab(Resource):
6465
"""Vocabulary for Hydra."""
6566

6667
def get(self) -> Response:
67-
"""Return the main hydra vocab."""
68-
return set_response_headers(jsonify(get_doc().generate()))
68+
"""Return the main hydra vocab or a fragment of the main hydra vocab."""
69+
try:
70+
resource = request.args.getlist('resource')[0]
71+
return set_response_headers(jsonify(get_fragments(resource)))
72+
except:
73+
return set_response_headers(jsonify(get_doc().generate()))
6974

7075

7176
class Entrypoint(Resource):

hydrus/tests/functional/test_app.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,30 @@ def test_Vocab(self, test_app_client, constants):
7070
data=json.dumps(dict(foo='bar')))
7171
assert response_post.status_code == 405
7272

73+
def test_fragments(self, test_app_client, constants):
74+
"""Test the fragments in vocab."""
75+
API_NAME = constants['API_NAME']
76+
HYDRUS_SERVER_URL = constants['HYDRUS_SERVER_URL']
77+
vocab_route = get_doc().doc_name
78+
response_get = test_app_client.get(f'/{API_NAME}/{vocab_route}#')
79+
response_get_data = json.loads(response_get.data.decode('utf-8'))
80+
81+
for class_ in response_get_data['supportedClass']:
82+
resource_id = class_['@id']
83+
regex = "#[a-zA-Z]+"
84+
resource_name = re.search(regex, resource_id).group(0)
85+
resource_name = resource_name[1:]
86+
87+
vocab_uri = f'{HYDRUS_SERVER_URL}{API_NAME}/{vocab_route}'
88+
param_string = f'?resource={resource_name}'
89+
response_fragment_get = test_app_client.get(f'{vocab_uri}{param_string}')
90+
response_fragment_get_data = json.loads(response_fragment_get.data.decode('utf-8'))
91+
92+
assert response_fragment_get.status_code == 200
93+
assert '@context' in response_fragment_get_data
94+
assert response_fragment_get_data['supportedClass'][0]['@id'] == resource_id
95+
assert len(response_fragment_get_data['supportedClass']) == 1
96+
7397
def test_Collections_GET(self, test_app_client, constants, doc, init_db_for_app_tests):
7498
"""Test GET on collection endpoints."""
7599
API_NAME = constants['API_NAME']

0 commit comments

Comments
 (0)