1. Tutorials¶
1.1. Getting started with the Java client library¶
In this tutorial you will create a Maven project that accesses the EDG API using the Java client library. It is based on the Java example.
Create a Maven project
Create a new Maven project with the usual structure (src/main/java
, et al.) and a pom.xml. The example pom.xml can be used as a guide.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.topbraid</groupId>
<artifactId>edg-client-example-java</artifactId>
<version>1.0.2-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<!-- Don't install this project -->
<skip>true</skip>
</configuration>
</plugin>
<!-- Use the exec-maven-plugin to run the example from the command line using Maven -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>ClientExample</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Client Java library dependency. Real projects should replace <version> with the latest library version. -->
<dependency>
<groupId>org.topbraid</groupId>
<artifactId>edg-client-java</artifactId>
<version>${project.version}</version>
</dependency>
<!-- The library uses SLF4J, supply an implementation. -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<!-- The client Maven artifact is in a private Maven repository. See the reference documentation for instructions on accessing this. -->
<repositories>
<repository>
<id>tq-maven-releases-public-ro</id>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
<url>https://nexus.topquadrant.net/repository/tq-maven-releases-public/</url>
</repository>
<repository>
<id>tq-maven-snapshots-public-ro</id>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
<url>https://nexus.topquadrant.net/repository/tq-maven-snapshots-public/</url>
</repository>
</repositories>
</project>
Create an entrypoint class
Create an entrypoint class such as Main.java
or ClientExample.java
in src/main/java
.
import org.topbraidlive.client.java.Client;
import org.topbraidlive.client.java.ClientConfiguration;
import org.topbraidlive.client.java.api.function.CreateProjectParameters;
import org.topbraidlive.client.java.api.type.ProjectGraphUri;
import org.topbraidlive.client.java.api.type.ProjectTypeUri;
import org.topbraidlive.client.java.api.type.Uri;
public final class ClientExample {
public static void main(final String[] argv) {
// Instantiate the client
final Client client = Client.create(ClientConfiguration.LOCALHOST_TEST);
// Check if the configured user can create a project
System.out.println(String.format("Can my user create a project?: %s", client.canCreateProject()));
// Check if the project exists
final String projectName = "example";
final ProjectGraphUri projectGraph = ProjectGraphUri.fromId(projectName);
System.out.println(String.format("Does %s exist?: %s", projectGraph, client.checkProjectExists(projectGraph)));
// Create the project
System.out.println(String.format("Creating project %s", projectGraph));
// The server can suffix the project graph we ask it to create
final ProjectGraphUri createdProjectGraph = client.createProject(CreateProjectParameters.builder().setDefaultNamespace(new Uri("http://example.com/data-graphs/test#")).setName(projectName).setProjectType(ProjectTypeUri.DATA_GRAPH).build());
System.out.println(String.format("Created project %s", projectGraph));
// Check if the project exists again
System.out.println(String.format("Does %s exist?: %s", projectGraph, client.checkProjectExists(createdProjectGraph)));
// Delete the project
System.out.println(String.format("Deleting project %s", createdProjectGraph));
client.deleteProject(createdProjectGraph);
System.out.println(String.format("Deleted project %s", createdProjectGraph));
// Check if the project exists again
System.out.println(String.format("Does %s exist?: %s", projectGraph, client.checkProjectExists(createdProjectGraph)));
System.out.println();
}
}
Configure the client
The example client uses a pre-defined configuration that corresponds to the Example EDG server configuration. You will need to specify a ClientConfiguration
that corresponds to your EDG installation. See the ClientConfiguration
Client library reference Javadocs for information on this object.
See Configure the Java client library using a Java servlet context for a more advanced example.
1.2. Getting started with the Python client library¶
In this tutorial you will create a Pytho project that accesses the EDG API using the Python client library. It is based on the Python example.
Create a Python project
Create a new directory for your project:
mkdir yourproject
Subsequent commands will be executed in this directory.
Create a Python virtual environment
python3 -m venv venv
The venv
directory should be .gitignored.
Activate the virtual environment
Unix:
source venv/bin/activate
Windows:
venv\Scripts\activate
Install the EDG client in the virtual environment
cd /path/to/client/repo/lib/py
python setup.py install
Then change back to your project directory.
Save your project dependencies
Use pip freeze
to record exact versions of your project’s dependencies:
pip freeze >requirements.txt
Since the virtual environment (venv
) is not committed to version control, you/your team need a mechanism to re-install exact versions of dependencies. The requirements.txt
serves this purpose. You can re-install the dependencies with:
pip install -r requirements.txt
Create an entrypoint file
The entrypoint should should resemble the client example:
import json
import os
from edg_client import (
Client,
ClientConfiguration,
ClientAuthenticationMethod,
UsernamePasswordClientCredentials,
)
def main():
# Instantiate the client.
# Assumes an EDG server is running on localhost:8080 and the client can use Basic authentication
# The Python Client can also be configured from environment variables, using ClientConfiguration.from_environment.
# See the reference documentation for details on the expected environment variables.
client = Client(
ClientConfiguration(
authentication_method=ClientAuthenticationMethod.BASIC,
credentials=UsernamePasswordClientCredentials(
password="password32", username="Admin_user"
),
server_base_url="http://localhost:8080",
)
)
# Query the platform API. Uses GraphQL under the hood.
print("Master graph URIs:")
master_graph_uris = client.list_projects()
for master_graph_uri in master_graph_uris:
print(" ", master_graph_uri)
print()
# Query the SPARQL endpoint.
# Returns a SPARQLWrapper (https://github.com/RDFLib/sparqlwrapper). See the documentation for that project.
sparql = client.sparql_endpoint()
sparql.setQuery(
"SELECT ?s ?p ?o FROM <%s> WHERE { ?s ?p ?o } LIMIT 1" % master_graph_uris[0]
)
sparql.setReturnFormat("json")
results = sparql.query().convert()
print("SPARQL results:")
print(results)
print()
# Open an arbitrary URL, letting the client handle authentication
with client.urlopen(
client.server_base_url
+ "/tbl/swp?_viewClass=teamwork%3AcanCreateProjectWrapper"
) as response:
assert response.getcode() == 200
response_body = json.load(response)
print(
"Authenticated user can create projects:",
response_body["canCreateProjects"],
)
if __name__ == "__main__" and os.environ.get("CI") is None:
main()
Note the inline comments.
Run the entrypoint
python client_example.py