Skip to content

marmotgraph-sdk

Java SDK for the MarmotGraph v4 API.

Install

The SDK is published to GitHub Packages. Add the repository and dependency:

<repositories>
    <repository>
        <id>github-marmotgraph</id>
        <url>https://maven.pkg.github.com/marmotgraph/marmotgraph-core-v4</url>
    </repository>
</repositories>

<dependency>
    <groupId>org.marmotgraph</groupId>
    <artifactId>marmotgraph-sdk</artifactId>
    <version>0.1.0</version>
</dependency>

GitHub Packages requires authentication even for public packages. Add a server entry to ~/.m2/settings.xml with a personal access token that has the read:packages scope (in GitHub Actions, ${{ secrets.GITHUB_TOKEN }} works):

<servers>
    <server>
        <id>github-marmotgraph</id>
        <username>YOUR_GITHUB_USER</username>
        <password>YOUR_TOKEN</password>
    </server>
</servers>

Publishing is handled by .github/workflows/java-sdk.yml: every push to dev touching sdks/java publishes the pom's -SNAPSHOT version; pushing a tag sdk-java-vX.Y.Z publishes release X.Y.Z.

For local development against this repository:

cd sdks/java
mvn clean package

Requires Java 17+. The only runtime dependency is jackson-databind. Models are records bound by Jackson (unknown fields are ignored, unknown enum values fall back to UNKNOWN, missing lists are empty lists). JSON-LD payloads (JsonLdDocument, Instance) are read from the generic JSON tree instead.

Quickstart

import org.marmotgraph.sdk.MarmotGraphClient;
import org.marmotgraph.sdk.model.Instance;
import org.marmotgraph.sdk.model.Stage;

MarmotGraphClient client = MarmotGraphClient.builder("https://core.marmotgraph.org")
        .token("<access-token>")
        .build();

// Read a single instance
Instance instance = client.instances().get(someUuid, Stage.RELEASED, ResponseOptions.DEFAULT);
System.out.println(instance.getUuid() + " " + instance.get("http://schema.org/name"));

// List and page through instances
Page<Instance> page = client.instances().list(
        InstanceListOptions.builder().types("https://openminds.ebrains.eu/core/Person").build());
page.allItems().forEach(person -> System.out.println(person.getUuid()));

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

Authentication

MarmotGraph uses Keycloak (OIDC). MarmotGraphClient.builder supports the common cases directly:

// A token you already have
MarmotGraphClient client = MarmotGraphClient.builder("https://core.marmotgraph.org").token("...").build();

// Service account / machine client (OAuth2 client-credentials grant;
// the token endpoint is discovered automatically from the server)
MarmotGraphClient client = MarmotGraphClient.builder("https://core.marmotgraph.org")
        .clientId("...").clientSecret("...").build();

// Interactive human login without a browser redirect (device flow) -
// prints a URL to visit and blocks until you complete it there
MarmotGraphClient client = MarmotGraphClient.withDeviceFlow("https://core.marmotgraph.org");

// Bring your own token source (e.g. hook into an existing auth stack)
MarmotGraphClient client = MarmotGraphClient.withCustomTokenProvider(
        "https://core.marmotgraph.org", () -> myApp.getToken());

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

Every builder setter also falls back to an environment variable (MARMOTGRAPH_HOST, MARMOTGRAPH_TOKEN, ...) when left unset, so MarmotGraphClient.builder().build() 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 LinkedHashMap subclass rather than a fixed schema. Instance adds getUuid() (the @id - always a plain UUID), getType() (the single @type) and getSpace().
  • Page<T> - every listing method returns a Page<T>. Iterate the current page directly (for (T item : page)), call page.next() for an Optional<Page<T>>, or page.allItems() for a lazy Stream<T> 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 throw MarmotGraphApiException (with statusCode(), message(), instanceId()) rather than returning a wrapped result object you have to check - the same departure from the Java server's Result/EmptyResult wrapper pattern that the Python SDK makes; exceptions are the idiomatic way to signal failure in both.
  • Stage - Stage.IN_PROGRESS (the working copy) or Stage.RELEASED. .defaultStage(...) on the builder 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<K, T> - 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/java
mvn clean test

Tests use JUnit 5 and a FakeHttpTransport test double instead of java.net.http - no live server or network access required.

License

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