Skip to content

Commit

Permalink
base implementation of sparql endpoint methods to add or remove datas…
Browse files Browse the repository at this point in the history
…ets #1890

(remove would be part of the delete method).
  • Loading branch information
jh-RLI committed Oct 21, 2024
1 parent 9f0815a commit 5da67ce
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions api/sparql_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import requests

from factsheet.oekg.connection import update_endpoint


def add_datasets_to_scenario(scenario_uuid, datasets, dataset_type):
"""
Function to add datasets to a scenario bundle in Jena Fuseki.
"""
for dataset in datasets:
sparql_query = f"""
PREFIX oeo: <http://example.org/ontology#>
INSERT DATA {{
GRAPH <http://example.org/scenario/{scenario_uuid}> {{
oeo:{dataset} a oeo:{dataset_type}Dataset .
}}
}}
"""
response = send_sparql_update(sparql_query)
if not response.ok:
return False # Return False if any query fails
return True


def remove_datasets_from_scenario(scenario_uuid, datasets, dataset_type):
"""
Function to remove datasets from a scenario bundle in Jena Fuseki.
"""
for dataset in datasets:
sparql_query = f"""
PREFIX oeo: <http://example.org/ontology#>
DELETE DATA {{
GRAPH <http://example.org/scenario/{scenario_uuid}> {{
oeo:{dataset} a oeo:{dataset_type}Dataset .
}}
}}
"""
response = send_sparql_update(sparql_query)
if not response.ok:
return False # Return False if any query fails
return True


def send_sparql_update(query):
"""
Helper function to send a SPARQL update query to Fuseki.
"""
headers = {"Content-Type": "application/sparql-update"}
response = requests.post(update_endpoint, data=query, headers=headers)
return response

0 comments on commit 5da67ce

Please sign in to comment.