Skip to main content

Utils package for interacting with SPARQL endpoint via FastAPI

Project description

RDF-FastAPI-Utils

This 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.FieldConfigurationRDF to add additional information for processing RDF data to the fields in a pydantic model
  • and models.RDFUtilsModelBaseClass as a pydantic base class to inherit from for adding SPARQL Json to a pydantic model.

Minimal example

this is taken from the tests

models.py:

from pydantic import Field
from rdf_fastapi_utils.models import FieldConfigurationRDF, RDFUtilsModelBaseClass


class TCPaginatedResponse(RDFUtilsModelBaseClass):
    count: int = Field(..., rdfconfig=FieldConfigurationRDF(path="count"))
    results: list[Union["TCPersonFull", "TCPlaceFull"]] = Field(
        ...,
        rdfconfig=FieldConfigurationRDF(path="results", serialization_class_callback=lambda field, item: TCPersonFull),
    )


class TCPersonFull(RDFUtilsModelBaseClass):
    id: str = Field(..., rdfconfig=FieldConfigurationRDF(anchor=True, path="person"))
    name: str = Field(..., rdfconfig=FieldConfigurationRDF(path="entityLabel"))
    events: list["TCEventFull"] = None


class TCPlaceFull(RDFUtilsModelBaseClass):
    id: str = Field(..., rdfconfig=FieldConfigurationRDF(anchor=True, path="person"))
    name: str = Field(..., rdfconfig=FieldConfigurationRDF(path="entityLabel"))
    events: list["TCEventFull"] = None


class TCEventFull(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:

import json
from .models import TCPaginatedResponse

with open("test_data.json") as inp:
    data = json.load(inp)
    res = TCPaginatedResponse(**data)

print(res)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rdf_fastapi_utils-0.1.3.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

rdf_fastapi_utils-0.1.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file rdf_fastapi_utils-0.1.3.tar.gz.

File metadata

  • Download URL: rdf_fastapi_utils-0.1.3.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Linux/5.19.13-300.fc37.x86_64

File hashes

Hashes for rdf_fastapi_utils-0.1.3.tar.gz
Algorithm Hash digest
SHA256 473605db85de88c188bb86843270063dd79447c37f029e57cff0cd692f89f931
MD5 5713da5471bdddb9acc77400b0af9fa1
BLAKE2b-256 38908f3b54d984f2649dbf385ddfecd3728804beff4fcf25095bae78036bb16e

See more details on using hashes here.

File details

Details for the file rdf_fastapi_utils-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: rdf_fastapi_utils-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.8 Linux/5.19.13-300.fc37.x86_64

File hashes

Hashes for rdf_fastapi_utils-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 de7c308cb8f118dd1287b0a14ac418b81f8066cf293d3f9ff6611c648d95953d
MD5 c77dce8d681f2d45c51d01f124f5c201
BLAKE2b-256 7e89a199a3f8acb149073076b7b2ca6fd0610531e84e0540c84ff5efcbbd4de1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page