Skip to content

Commit

Permalink
fix: check if graph and context are in the response
Browse files Browse the repository at this point in the history
  • Loading branch information
mosoriob committed Feb 14, 2022
1 parent 4f1acd4 commit 1ee4848
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions obasparql/query_manager.py
Expand Up @@ -207,7 +207,8 @@ def get_one_resource(self, request_args, query_type, **kwargs):
: return:
: rtype:
"""
owl_class_name, resource_type_uri, username = self.parse_request_arguments(**kwargs)
owl_class_name, resource_type_uri, username = self.parse_request_arguments(
**kwargs)
request_args[SPARQL_ID_TYPE_VARIABLE] = self.build_instance_uri(
kwargs[ID_KEY])
request_args[SPARQL_GRAPH_TYPE_VARIABLE] = self.generate_graph(
Expand All @@ -228,7 +229,8 @@ def get_all_resource(self, request_args, query_type, **kwargs):
: return:
: rtype:
"""
owl_class_name, resource_type_uri, username = self.parse_request_arguments(**kwargs)
owl_class_name, resource_type_uri, username = self.parse_request_arguments(
**kwargs)
request_args[SPARQL_QUERY_TYPE_VARIABLE] = resource_type_uri
request_args[SPARQL_GRAPH_TYPE_VARIABLE] = self.generate_graph(
username)
Expand Down Expand Up @@ -416,8 +418,7 @@ def obtain_query(self,
1) * request_args[PER_PAGE_KEY]
try:
result = self.dispatch_sparql_query(
raw_sparql_query=query_template,
request_args=request_args)
raw_sparql_query=query_template, request_args=request_args)
except Exception as e:
raise e

Expand Down Expand Up @@ -461,7 +462,17 @@ def frame_results(self,
if "@id" in response_dict and '@graph' not in response_dict:
return []

frame = {"@context": response_dict['@context'], "@type": owl_class_uri}
if '@context' in response_dict:
response_context = response_dict['@context']
else:
response_context = {}

if '@graph' in response_dict:
response_graph = response_dict['@graph']
else:
response_graph = {}

frame = {"@context": response_context, "@type": owl_class_uri}
# if owl_resource_iri is set, the user is requesting a specific resource and we must add it to the frame
if owl_resource_iri is not None:
frame['@id'] = owl_resource_iri
Expand All @@ -475,18 +486,19 @@ def frame_results(self,
frame["@context"][prop]["@container"] = "@set"

#TODO: I don't know why but the following line is needed to avoid the error:
if '@graph' in response_dict and 'id' in response_dict["@graph"]:
del response_dict["@graph"][ID_KEY]
if 'id' in response_graph:
del response_graph[ID_KEY]

# Context (returned by the endpoint) does not contain information about the classes
# so we must add it to the frame
frame["@context"] = {**self.class_context, **frame["@context"]}

if '@graph' in response_dict:
logger.debug(json.dumps(response_dict["@graph"], indent=4))

framed = jsonld.frame(response_dict, frame,
{"embed": (f"{EMBED_OPTION}")})
framed = jsonld.frame(
{
"@graph": response_graph,
"context": response_context
}, frame, {"embed": (f"{EMBED_OPTION}")})

if '@graph' in framed:
return framed['@graph']
else:
Expand Down Expand Up @@ -746,7 +758,7 @@ def dispatch_sparql_query(self, raw_sparql_query: str, request_args: str):
Returns:
dict: JSON-LD response
"""
"""

query_metadata = gquery.get_metadata(raw_sparql_query, self.endpoint)
rewritten_query = query_metadata['query']
Expand Down

0 comments on commit 1ee4848

Please sign in to comment.