Skip to content

Commit

Permalink
feat: introduce persondetail endpoint, model & template
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jan 10, 2025
1 parent 5254990 commit b67b559
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions pfp_api/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import binascii
from base64 import urlsafe_b64decode
from jinja2 import Environment, PackageLoader
from typing import Annotated
from fastapi import FastAPI, Query, Response, status
from fastapi import FastAPI, Query, Response, status, HTTPException

from rdfproxy import Page, SPARQLModelAdapter, QueryParameters

from pfp_api.models import Person
from pfp_api.models import Person, PersonDetail


app = FastAPI()
Expand All @@ -29,3 +31,18 @@ def persons(query_parameters: Annotated[PersonParams, Query()]) -> Page[Person]:
model=Person,
)
return adapter.query(query_parameters)


@app.get("/person/{person_id}")
def person(person_id: str):
template = env.get_template("person.j2")
try:
person_id = urlsafe_b64decode(person_id).decode()
except binascii.Error:
raise HTTPException(status_code=404, detail="Person not found")
adapter = SPARQLModelAdapter(
target="https://pfp-ts-backend.acdh-ch-dev.oeaw.ac.at/",
query=template.render({"person_id": person_id}),
model=PersonDetail
)
return adapter.query(QueryParameters())
7 changes: 7 additions & 0 deletions pfp_api/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pydantic import BaseModel, Field
from typing import List, Optional


class Entity(BaseModel): ...
Expand All @@ -12,3 +13,9 @@ class Person(Entity):
person: str = None
label: str = Field(..., example="Arthur Schnitzler")
graph: str = None


class PersonDetail(Entity):
person: str = None
label: str = Field(..., example="Arthur Schnitzler")
events: Optional[List[Event]] = None
6 changes: 6 additions & 0 deletions pfp_api/templates/person.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?subject
WHERE {
?subject proxy_for <{{ person_id }}> .
}

0 comments on commit b67b559

Please sign in to comment.