Skip to content

Commit

Permalink
1. Change dcterms:identifier with prez:identifier datatype to prez:id…
Browse files Browse the repository at this point in the history
…entifier datatype. 2. Expand accepted mediatypes on homepage
  • Loading branch information
recalcitrantsupplant committed Jul 11, 2024
1 parent 78fda68 commit 02553f8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
17 changes: 12 additions & 5 deletions prez/routers/management.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import logging
import pickle
from typing import Optional

from aiocache import caches
from fastapi import APIRouter
from fastapi import APIRouter, Query
from rdflib import BNode
from rdflib import Graph, URIRef, Literal
from rdflib.collection import Collection
Expand All @@ -12,23 +13,29 @@
from prez.cache import endpoints_graph_cache
from prez.config import settings
from prez.reference_data.prez_ns import PREZ
from prez.renderers.renderer import return_rdf
from prez.services.connegp_service import RDF_MEDIATYPES
from prez.renderers.renderer import return_rdf, return_from_graph
from prez.services.connegp_service import RDF_MEDIATYPES, MediaType

router = APIRouter(tags=["Management"])
log = logging.getLogger(__name__)


@router.get("/", summary="Home page", tags=["Prez"])
async def index():
async def index(
mediatype: Optional[MediaType] = Query(
MediaType("text/turtle"), alias="_mediatype", description="Requested mediatype"
),
):
"""Returns the following information about the API"""
if "anot+" in mediatype.value:
mediatype = MediaType(mediatype.value.replace("anot+", ""))
g = Graph()
g.bind("prez", "https://prez.dev/")
g.bind("ont", "https://prez.dev/ont/")
g.add((URIRef(settings.system_uri), PREZ.version, Literal(settings.prez_version)))
g += endpoints_graph_cache
g += await return_annotation_predicates()
return await return_rdf(g, "text/turtle", profile_headers={})
return await return_rdf(g, mediatype.value, profile_headers={})


@router.get("/purge-tbox-cache", summary="Reset Tbox Cache")
Expand Down
21 changes: 21 additions & 0 deletions prez/services/connegp_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
from enum import Enum
from textwrap import dedent

from pydantic import BaseModel
Expand Down Expand Up @@ -35,6 +36,26 @@
"text/plain": "nt", # text/plain is the old/deprecated mimetype for n-triples
}

class MediaType(str, Enum):
turtle = "text/turtle"
n3 = "text/n3"
nt = "application/n-triples"
json_ld = "application/ld+json"
xml = "application/rdf+xml"
anot_turtle = "text/anot+turtle"
anot_n3 = "text/anot+n3"
anot_nt = "application/anot+n-triples"
anot_json_ld = "application/anot+ld+json"
anot_xml = "application/anot+rdf+xml"
# Some common but incorrect mimetypes
application_rdf = "application/rdf"
application_rdf_xml = "application/rdf xml"
application_json = "application/json"
application_ld_json = "application/ld json"
text_ttl = "text/ttl"
text_ntriples = "text/ntriples"
text_plain = "text/plain"


class TokenError(Exception):
def __init__(self, *args):
Expand Down
26 changes: 13 additions & 13 deletions prez/services/link_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
from string import Template

from rdflib import Graph, Literal, URIRef, DCTERMS, BNode
from rdflib import Graph, Literal, URIRef
from rdflib.namespace import SH, RDF
from sparql_grammar_pydantic import (
IRI,
Expand Down Expand Up @@ -46,11 +46,11 @@ async def add_prez_links(graph: Graph, repo: Repo, endpoint_structure):


async def _link_generation(
uri: URIRef,
repo: Repo,
klasses,
graph: Graph,
endpoint_structure: str = settings.endpoint_structure,
uri: URIRef,
repo: Repo,
klasses,
graph: Graph,
endpoint_structure: str = settings.endpoint_structure,
):
"""
Generates links for the given URI if it is not already cached.
Expand All @@ -72,10 +72,10 @@ async def _link_generation(
ns
for ns in available_nodeshapes
if ns.uri
not in [
URIRef("http://example.org/ns#CQL"),
URIRef("http://example.org/ns#Search"),
]
not in [
URIRef("http://example.org/ns#CQL"),
URIRef("http://example.org/ns#Search"),
]
]
# run queries for available nodeshapes to get link components
for ns in available_nodeshapes:
Expand Down Expand Up @@ -133,18 +133,18 @@ async def get_nodeshapes_constraining_class(klasses, uri):


async def add_links_to_graph_and_cache(
curie_for_uri, graph, members_link, object_link, uri
curie_for_uri, graph, members_link, object_link, uri
):
"""
Adds links and identifiers to the given graph and cache.
"""
quads = []
quads.append((uri, PREZ["link"], Literal(object_link), uri))
quads.append(
(uri, DCTERMS.identifier, Literal(curie_for_uri, datatype=PREZ.identifier), uri)
(uri, PREZ.identifier, Literal(curie_for_uri), uri)
)
if (
members_link
members_link
): # TODO need to confirm the link value doesn't match the existing link value, as multiple endpoints can deliver the same class/have different links for the same URI
existing_members_link = list(
links_ids_graph_cache.quads((uri, PREZ["members"], None, uri))
Expand Down

0 comments on commit 02553f8

Please sign in to comment.