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.2.tar.gz (8.4 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: rdf_fastapi_utils-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 9a861c2fa4ab3df09a5b99e73407e8c49a773efffcf04e81c01cd8732a74700e
MD5 ad16db2e68bd36994223a83170c17596
BLAKE2b-256 48264e9533b04cec4839f9dc1fdb4cccabd7d657547111e96f6f9757c1cfe1f8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: rdf_fastapi_utils-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0dceac98b1c687a8a4cd5b37bbe01b277821af0a0d85cd67748bedfe7d46662b
MD5 70f1e206e740c9ab5a5d244085880a3c
BLAKE2b-256 d9320dba0ba1ae8561697d7791a924eb286d81b6975cca1803ff37a8ca132372

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