Skip to content

Fuseki

kurra.db.fuseki

Functions to work with the Jena Fuseki RDF Database' API

FusekiError(message_context: str, message: str, status_code: int)

Bases: Exception

An error that occurred while interacting with Fuseki.

Source code in kurra/db/fuseki.py
def __init__(self, message_context: str, message: str, status_code: int) -> None:
    self.message = f"{status_code} {message_context}. {message}"
    super().__init__(self.message)

describe(base_url: str, dataset_name: str = None, http_client: httpx.Client | None = None) -> dict

Describe the datasetss or a single dataset in a Fuseki server instances.

:param base_url: The base URL of the Fuseki server. E.g., http://localhost:3030 :param dataset_name: The dataset to be described. If None (default), then all datasets will be listed :param http_client: The synchronous httpx client to be used. If this is not provided, a temporary one will be created. :raises FusekiError: If the datasets fail to list or the server responds with an invalid data structure. :returns: The Fuseki listing of datasets as a dictionary.

Source code in kurra/db/fuseki.py
def describe(
    base_url: str,
    dataset_name: str = None,
    http_client: httpx.Client | None = None,
) -> dict:
    """
    Describe the datasetss or a single dataset in a Fuseki server instances.

    :param base_url: The base URL of the Fuseki server. E.g., http://localhost:3030
    :param dataset_name: The dataset to be described. If None (default), then all datasets will be listed
    :param http_client: The synchronous httpx client to be used. If this is not provided, a temporary one will be created.
    :raises FusekiError: If the datasets fail to list or the server responds with an invalid data structure.
    :returns: The Fuseki listing of datasets as a dictionary.
    """
    close_http_client = False
    if http_client is None:
        http_client = httpx.Client()
        close_http_client = True

    headers = {"accept": "application/json"}
    url = (
        f"{base_url}/$/datasets/{dataset_name}"
        if dataset_name is not None
        else f"{base_url}/$/datasets"
    )
    r = http_client.get(url, headers=headers)

    if r.status_code != 200:
        raise FusekiError(
            f"Failed to list datasets at {base_url}", r.text, r.status_code
        )

    if close_http_client:
        http_client.close()

    try:
        if dataset_name is None:
            return r.json()["datasets"]
        else:
            return r.json()

    except KeyError:
        raise FusekiError(
            f"Failed to parse datasets r from {base_url}",
            r.text,
            r.status_code,
        )

delete(base_url: str, dataset_name: str, http_client: httpx.Client | None = None) -> str

Delete a Fuseki dataset.

:param base_url: The base URL of the Fuseki server. E.g., http://localhost:3030 :param dataset_name: The dataset to be deleted :param http_client: The synchronous httpx client to be used. If this is not provided, a temporary one will be created. :raises FusekiError: If the dataset fails to delete. :returns: A message indicating the successful deletion of the dataset.

Source code in kurra/db/fuseki.py
def delete(
    base_url: str, dataset_name: str, http_client: httpx.Client | None = None
) -> str:
    """
    Delete a Fuseki dataset.

    :param base_url: The base URL of the Fuseki server. E.g., http://localhost:3030
    :param dataset_name: The dataset to be deleted
    :param http_client: The synchronous httpx client to be used. If this is not provided, a temporary one will be created.
    :raises FusekiError: If the dataset fails to delete.
    :returns: A message indicating the successful deletion of the dataset.
    """
    if not dataset_name:
        raise ValueError("You must supply a dataset name")

    close_http_client = False
    if http_client is None:
        http_client = httpx.Client()
        close_http_client = True

    r = http_client.delete(f"{base_url}/$/datasets/{dataset_name}")

    if r.status_code != 200:
        raise FusekiError(
            f"Failed to delete dataset '{dataset_name}'", r.text, r.status_code
        )

    if close_http_client:
        http_client.close()

    return f"Dataset {dataset_name} deleted."