aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMunyoki Kilyungi2022-11-14 17:14:13 +0300
committerBonfaceKilz2022-11-16 11:02:27 +0300
commit749537c1831afb5644aba70eb24def39aef26104 (patch)
tree9ab446014e1f2792507f79e176446c62d64233b1
parentbec9a1d1c1611771bc16fd1f304e56b2e1810ed0 (diff)
downloadgenenetwork3-749537c1831afb5644aba70eb24def39aef26104.tar.gz
Inject SPARQLWrapper as a parameter
* gn3/db/rdf.py: Delete gn3.setting.SPARQL_ENDPOINT import. (sparql_query): Inject SPARQLWrapper. (get_dataset_metadata): Ditto.
-rw-r--r--gn3/db/rdf.py18
1 files changed, 11 insertions, 7 deletions
diff --git a/gn3/db/rdf.py b/gn3/db/rdf.py
index f1312e3..7ed80b1 100644
--- a/gn3/db/rdf.py
+++ b/gn3/db/rdf.py
@@ -9,21 +9,23 @@ from string import Template
from SPARQLWrapper import JSON, SPARQLWrapper
from pymonad.maybe import Just
from gn3.monads import MonadicDict
-from gn3.settings import SPARQL_ENDPOINT
-def sparql_query(query: str) -> Tuple[MonadicDict, ...]:
+def sparql_query(
+ sparql_conn: SPARQLWrapper, query: str
+) -> Tuple[MonadicDict, ...]:
"""Run a SPARQL query and return the bound variables."""
- sparql = SPARQLWrapper(SPARQL_ENDPOINT)
- sparql.setQuery(query)
- sparql.setReturnFormat(JSON)
- results = sparql.queryAndConvert()
+ sparql_conn.setQuery(query)
+ sparql_conn.setReturnFormat(JSON)
+ results = sparql_conn.queryAndConvert()
if _r := results["results"]["bindings"]: # type: ignore
return (*(MonadicDict(bindings) for bindings in _r),) # type: ignore
return (MonadicDict(),)
-def get_dataset_metadata(accession_id: str) -> MonadicDict:
+def get_dataset_metadata(
+ sparql_conn: SPARQLWrapper, accession_id: str
+) -> MonadicDict:
"""Return info about dataset with ACCESSION_ID."""
# Check accession_id to protect against query injection.
# TODO: This function doesn't yet return the names of the actual dataset
@@ -94,6 +96,7 @@ WHERE {
if not (
# Expecting only one result
sparql_result := sparql_query(
+ sparql_conn,
Template(query).substitute(accession_id=accession_id)
)[0]
):
@@ -103,6 +106,7 @@ WHERE {
result[key] = value.bind(lambda x: Just(x["value"]))
investigator_query_result = sparql_query(
+ sparql_conn,
Template(
"""
PREFIX gn: <http://genenetwork.org/>