Skip to content

Commit

Permalink
Minimal tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Aug 9, 2023
2 parents c61ca27 + e493bdd commit bd93347
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
7 changes: 7 additions & 0 deletions prez/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
populate_api_info,
add_prefixes_to_prefix_graph,
)
from prez.services.app_service import (
healthcheck_sparql_endpoints,
count_objects,
create_endpoints_graph,
populate_api_info,
add_prefixes_to_prefix_graph,
)
from prez.services.exception_catchers import (
catch_400,
catch_404,
Expand Down
4 changes: 2 additions & 2 deletions prez/renderers/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from prez.models.profiles_and_mediatypes import ProfilesMediatypesInfo
from prez.models.profiles_item import ProfileItem
from prez.reference_data.prez_ns import PREZ
from prez.sparql.methods import send_queries
from prez.sparql.methods import send_queries, rdf_queries_to_graph
from prez.services.curie_functions import get_curie_id_for_uri
from prez.sparql.objects_listings import (
generate_item_construct,
Expand Down Expand Up @@ -81,7 +81,7 @@ async def get_annotations_graph(profile, graph, cache):
if queries_for_uncached is None:
anots_from_triplestore = Graph()
else:
anots_from_triplestore = await queries_to_graph([queries_for_uncached])
anots_from_triplestore = await rdf_queries_to_graph(queries_for_uncached)

if len(anots_from_triplestore) > 1:
annotations_graph += anots_from_triplestore
Expand Down
32 changes: 21 additions & 11 deletions prez/routers/vocprez.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
return_from_graph,
)
from prez.services.curie_functions import get_curie_id_for_uri
from prez.sparql.methods import queries_to_graph
from prez.sparql.methods import rdf_queries_to_graph
from prez.sparql.objects_listings import (
generate_listing_construct,
generate_listing_count_construct,
Expand All @@ -41,8 +41,16 @@ async def vocprez_home():
return PlainTextResponse("VocPrez Home")


@router.get("/v/collection", summary="List Collections", name="https://prez.dev/endpoint/vocprez/collection-listing")
@router.get("/v/vocab", summary="List Vocabularies", name="https://prez.dev/endpoint/vocprez/vocabs-listing")
@router.get(
"/v/collection",
summary="List Collections",
name="https://prez.dev/endpoint/vocprez/collection-listing",
)
@router.get(
"/v/vocab",
summary="List Vocabularies",
name="https://prez.dev/endpoint/vocprez/vocabs-listing",
)
async def schemes_endpoint(
request: Request,
page: int = 1,
Expand Down Expand Up @@ -75,9 +83,9 @@ async def schemes_endpoint(


@router.get(
"/v/vocab/{scheme_curie}/all",
"/v/vocab/{scheme_curie}/all",
summary="Get Concept Scheme and all its concepts",
name="https://prez.dev/endpoint/vocprez/vocab"
name="https://prez.dev/endpoint/vocprez/vocab",
)
async def vocprez_scheme(request: Request, scheme_curie: str):
"""Get a SKOS Concept Scheme and all of its concepts.
Expand Down Expand Up @@ -149,7 +157,7 @@ async def concept_scheme_top_concepts_route(
iri, page, per_page
)

graph = await queries_to_graph([concept_scheme_top_concepts_query])
graph = await rdf_queries_to_graph(concept_scheme_top_concepts_query)
for concept in graph.objects(iri, SKOS.hasTopConcept):
if isinstance(concept, URIRef):
concept_curie = get_curie_id_for_uri(concept)
Expand Down Expand Up @@ -204,7 +212,7 @@ async def concept_narrowers_route(
iri = get_iri_route(concept_curie)
concept_narrowers_query = get_concept_narrowers_query(iri, page, per_page)

graph = await queries_to_graph([concept_narrowers_query])
graph = await rdf_queries_to_graph(concept_narrowers_query)
for concept in graph.objects(iri, SKOS.narrower):
if isinstance(concept, URIRef):
concept_curie = get_curie_id_for_uri(concept)
Expand Down Expand Up @@ -279,9 +287,11 @@ async def vocprez_collection(request: Request, collection_curie: str):
return await item_endpoint(request)


@router.get("/v/collection/{collection_curie}/{concept_curie}",
summary="Get Concept",
name="https://prez.dev/endpoint/vocprez/collection-concept")
@router.get(
"/v/collection/{collection_curie}/{concept_curie}",
summary="Get Concept",
name="https://prez.dev/endpoint/vocprez/collection-concept",
)
async def vocprez_collection_concept(
request: Request, collection_curie: str, concept_curie: str
):
Expand All @@ -295,7 +305,7 @@ async def item_endpoint(request: Request, vp_item: Optional[VocabItem] = None):
**request.path_params,
**request.query_params,
url_path=str(request.url.path),
endpoint_uri=request.scope["route"].name
endpoint_uri=request.scope["route"].name,
)
prof_and_mt_info = ProfilesMediatypesInfo(request=request, classes=vp_item.classes)
vp_item.selected_class = prof_and_mt_info.selected_class
Expand Down
5 changes: 2 additions & 3 deletions prez/services/app_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
)
from prez.config import settings
from prez.reference_data.prez_ns import PREZ, ALTREXT
from prez.sparql.methods import rdf_queries_to_graph
from prez.sparql.methods import query_to_graph, sparql_query_non_async
from prez.sparql.methods import rdf_queries_to_graph, sparql_query_non_async
from prez.sparql.objects_listings import startup_count_objects
from prez.services.curie_functions import get_curie_id_for_uri

Expand Down Expand Up @@ -109,7 +108,7 @@ async def add_prefixes_to_prefix_graph():
SELECT DISTINCT ?iri
WHERE {
?iri ?p ?o .
FILTER(isIRI(?iri))
FILTER(isIRI(?iri))
}
"""

Expand Down
2 changes: 1 addition & 1 deletion prez/sparql/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ async def send_query(query: str, mediatype="text/turtle"):
return await async_client.send(query_rq, stream=True)


async def rdf_queries_to_graph(query: str):
async def rdf_queries_to_graph(query: str) -> Graph:
"""
Sends a SPARQL query asynchronously and parses the response into an RDFLib Graph.
Args: query: str: A SPARQL query to be sent asynchronously.
Expand Down
10 changes: 5 additions & 5 deletions prez/sparql/objects_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def generate_listing_construct(
{{
SELECT ?top_level_item ?child_item
WHERE {{
{f'{uri_or_tl_item} a <{focus_item.general_class}> .{chr(10)}' if focus_item.top_level_listing else generate_outbound_predicates(uri_or_tl_item, outbound_children, outbound_parents)}\
{f'{uri_or_tl_item} a <{focus_item.base_class}> .{chr(10)}' if focus_item.top_level_listing else generate_focus_to_x_predicates(uri_or_tl_item, focus_to_child, focus_to_parent)}\
OPTIONAL {{
{f'{uri_or_tl_item} <{profile_item.label}> ?label .' if focus_item.top_level_listing else ""}\
}}
Expand All @@ -142,7 +142,7 @@ def generate_listing_construct(
"top_level_gen_class": focus_item.base_class
if focus_item.top_level_listing
else None,
# if this is a top level class, include it's general class here so we can create
# if this is a top level class, include it's base class here so we can create
# links to instances of the top level class,
}
return query, predicates_for_link_addition
Expand Down Expand Up @@ -520,7 +520,7 @@ def generate_listing_count_construct(
"""
Generates a SPARQL construct query to count either:
1. the members of a collection, if a URI is given, or;
2. the number of instances of a general class, given a general class.
2. the number of instances of a base class, given a base class.
"""
if not item.top_level_listing:
query = dedent(
Expand Down Expand Up @@ -772,7 +772,7 @@ def select_profile_mediatype(
defaults, and the availability of these in profiles.
NB: Most specific class refers to the rdfs:Class of an object which has the most specific rdfs:subClassOf links to
the general class delivered by that API endpoint. The general classes delivered by each API endpoint are:
the base class delivered by that API endpoint. The base classes delivered by each API endpoint are:
SpacePrez:
/s/datasets -> prez:DatasetList
Expand Down
4 changes: 2 additions & 2 deletions prez/sparql/resource.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from rdflib import Graph

from prez.sparql.methods import query_to_graph
from prez.sparql.methods import rdf_queries_to_graph


async def get_resource(iri: str) -> Graph:
query = f"""DESCRIBE <{iri}>"""
return await query_to_graph(query)
return await rdf_queries_to_graph(query)

0 comments on commit bd93347

Please sign in to comment.