diff --git a/examples/basic_fastapi_example.py b/examples/basic_fastapi_example.py deleted file mode 100644 index f2b17ae..0000000 --- a/examples/basic_fastapi_example.py +++ /dev/null @@ -1,66 +0,0 @@ -"""Basic example for using rdfproxy.SPARQLModelAdapter with FastAPI.""" - -from fastapi import FastAPI -from pydantic import BaseModel -from rdfproxy import Page, SPARQLModelAdapter - - -class SimpleModel(BaseModel): - x: int - y: int - - -class NestedModel(BaseModel): - a: str - b: SimpleModel - - -class ComplexModel(BaseModel): - p: str - q: NestedModel - - -query = """ -select ?x ?y ?a ?p -where { - values (?x ?y ?a ?p) { - (1 2 "a value 1" "p value 1") - (1 22 "a value 2" "p value 2") - (1 222 "a value 3" "p value 3") - (2 3 "a value 4" "p value 4") - (2 33 "a value 5" "p value 5") - (3 4 "a value 6" "p value 6") - (4 5 "a value 7" "p value 7") - } -} -""" - - -adapter = SPARQLModelAdapter( - target="https://query.wikidata.org/bigdata/namespace/wdq/sparql", - query=query, - model=ComplexModel, -) - - -app = FastAPI() - - -@app.get("/") -def base() -> list[ComplexModel]: - return adapter.query() - - -@app.get("/group/") -def group(group_by: str) -> dict[str, list[ComplexModel]]: - return adapter.query(group_by=group_by) - - -@app.get("/paginate/") -def paginate(page: int, size: int) -> Page[ComplexModel]: - return adapter.query(page=page, size=size) - - -@app.get("/grouped_paginate/") -def grouped_paginate(page: int, size: int, group_by: str) -> Page[ComplexModel]: - return adapter.query(page=page, size=size, group_by=group_by) diff --git a/examples/full_static_fastapi_example.py b/examples/full_static_fastapi_example.py new file mode 100644 index 0000000..4d3b8b4 --- /dev/null +++ b/examples/full_static_fastapi_example.py @@ -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' UNDEF) + (115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' 299260555) + (115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' 6762154387354230970008) + (115612815 'Geiger' 'University of Vienna' 'Unter der Drachenwand' 2277151717053313900002) + (1136992030 'Edelbauer' 'University of Vienna' 'Das flüssige Land' UNDEF) + (1136992030 'Edelbauer' 'University of Applied Arts Vienna' 'Das flüssige Land' 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) diff --git a/examples/releven_person_fastapi_example.py b/examples/releven_person_fastapi_example.py index ef8f8a3..80f61e6 100644 --- a/examples/releven_person_fastapi_example.py +++ b/examples/releven_person_fastapi_example.py @@ -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 @@ -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 ; @@ -25,7 +27,7 @@ } { - # appellation + # appellation ?person ^crm:P140_assigned_attribute_to ?e13_appellation . ?e13_appellation a star:E13_crm_P1 ; @@ -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 . } } + """ @@ -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) diff --git a/examples/wikidata_grouped_person_fastapi_example.py b/examples/wikidata_grouped_person_fastapi_example.py new file mode 100644 index 0000000..c6a8868 --- /dev/null +++ b/examples/wikidata_grouped_person_fastapi_example.py @@ -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) diff --git a/examples/wikidata_person_fastapi_example.py b/examples/wikidata_ungrouped_person_fastapi_example.py similarity index 58% rename from examples/wikidata_person_fastapi_example.py rename to examples/wikidata_ungrouped_person_fastapi_example.py index 0c2b7e4..8387f9d 100644 --- a/examples/wikidata_person_fastapi_example.py +++ b/examples/wikidata_ungrouped_person_fastapi_example.py @@ -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 @@ -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 { @@ -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, @@ -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)