Skip to content

Commit

Permalink
docs(examples): update examples
Browse files Browse the repository at this point in the history
Closes #145.
  • Loading branch information
lu-pl committed Nov 25, 2024
1 parent 8b96a0b commit 7e0a8e4
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 112 deletions.
66 changes: 0 additions & 66 deletions examples/basic_fastapi_example.py

This file was deleted.

52 changes: 52 additions & 0 deletions examples/full_static_fastapi_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""RDFProxy-based FastAPI route example: Static query targeting Wikidata with grouped and nested models."""

from typing import Annotated

from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict
from rdfproxy import Page, SPARQLBinding, SPARQLModelAdapter


query = """
select *
where {
values (?gnd ?authorName ?educatedAt ?workName ?work ?viaf) {
(119359464 'Schindel' UNDEF 'Gebürtig' <http://www.wikidata.org/entity/Q1497409> UNDEF)
(115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' <http://www.wikidata.org/entity/Q15805238> 299260555)
(115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' <http://www.wikidata.org/entity/Q15805238> 6762154387354230970008)
(115612815 'Geiger' 'University of Vienna' 'Unter der Drachenwand' <http://www.wikidata.org/entity/Q58038819> 2277151717053313900002)
(1136992030 'Edelbauer' 'University of Vienna' 'Das flüssige Land' <http://www.wikidata.org/entity/Q100266054> UNDEF)
(1136992030 'Edelbauer' 'University of Applied Arts Vienna' 'Das flüssige Land' <http://www.wikidata.org/entity/Q100266054> UNDEF)
}
}
"""


class Work(BaseModel):
model_config = ConfigDict(group_by="workName")

name: Annotated[str, SPARQLBinding("workName")]
viafs: Annotated[list[str], SPARQLBinding("viaf")]


class Author(BaseModel):
model_config = ConfigDict(group_by="authorName")

gnd: str
surname: Annotated[str, SPARQLBinding("authorName")]
works: list[Work]
education: Annotated[list[str], SPARQLBinding("educatedAt")]


adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
model=Author,
)

app = FastAPI()


@app.get("/")
def base_route(page: int = 1, size: int = 100) -> Page[Author]:
return adapter.query(page=page, size=size)
28 changes: 8 additions & 20 deletions examples/releven_person_fastapi_example.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""RDFProxy-based FastAPI route example: CRM query targeting Releven GraphDB with simple ungrouped Person model."""

from fastapi import FastAPI
from pydantic import BaseModel
from rdfproxy import Page, SPARQLModelAdapter
Expand All @@ -13,8 +15,8 @@
?person a crm:E21_Person .
{
# id
?person ^crm:P140_assigned_attribute_to ?e13_id .
# id
?person ^crm:P140_assigned_attribute_to ?e13_id .
?e13_id a crm:E15_Identifier_Assignment ;
crm:P37_assigned [
a crm:E42_Identifier ;
Expand All @@ -25,7 +27,7 @@
}
{
# appellation
# appellation
?person ^crm:P140_assigned_attribute_to ?e13_appellation .
?e13_appellation a star:E13_crm_P1 ;
Expand All @@ -37,12 +39,13 @@
{
# note
?person ^crm:P140_assigned_attribute_to ?e13_note .
?person ^crm:P140_assigned_attribute_to ?e13_note .
?e13_note a star:E13_crm_P3 ;
crm:P141_assigned ?note .
}
}
"""


Expand All @@ -63,20 +66,5 @@ class R11PersonModel(BaseModel):


@app.get("/")
def base() -> list[R11PersonModel]:
return adapter.query()


@app.get("/group/")
def group(group_by: str) -> dict[str, list[R11PersonModel]]:
return adapter.query(group_by=group_by)


@app.get("/paginate/")
def paginate(page: int, size: int) -> Page[R11PersonModel]:
def base(page=1, size=100) -> Page[R11PersonModel]:
return adapter.query(page=page, size=size)


@app.get("/grouped_paginate/")
def grouped_paginate(page: int, size: int, group_by: str) -> Page[R11PersonModel]:
return adapter.query(page=page, size=size, group_by=group_by)
44 changes: 44 additions & 0 deletions examples/wikidata_grouped_person_fastapi_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""RDFProxy-based FastAPI route example: Wikidata query with simple grouped model."""

from typing import Annotated

from fastapi import FastAPI
from pydantic import BaseModel, ConfigDict
from rdfproxy import Page, SPARQLBinding, SPARQLModelAdapter


query = """
SELECT ?name ?title
WHERE {
wd:Q44336 wdt:P1559 ?name .
wd:Q44336 wdt:P800 ?work .
?work wdt:P1476 ?title .
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""


class Work(BaseModel):
name: Annotated[str, SPARQLBinding("title")]


class Person(BaseModel):
model_config = ConfigDict(group_by="name")

name: str
work: list[Work]


adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
model=Person,
)


app = FastAPI()


@app.get("/")
def base(page: int = 1, size: int = 100) -> Page[Person]:
return adapter.query(page=page, size=size)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""rdfproxy.SPARQLModelAdapter + FastAPI example targeting Wikidata."""
"""RDFProxy-based FastAPI route example: Wikidata query with simple ungrouped model."""

from typing import Annotated

Expand All @@ -7,15 +7,6 @@
from rdfproxy import Page, SPARQLBinding, SPARQLModelAdapter


class Work(BaseModel):
name: Annotated[str, SPARQLBinding("title")]


class Person(BaseModel):
name: str
work: Work


query = """
SELECT ?name ?title
WHERE {
Expand All @@ -26,6 +17,16 @@ class Person(BaseModel):
}
"""


class Work(BaseModel):
name: Annotated[str, SPARQLBinding("title")]


class Person(BaseModel):
name: str
work: Work


adapter = SPARQLModelAdapter(
target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
query=query,
Expand All @@ -37,20 +38,5 @@ class Person(BaseModel):


@app.get("/")
def base() -> list[Person]:
return adapter.query()


@app.get("/group/")
def group(group_by: str) -> dict[str, list[Person]]:
return adapter.query(group_by=group_by)


@app.get("/paginate/")
def paginate(page: int, size: int) -> Page[Person]:
def base(page: int = 1, size: int = 100) -> Page[Person]:
return adapter.query(page=page, size=size)


@app.get("/grouped_paginate/")
def grouped_paginate(page: int, size: int, group_by: str) -> Page[Person]:
return adapter.query(page=page, size=size, group_by=group_by)

0 comments on commit 7e0a8e4

Please sign in to comment.