Skip to content

Commit

Permalink
chore: flesh out example
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Dec 27, 2023
1 parent f0bd5cf commit 0432855
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 83 deletions.
251 changes: 168 additions & 83 deletions example/example1/src/main/java/dev/openfga/sdk/example/Example1.java
Original file line number Diff line number Diff line change
@@ -1,100 +1,185 @@
package dev.openfga.sdk.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.ClientAssertion;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
import dev.openfga.sdk.api.configuration.ClientCredentials;
import dev.openfga.sdk.api.configuration.Credentials;
import dev.openfga.sdk.api.client.model.*;
import dev.openfga.sdk.api.configuration.*;
import dev.openfga.sdk.api.model.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

class Example1 {

public static void main(String[] args) throws Exception {
var credentials = new Credentials();
if (System.getenv("FGA_CLIENT_ID") != null) {
credentials = new Credentials(new ClientCredentials()
.apiAudience(System.getenv("FGA_API_AUDIENCE"))
.apiTokenIssuer(System.getenv("FGA_TOKEN_ISSUER"))
.clientId("FGA_CLIENT_ID")
.clientSecret("FGA_CLIENT_SECRET"));
} else {
System.out.println("Proceeding with no credentials (expecting localhost)");
}

var configuration = new ClientConfiguration()
.apiUrl(System.getenv("FGA_API_URL")) // required, e.g. https://api.fga.example
.storeId(System.getenv("FGA_STORE_ID")) // not needed when calling `CreateStore` or `ListStores`
.authorizationModelId(
System.getenv("FGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
.credentials(credentials);
var fgaClient = new OpenFgaClient(configuration);

// ListStores
System.out.println("Listing Stores");
var stores1 = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores1.getStores().size());

// CreateStore
System.out.println("Creating Test Store");
var store = fgaClient
.createStore(new CreateStoreRequest().name("Test Store"))
.get();
System.out.println("Test Store ID: " + store.getId());

// Set the store id
fgaClient.setStoreId(store.getId());

// ListStores after Create
System.out.println("Listing Stores");
var stores = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores.getStores().size());

// GetStore
System.out.println("Getting Current Store");
var currentStore = fgaClient.getStore().get();
System.out.println("Current Store Name: " + currentStore.getName());

// ReadAuthorizationModels
System.out.println("Reading Authorization Models");
var models = fgaClient.readAuthorizationModels().get();
System.out.println("Models Count: " + models.getAuthorizationModels().size());

// ReadLatestAuthorizationModel
public static void main(String[] args) {
try {
var latestAuthorizationModel = fgaClient
.readLatestAuthorizationModel()
.get(); // TODO: Should this really return null? Optional<...Response>?
var credentials = new Credentials();
if (System.getenv("FGA_CLIENT_ID") != null) {
credentials = new Credentials(new ClientCredentials()
.apiAudience(System.getenv("FGA_API_AUDIENCE"))
.apiTokenIssuer(System.getenv("FGA_TOKEN_ISSUER"))
.clientId("FGA_CLIENT_ID")
.clientSecret("FGA_CLIENT_SECRET"));
} else {
System.out.println("Proceeding with no credentials (expecting localhost)");
}

var configuration = new ClientConfiguration()
.apiUrl(System.getenv("FGA_API_URL")) // required, e.g. https://api.fga.example
.storeId(System.getenv("FGA_STORE_ID")) // not needed when calling `CreateStore` or `ListStores`
.authorizationModelId(
System.getenv("FGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
.credentials(credentials);
var fgaClient = new OpenFgaClient(configuration);

// ListStores
System.out.println("Listing Stores");
var stores1 = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores1.getStores().size());

// CreateStore
System.out.println("Creating Test Store");
var store = fgaClient
.createStore(new CreateStoreRequest().name("Test Store"))
.get();
System.out.println("Test Store ID: " + store.getId());

// Set the store id
fgaClient.setStoreId(store.getId());

// ListStores after Create
System.out.println("Listing Stores");
var stores = fgaClient.listStores().get();
System.out.println("Stores Count: " + stores.getStores().size());

// GetStore
System.out.println("Getting Current Store");
var currentStore = fgaClient.getStore().get();
System.out.println("Current Store Name: " + currentStore.getName());

// ReadAuthorizationModels
System.out.println("Reading Authorization Models");
var models = fgaClient.readAuthorizationModels().get();
System.out.println(
"Models Count: " + models.getAuthorizationModels().size());

// ReadLatestAuthorizationModel
try {
var latestAuthorizationModel = fgaClient
.readLatestAuthorizationModel()
.get(); // TODO: Should this really return null? Optional<...Response>?
System.out.println("Latest Authorization Model ID "
+ latestAuthorizationModel.getAuthorizationModel().getId());
} catch (Exception e) {
System.out.println("Latest Authorization Model not found");
}

var mapper = new ObjectMapper().findAndRegisterModules();

// WriteAuthorizationModel
var authModelJson = Files.readString(Paths.get("src", "main", "resources", "auth-model.json"));
var authorizationModel = fgaClient
.writeAuthorizationModel(mapper.readValue(authModelJson, WriteAuthorizationModelRequest.class))
.get();
System.out.println("Authorization Model ID " + authorizationModel.getAuthorizationModelId());

// ReadAuthorizationModels - after Write
System.out.println("Reading Authorization Models");
models = fgaClient.readAuthorizationModels().get();
System.out.println(
"Models Count: " + models.getAuthorizationModels().size());

// ReadLatestAuthorizationModel - after Write
var latestAuthorizationModel =
fgaClient.readLatestAuthorizationModel().get();
System.out.println("Latest Authorization Model ID "
+ latestAuthorizationModel.getAuthorizationModel().getId());
} catch (Exception e) {
System.out.println("Latest Authorization Model not found");
}

var mapper = new ObjectMapper().findAndRegisterModules();

// WriteAuthorizationModel
var authModelJson = Files.readString(Paths.get("src", "main", "resources", "auth-model.json"));
var authorizationModel = fgaClient
.writeAuthorizationModel(mapper.readValue(authModelJson, WriteAuthorizationModelRequest.class))
.get();
System.out.println("Authorization Model ID " + authorizationModel.getAuthorizationModelId());
// Set the model ID
fgaClient.setAuthorizationModelId(
latestAuthorizationModel.getAuthorizationModel().getId());

// Write
System.out.println("Writing Tuples");
fgaClient
.write(
new ClientWriteRequest()
.writes(List.of(new ClientTupleKey()
.user("user:anne")
.relation("writer")
._object("document:roadmap"))),
new ClientWriteOptions()
.disableTransactions(true)
.authorizationModelId(authorizationModel.getAuthorizationModelId()))
.get();
System.out.println("Done Writing Tuples");

// Read
System.out.println("Reading Tuples");
var readTuples = fgaClient.read(new ClientReadRequest()).get();
System.out.println("Read Tuples" + mapper.writeValueAsString(readTuples));

// ReadChanges
System.out.println("Reading Tuple Changess");
var readChangesTuples =
fgaClient.readChanges(new ClientReadChangesRequest()).get();
System.out.println("Read Changes Tuples" + mapper.writeValueAsString(readChangesTuples));

// Check
System.out.println("Checking for access");
try {
var failingCheckResponse = fgaClient
.check(new ClientCheckRequest()
.user("user:anne")
.relation("reader")
._object("document:roadmap"))
.get();
System.out.println("Allowed: " + failingCheckResponse.getAllowed());
} catch (Exception e) {
System.out.println("Failed due to: " + e.getMessage());
}

// Checking for access with context
// TODO: Add ClientCheckRequest.context
// System.out.println("Checking for access with context");
// var checkResponse = fgaClient
// .check(new ClientCheckRequest()
// .user("user:anne")
// .relation("reader")
// ._object("document:roadmap")
// .context(Map.of("ViewCount", 100)))
// .get();
// System.out.println("Allowed: " + checkResponse.getAllowed());

// WriteAssertions
fgaClient
.writeAssertions(List.of(
new ClientAssertion()
.user("user:carl")
.relation("writer")
._object("document:budget")
.expectation(true),
new ClientAssertion()
.user("user:anne")
.relation("reader")
._object("document:roadmap")
.expectation(false)))
.get();
System.out.println("Assertions updated");

// ReadAssertions
System.out.println("Reading Assertions");
var assertions = fgaClient.readAssertions().get();
System.out.println("Assertions " + mapper.writeValueAsString(assertions));

// DeleteStore
System.out.println("Deleting Current Store");
fgaClient.deleteStore().get();
System.out.println("Deleted Store: " + currentStore.getName());

// ReadAuthorizationModels - after Write
System.out.println("Reading Authorization Models");
models = fgaClient.readAuthorizationModels().get();
System.out.println("Models Count: " + models.getAuthorizationModels().size());

// ReadLatestAuthorizationModel - after Write
var latestAuthorizationModel = fgaClient.readLatestAuthorizationModel().get();
System.out.println("Latest Authorization Model ID "
+ latestAuthorizationModel.getAuthorizationModel().getId());

// Set the model ID
fgaClient.setAuthorizationModelId(
latestAuthorizationModel.getAuthorizationModel().getId());

System.out.println("MORE COMING SOON");
} catch (Exception e) {
System.err.printf("ERROR: %s%n", e);
}
}
}
6 changes: 6 additions & 0 deletions example/example1/src/main/resources/auth-model.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
"parameters": {
"ViewCount": {
"type_name": "TYPE_NAME_INT"
},
"Type": {
"type_name": "TYPE_NAME_STRING"
},
"Name": {
"type_name": "TYPE_NAME_STRING"
}
}
}
Expand Down

0 comments on commit 0432855

Please sign in to comment.