-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: discontinue use of SPARQLWrapper library
- Loading branch information
Showing
5 changed files
with
46 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
from rdfproxy.adapter import SPARQLModelAdapter # noqa: F401 | ||
from rdfproxy.sparql_strategies import ( | ||
SPARQLStrategy, # noqa: F401 | ||
SPARQLWrapperStrategy, # noqa: F401 | ||
HttpxStrategy, # noqa: F401 | ||
) | ||
from rdfproxy.sparqlwrapper import SPARQLWrapper # noqa: F401 | ||
from rdfproxy.utils._types import ConfigDict, SPARQLBinding # noqa: F401 | ||
from rdfproxy.utils.models import Page, QueryParameters # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from collections.abc import Iterator | ||
|
||
import httpx | ||
|
||
|
||
class SPARQLWrapper: | ||
"""Simple httpx-based SPARQLWrapper implementaton for RDFProxy.""" | ||
|
||
def __init__(self, endpoint: str): | ||
self.endpoint = endpoint | ||
|
||
def query(self, query: str) -> Iterator[dict[str, str]]: | ||
"""Run a SPARQL query against endpoint and return an Iterator of flat result mappings.""" | ||
result: httpx.Response = self._httpx_run_sparql_query(query) | ||
return self._get_bindings_from_bindings_dict(result.json()) | ||
|
||
@staticmethod | ||
def _get_bindings_from_bindings_dict(bindings_dict: dict) -> Iterator[dict]: | ||
bindings = map( | ||
lambda binding: {k: v["value"] for k, v in binding.items()}, | ||
bindings_dict["results"]["bindings"], | ||
) | ||
return bindings | ||
|
||
def _httpx_run_sparql_query(self, query: str) -> httpx.Response: | ||
data = {"output": "json", "query": query} | ||
headers = { | ||
"Accept": "application/sparql-results+json", | ||
} | ||
|
||
response = httpx.post( | ||
self.endpoint, | ||
headers=headers, | ||
data=data, | ||
) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters