Package: rdf-fastapi-utils
RDF-FastAPI-UtilsThis is a small utils library for providing access to data in a triplestore via a FastAPI Rest endpoint.
Currently it contains only those classes used to convert a Json resulting from a SPARQL query into a given pydantic model.Currently it contains two classes:models.FieldConfigurationRDFto add additional information for processing RDF data to the fields in a pydantic modelandmodels.RDFUtilsModelBaseClassas a pydantic base class to inherit from for adding SPARQL Json to a pydantic model.Minimal examplethis is taken from the testsmodels.py:frompydanticimportFieldfromrdf_fastapi_utils.modelsimportFieldConfigurationRDF,RDFUtilsModelBaseClassclassTCPaginatedResponse(RDFUtilsModelBaseClass):count:int=Field(...,rdfconfig=FieldConfigurationRDF(path="count"))results:list[Union["TCPersonFull","TCPlaceFull"]]=Field(...,rdfconfig=FieldConfigurationRDF(path="results",serialization_class_callback=lambdafield,item:TCPersonFull),)classTCPersonFull(RDFUtilsModelBaseClass):id:str=Field(...,rdfconfig=FieldConfigurationRDF(anchor=True,path="person"))name:str=Field(...,rdfconfig=FieldConfigurationRDF(path="entityLabel"))events:list["TCEventFull"]=NoneclassTCPlaceFull(RDFUtilsModelBaseClass):id:str=Field(...,rdfconfig=FieldConfigurationRDF(anchor=True,path="person"))name:str=Field(...,rdfconfig=FieldConfigurationRDF(path="entityLabel"))events:list["TCEventFull"]=NoneclassTCEventFull(RDFUtilsModelBaseClass):id:str=Field(...,rdfconfig=FieldConfigurationRDF(anchor=True,path="event"))label:str=Field(...,rdfconfig=FieldConfigurationRDF(path="eventLabel"))TCPaginatedResponse.update_forward_refs()TCPersonFull.update_forward_refs()TCEventFull.update_forward_refs()test_data.json{"page":1,"count":1,"pages":1,"results":[{"count":1,"person":"http://www.intavia.eu/apis/personproxy/27118","entityTypeLabel":"person","entityLabel":"Tesla, Nikola","linkedIds":"https://apis-edits.acdh-dev.oeaw.ac.at/entity/27118/","role":"http://www.intavia.eu/apis/deceased_person/27118","event":"http://www.intavia.eu/apis/deathevent/27118","evPlace":"http://www.intavia.eu/apis/place/25209","evPlaceLatLong":"Point ( -74.00597 +40.71427 )","evPlaceLabel":"New York City","eventLabel":"Death of Nikola Tesla","end":"1943-01-07 23:59:59+00:00"},{"count":1,"person":"http://www.intavia.eu/apis/personproxy/27118","entityTypeLabel":"person","entityLabel":"Tesla, Nikola","linkedIds":"https://apis.acdh.oeaw.ac.at/entity/27118","role":"http://www.intavia.eu/apis/deceased_person/27118","event":"http://www.intavia.eu/apis/deathevent/27118","evPlace":"http://www.intavia.eu/apis/place/25209","evPlaceLatLong":"Point ( -74.00597 +40.71427 )","evPlaceLabel":"New York City","eventLabel":"Death of Nikola Tesla","end":"1943-01-07 23:59:59+00:00"},{"count":1,"person":"http://www.intavia.eu/apis/personproxy/27118","entityTypeLabel":"person","entityLabel":"Tesla, Nikola","linkedIds":"https://apis-edits.acdh-dev.oeaw.ac.at/entity/27118/","role":"http://www.intavia.eu/apis/personplace/eventrole/155790","event":"http://www.intavia.eu/apis/event/personplace/155790","evPlace":"http://www.intavia.eu/apis/place/129965","evPlaceLatLong":"Point ( +15.31806 +44.56389 )","evPlaceLabel":"Smiljan","roleLabel":"ausgebildet in","eventLabel":"Tesla, Nikola ausgebildet in Smiljan","start":"1862-01-01 00:00:00+00:00","end":"1866-12-31 23:59:59+00:00"},{"count":1,"person":"http://www.intavia.eu/apis/personproxy/27118","entityTypeLabel":"person","entityLabel":"Tesla, Nikola","linkedIds":"https://apis.acdh.oeaw.ac.at/entity/27118","role":"http://www.intavia.eu/apis/personplace/eventrole/155790","event":"http://www.intavia.eu/apis/event/personplace/155790","evPlace":"http://www.intavia.eu/apis/place/129965","evPlaceLatLong":"Point ( +15.31806 +44.56389 )","evPlaceLabel":"Smiljan","roleLabel":"ausgebildet in","eventLabel":"Tesla, Nikola ausgebildet in Smiljan","start":"1862-01-01 00:00:00+00:00","end":"1866-12-31 23:59:59+00:00"}]}Running the following with the above files in place will result in a correctly nested python object:importjsonfrom.modelsimportTCPaginatedResponsewithopen("test_data.json")asinp:data=json.load(inp)res=TCPaginatedResponse(**data)print(res)