Skip to content

Commit

Permalink
docs: Add basic README
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
lu-pl committed Jul 25, 2024
1 parent 9587ef9 commit bba0cb2
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# RDFProxy

[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

A collection of Python utilities for connecting RDF stores to FastAPI.


## SPARQLModelAdapter

The `rdfproxy.SPARQLModelAdapter` class allows to run a query against an endpoint and map a flat SPARQL query result set to a potentially nested Pydantic model.

E.g. the result set of the following query
```sparql
select ?x ?y ?a ?p
where {
values (?x ?y ?a ?p) {
(1 2 "a value" "p value")
}
}
```
can be run against an endpoint and mapped to a nested Pydantic model like so:

```python
from SPARQLWrapper import SPARQLWrapper
from pydantic import BaseModel
from rdfproxy import SPARQLModelAdapter

class SimpleModel(BaseModel):
x: int
y: int

class NestedModel(BaseModel):
a: str
b: SimpleModel

class ComplexModel(BaseModel):
p: str
q: NestedModel


sparql_wrapper = SPARQLWrapper("https://some.actual/endpoint")

with open("example_query.rq") as f:
query = f.read()

adapter = SPARQLModelAdapter(sparql_wrapper=sparql_wrapper)
models: Sequence[_TModelInstance] = adapter(query=query, model_constructor=ComplexModel)
```

This produces a Sequence of Pydantic model instances which can then be served via FastAPI.

The `model_constructor` parameter takes either a Pydantic model directly or a model_constructor callable which receives the raw `SPARQLWrapper.QueryResult` object and is responsible for returning a Sequence of model instances.

0 comments on commit bba0cb2

Please sign in to comment.