Skip to content

marmotgraph

Python SDK for the MarmotGraph v4 API.

Install

pip install marmotgraph-sdk

For local development against this repository:

cd sdks/python
pip install -e ".[dev]"

Requires Python 3.10+. The only runtime dependency is requests - responses are modeled with plain dataclasses, not a heavier validation library, to keep the install footprint small.

Quickstart

from marmotgraph import Client, Stage

client = Client("https://core.marmotgraph.org", token="<access-token>")

# Read a single instance
instance = client.instances.get(some_uuid, stage=Stage.RELEASED)
print(instance.uuid, instance["http://schema.org/name"])

# List and page through instances
page = client.instances.list(types=["https://openminds.ebrains.eu/core/Person"])
for person in page.all_items():  # lazily fetches further pages as needed
    print(person.uuid)

# Create an instance
created = client.instances.create("myspace", {
    "@type": "https://openminds.ebrains.eu/core/Person",
    "http://schema.org/name": "Ada Lovelace",
})

Authentication

MarmotGraph uses Keycloak (OIDC). The Client constructor supports the common cases directly:

# A token you already have (e.g. copied from a notebook login cell)
client = Client("https://core.marmotgraph.org", token="...")

# Service account / machine client (OAuth2 client-credentials grant;
# the token endpoint is discovered automatically from the server)
client = Client("https://core.marmotgraph.org", client_id="...", client_secret="...")

# Interactive human login without a browser redirect (device flow) -
# prints a URL to visit and blocks until you complete it there
client = Client.with_device_flow("https://core.marmotgraph.org")
# ... which is also the fallback (on first use) when no credentials are configured at all
client = Client("https://core.marmotgraph.org")

# Bring your own token source (e.g. hook into an existing auth stack)
client = Client.with_custom_token_provider("https://core.marmotgraph.org", lambda: my_app.get_token())

To forward per-request tokens from a service, build one client and derive a lightweight view per request with client.with_token(token) - see docs/authentication.md.

See docs/authentication.md for every option, including the lower-level TokenProvider classes in marmotgraph.auth.

Every one of Client's arguments (host, token, client id/secret, default stage, timeout, retry on 401) also falls back to an environment variable (MARMOTGRAPH_HOST, MARMOTGRAPH_TOKEN, ...) when left unset, so Client() with no arguments works in a properly configured environment. See docs/configuration.md.

Core concepts

  • Instance (and JsonLdDocument) - instance payloads are arbitrary JSON-LD, so they're modeled as a dict subclass rather than a fixed schema. Instance adds .uuid (the @id - always a plain UUID), .type (the single @type) and .space.
  • Page - every listing method returns a Page. Iterate the current page directly (for x in page), call page.next() for the next one, or page.all_items() to lazily walk every item across every page. The pages are chained by the opaque marker/nextMarker of the server (the API v4 doesn't support offset based pagination).
  • Errors - failed requests raise MarmotGraphAPIError (with .status_code, .message, .instance_id) rather than returning a wrapped result object you have to check. This is a deliberate departure from the Java client's Result/EmptyResult wrapper pattern - exceptions are the more idiomatic way to signal failure in Python.
  • Stage - Stage.IN_PROGRESS (the working copy) or Stage.RELEASED. Client(..., default_stage=...) sets the default used when a method's stage argument is omitted.
  • client.batch - server-side batches: create/update, delete, retract, sync, release or unrelease many instances in one request, processed set-based on the server; waits for the job by default. See docs/batch.md.
  • Batch - a thread-pool-backed queue for running many independent calls concurrently, for whatever client.batch doesn't cover. See docs/batch.md.

Guides

Development

cd sdks/python
python3 -m unittest discover -s tests -t .

Tests use only unittest/unittest.mock and fakes for requests.Session - no live server or network access required.

License

Apache License 2.0 - see the LICENSE file next to this README.