Skip to content

Commit

Permalink
Adding fragments as parameters when displaying vocab (#518)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
raoashish10 authored May 24, 2021
1 parent cbcce67 commit b9aebc4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
34 changes: 34 additions & 0 deletions hydrus/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from flask import Response, jsonify

import re

from hydrus.data import crud

from hydrus.utils import get_doc, get_api_name, get_hydrus_server_url, get_session
Expand Down Expand Up @@ -543,3 +545,35 @@ def parse_collection_members(object_: dict) -> dict:
return error_response(error)
object_['members'] = members
return object_


def get_fragments(resource: str) -> dict:
"""Gets a fragment of the main hydra vocabulary.
:param resource: Fragment specified in the request parameters
:type resource: str
:return: Object referred to by the fragment
:rtype: dict
"""
resource_dict = dict()
gen_doc = get_doc().generate()
if 'EntryPoint/' in resource:
match_string = r'\b(EntryPoint)\b'
for class_ in gen_doc['supportedClass']:
if re.search(match_string, class_['@id']):
res = class_
break
for properties in res["supportedProperty"]:
if resource in properties["property"]["@id"]:
res = properties
break
resource_dict[resource[11:]] = res
else:
resource_dict['@context'] = gen_doc['@context']
for class_ in gen_doc['supportedClass']:
match_string = r'\b({0})\b'.format(resource)
if re.search(match_string, class_['@id']):
res = class_
break
resource_dict["supportedClass"] = [res]
return resource_dict
11 changes: 8 additions & 3 deletions hydrus/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
checkClassOp,
checkEndpoint,
check_writeable_props,
get_context
get_context,
get_fragments
)
from hydrus.utils import get_doc, get_collections_and_parsed_classes
from hydrus.itemhelpers import (
Expand Down Expand Up @@ -64,8 +65,12 @@ class Vocab(Resource):
"""Vocabulary for Hydra."""

def get(self) -> Response:
"""Return the main hydra vocab."""
return set_response_headers(jsonify(get_doc().generate()))
"""Return the main hydra vocab or a fragment of the main hydra vocab."""
try:
resource = request.args.getlist('resource')[0]
return set_response_headers(jsonify(get_fragments(resource)))
except:
return set_response_headers(jsonify(get_doc().generate()))


class Entrypoint(Resource):
Expand Down
24 changes: 24 additions & 0 deletions hydrus/tests/functional/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ def test_Vocab(self, test_app_client, constants):
data=json.dumps(dict(foo='bar')))
assert response_post.status_code == 405

def test_fragments(self, test_app_client, constants):
"""Test the fragments in vocab."""
API_NAME = constants['API_NAME']
HYDRUS_SERVER_URL = constants['HYDRUS_SERVER_URL']
vocab_route = get_doc().doc_name
response_get = test_app_client.get(f'/{API_NAME}/{vocab_route}#')
response_get_data = json.loads(response_get.data.decode('utf-8'))

for class_ in response_get_data['supportedClass']:
resource_id = class_['@id']
regex = "#[a-zA-Z]+"
resource_name = re.search(regex, resource_id).group(0)
resource_name = resource_name[1:]

vocab_uri = f'{HYDRUS_SERVER_URL}{API_NAME}/{vocab_route}'
param_string = f'?resource={resource_name}'
response_fragment_get = test_app_client.get(f'{vocab_uri}{param_string}')
response_fragment_get_data = json.loads(response_fragment_get.data.decode('utf-8'))

assert response_fragment_get.status_code == 200
assert '@context' in response_fragment_get_data
assert response_fragment_get_data['supportedClass'][0]['@id'] == resource_id
assert len(response_fragment_get_data['supportedClass']) == 1

def test_Collections_GET(self, test_app_client, constants, doc, init_db_for_app_tests):
"""Test GET on collection endpoints."""
API_NAME = constants['API_NAME']
Expand Down

0 comments on commit b9aebc4

Please sign in to comment.