### Build and Run Example Source: https://github.com/openfga/java-sdk/blob/main/examples/streamed-list-objects/README.md Instructions for building the SDK and running the streamed list objects example. ```bash # From the SDK root directory, build the SDK first ./gradlew build # Then run the example cd examples/streamed-list-objects ./gradlew run ``` ```bash make build make run ``` -------------------------------- ### Run Example with Manual OpenTelemetry Configuration Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Execute the example using Gradle with manual OpenTelemetry configuration. This approach requires adding OpenTelemetry SDK dependencies to your application and configuring them in code. ```bash ./gradlew run ``` -------------------------------- ### Run Example with Java Agent Instrumentation Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Execute the example using Gradle with the Java Agent for automatic OpenTelemetry instrumentation. This method requires no code changes and automatically instruments the OpenFGA SDK. ```bash ./gradlew runWithAgent ``` -------------------------------- ### GET Request Example Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Example of making a GET request using ApiExecutor, demonstrating path parameter usage and handling the response. ```APIDOC ### GET Request (ApiExecutor) ```java ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.GET, "/stores/{store_id}/feature") .pathParam("store_id", storeId) .build(); client.apiExecutor().send(request, FeatureResponse.class) .thenAccept(r -> System.out.println("Status: " + r.getStatusCode())); ``` ``` -------------------------------- ### Beta Versioning Example Source: https://github.com/openfga/java-sdk/blob/main/RELEASE.md Illustrates how to use the 'explicit' option for managing beta releases and incrementing pre-release suffixes. ```text 0.10.0-beta.1 → explicit: 0.10.0-beta.2 → explicit: 0.10.0 ``` -------------------------------- ### Start OpenTelemetry Collector Stack Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Clone the OpenTelemetry Collector setup repository and start the services using Docker Compose. This provides Jaeger, Prometheus, and Grafana for observing metrics and traces. ```bash # Clone the OpenTelemetry Collector setup git clone https://github.com/ewanharris/opentelemetry-collector-dev-setup.git otel-collector cd otel-collector # Start the services docker-compose up -d ``` -------------------------------- ### Build Main SDK Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Build the main OpenFGA Java SDK from the repository root before building the example. This ensures that the local SDK changes are compiled and available. ```bash cd ../.. ./gradlew build cd examples/opentelemetry ``` -------------------------------- ### POST with Body Example Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Example of a POST request with a request body and query parameters using ApiExecutor. ```APIDOC ### POST with Body (ApiExecutor) ```java ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.POST, "/stores/{store_id}/bulk-delete") .pathParam("store_id", storeId) .queryParam("force", "true") .body(new BulkDeleteRequest("2023-01-01", "user", 1000)) .build(); client.apiExecutor().send(request, BulkDeleteResponse.class).get(); ``` ``` -------------------------------- ### Get Store with Options Source: https://github.com/openfga/java-sdk/blob/main/README.md Retrieve information about the current store. Requires the client to be initialized with a store ID. Optional `ClientGetStoreOptions` can be used for additional headers. ```java var options = new ClientGetStoreOptions().additionalHeaders(Map.of("Some-Http-Header", "Some value")); var store = fgaClient.getStore(options).get(); // store = { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" } ``` -------------------------------- ### List Stores with OpenFgaApi Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to list stores using the OpenFgaApi. Includes setup for the API client and handling of potential exceptions. Ensure the base path is correctly configured. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); Integer pageSize = 56; // Integer | String continuationToken = "continuationToken_example"; // String | String name = "name_example"; // String | The name parameter instructs the API to only include results that match that name.Multiple results may be returned. Only exact matches will be returned; substring matches and regexes will not be evaluated try { CompletableFuture result = apiInstance.listStores(pageSize, continuationToken, name); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#listStores"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Read API Response Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This is an example of a successful response from the read API, including the retrieved tuples and a continuation token for pagination. ```json { "tuples": [ { "key": { "user": "user:bob", "relation": "reader", "object": "document:2021-budget" }, "timestamp": "2021-10-06T15:32:11.128Z" } ], "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ" } ``` -------------------------------- ### Configure OpenFGA Connection Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Copy the example environment file and edit it with your specific OpenFGA store details, such as store ID and model ID. This file is used to configure the OpenFGA client connection. ```bash cp .env.example .env # Edit .env with your OpenFGA store details ``` -------------------------------- ### Write API Example - Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to use the OpenFgaApi write method to add or delete tuples. Ensure the base path is correctly configured and handle potential API exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | WriteRequest body = new WriteRequest(); // WriteRequest | try { CompletableFuture result = apiInstance.write(storeId, body); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#write"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### List Users with HTTP Info in Java Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to call the listUsersWithHttpInfo method to retrieve users. Includes setup for the API client and handling of API responses and exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ListUsersRequest body = new ListUsersRequest(); // ListUsersRequest | try { CompletableFuture> response = apiInstance.listUsersWithHttpInfo(storeId, body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#listUsers"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#listUsers"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### List Users Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to call the listUsers API to retrieve users with a specific relation to an object. Ensure the base path is correctly configured and handle potential API exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ListUsersRequest body = new ListUsersRequest(); // ListUsersRequest | try { CompletableFuture result = apiInstance.listUsers(storeId, body); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#listUsers"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Example Conventional Commits Source: https://github.com/openfga/java-sdk/blob/main/RELEASE.md Examples of commit messages following Conventional Commits format and their corresponding changelog entries. ```text feat: add support for batch check → Added fix: correct retry logic for transient errors → Fixed docs: update API reference → Documentation perf: cache DNS lookups → Changed refactor: extract auth helper → (hidden) chore: bump dependencies → (hidden) ``` -------------------------------- ### Get Store Source: https://github.com/openfga/java-sdk/blob/main/README.md Get information about the current store. Requires a client initialized with a storeId. Passing ClientGetStoreOptions is optional. ```APIDOC ## Get Store ### Description Get information about the current store. ### Method GET ### Endpoint /stores/{store_id} ### Parameters #### Path Parameters - **store_id** (string) - Required - The ID of the store to retrieve. #### Request Body None ### Request Example ```java var options = new ClientGetStoreOptions().additionalHeaders(Map.of("Some-Http-Header", "Some value")); var store = fgaClient.getStore(options).get(); ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier of the store. - **name** (string) - The name of the store. - **created_at** (string) - The timestamp when the store was created. - **updated_at** (string) - The timestamp when the store was last updated. #### Response Example ```json { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" } ``` ``` -------------------------------- ### Read API Response Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This is an example of a response from the Read API, which includes a list of matching tuples and a continuation token if there are more results. ```json { "tuples": [ { "key": { "user": "user:bob", "relation": "reader", "object": "document:2021-budget" }, "timestamp": "2021-10-06T15:32:11.128Z" } ], "continuation_token": "eyJwayI6IkxBVEVTVF9OU0NPTkZJR19hdXRoMHN0b3JlIiwic2siOiIxem1qbXF3MWZLZExTcUoyN01MdTdqTjh0cWgifQ==" } ``` -------------------------------- ### getStore Source: https://github.com/openfga/java-sdk/blob/main/README.md Get a store. ```APIDOC ## GET /stores/{store_id} ### Description Get a store. ### Method GET ### Endpoint /stores/{store_id} ``` -------------------------------- ### Expand Relationship Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to expand a relationship in OpenFGA using the Java SDK. This is useful for understanding the full set of users or groups that have a specific relationship to an object. ```json { "type": "object", "properties": { "page_title": { "type": "string", "description": "Concise title for the documentation page" }, "page_description": { "type": "string", "description": "Brief description of the page content" }, "page_summary": { "type": "string", "description": "Summary of the main concepts covered" }, "codeSnippets": { "type": "array", "items": { "type": "object", "properties": { "title": { "type": "string", "description": "Title of the code snippet" }, "description": { "type": "string", "description": "Useful information about the code snippet" }, "language": { "type": "string", "description": "Primary programming language" }, "codeList": { "type": "array", "items": { "type": "object", "properties": { "language": { "type": "string" }, "code": { "type": "string" } }, "required": [ "language", "code" ] } } }, "required": [ "title", "description", "language", "codeList" ] } } }, "required": [ "page_title", "page_description", "page_summary", "codeSnippets" ] } ``` -------------------------------- ### Get Store Information with HTTP Details (Java) Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to retrieve a store's details using the `getStoreWithHttpInfo` method. It includes setting up the API client, making the request, and handling both successful responses and potential exceptions, including detailed error information from the API. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | try { CompletableFuture> response = apiInstance.getStoreWithHttpInfo(storeId); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#getStore"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#getStore"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### List Objects with HTTP Info in Java Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to use the listObjectsWithHttpInfo method to retrieve a list of objects. This example includes setting up the API client, making the request, and handling potential exceptions. It's recommended to specify the authorization model ID for better performance and to provide all condition parameters for accurate tuple evaluation. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ListObjectsRequest body = new ListObjectsRequest(); // ListObjectsRequest | try { CompletableFuture> response = apiInstance.listObjectsWithHttpInfo(storeId, body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#listObjects"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#listObjects"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### read Source: https://github.com/openfga/java-sdk/blob/main/README.md Get tuples from the store that matches a query, without following userset rewrite rules. ```APIDOC ## POST /stores/{store_id}/read ### Description Get tuples from the store that matches a query, without following userset rewrite rules. ### Method POST ### Endpoint /stores/{store_id}/read ``` -------------------------------- ### Java SDK Read Operation Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This snippet shows how to initialize the OpenFgaApi client and perform a read operation. Ensure you have the necessary imports and configure the API client's base path before execution. Error handling for API exceptions is included. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ReadRequest body = new ReadRequest(); // ReadRequest | try { CompletableFuture result = apiInstance.read(storeId, body); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#read"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Write Authorization Model with HttpInfo Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This example demonstrates how to create a new authorization model using the `writeAuthorizationModelWithHttpInfo` method. It includes setting up the API client, defining the model structure, and handling the asynchronous response, including potential exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | WriteAuthorizationModelRequest body = new WriteAuthorizationModelRequest(); // WriteAuthorizationModelRequest | try { CompletableFuture> response = apiInstance.writeAuthorizationModelWithHttpInfo(storeId, body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#writeAuthorizationModel"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#writeAuthorizationModel"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### Get Store Information with OpenFgaApi Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This snippet demonstrates how to retrieve store information using the `getStore` method. It initializes the API client, sets the base path, and handles potential API exceptions. Ensure all necessary imports are included. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | try { CompletableFuture result = apiInstance.getStore(storeId); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#getStore"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### List Stores Source: https://github.com/openfga/java-sdk/blob/main/README.md Get a paginated list of stores. Passing ClientListStoresOptions is optional. ```APIDOC ## List Stores ### Description Get a paginated list of stores. ### Method GET ### Endpoint /stores ### Parameters #### Query Parameters - **pageSize** (integer) - Optional - The maximum number of stores to return. - **continuationToken** (string) - Optional - The continuation token for pagination. #### Request Body None ### Request Example ```java var options = new ClientListStoresOptions() .additionalHeaders(Map.of("Some-Http-Header", "Some value")) .pageSize(10) .continuationToken("..."); var stores = fgaClient.listStores(options); ``` ### Response #### Success Response (200) - **stores** (array) - A list of store objects. - **id** (string) - The unique identifier of the store. - **name** (string) - The name of the store. - **created_at** (string) - The timestamp when the store was created. - **updated_at** (string) - The timestamp when the store was last updated. #### Response Example ```json { "stores": [ { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" } ] } ``` ``` -------------------------------- ### Check for Self-Defining Usersets Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This example demonstrates a Check query for a userset that is self-defining according to the authorization model. Such queries will always return true. ```python model schema 1.1 type user type document relations define reader: [user] ``` ```json { "tuple_key": { "user": "document:2021-budget#reader", "relation": "reader", "object": "document:2021-budget" } } ``` -------------------------------- ### Read Authorization Model Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to call the `readAuthorizationModel` method to retrieve a specific version of an authorization model. Ensure the base path is correctly configured and handle potential API exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | String id = "id_example"; // String | try { CompletableFuture result = apiInstance.readAuthorizationModel(storeId, id); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#readAuthorizationModel"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Read Changes with OpenFgaApi Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to use the `readChanges` method to retrieve a paginated list of tuple changes from a store. Includes setup for the API client and error handling. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | String type = "type_example"; // String | Integer pageSize = 56; // Integer | String continuationToken = "continuationToken_example"; // String | OffsetDateTime startTime = OffsetDateTime.now(); // OffsetDateTime | Start date and time of changes to read. Format: ISO 8601 timestamp (e.g., 2022-01-01T00:00:00Z) If a continuation_token is provided along side start_time, the continuation_token will take precedence over start_time. try { CompletableFuture result = apiInstance.readChanges(storeId, type, pageSize, continuationToken, startTime); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#readChanges"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Write Assertions with OpenFgaApi Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This example demonstrates how to write assertions for an authorization model using the `writeAssertions` method. It handles potential API exceptions. Ensure the store and authorization model IDs are correct. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | String authorizationModelId = "authorizationModelId_example"; // String | WriteAssertionsRequest body = new WriteAssertionsRequest(); // WriteAssertionsRequest | try { CompletableFuture result = apiInstance.writeAssertions(storeId, authorizationModelId, body); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#writeAssertions"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Streamed List Objects Example in Java Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This Java code snippet shows how to initialize the OpenFgaApi client, set the base path, and call the `streamedListObjectsWithHttpInfo` method. It includes comprehensive error handling for potential API exceptions and execution issues. Ensure the base path is correctly configured for your OpenFGA deployment. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ListObjectsRequest body = new ListObjectsRequest(); // ListObjectsRequest | try { CompletableFuture> response = apiInstance.streamedListObjectsWithHttpInfo(storeId, body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#streamedListObjects"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#streamedListObjects"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### Migrating from API Executor to Typed Methods Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Example showing the transition from using the generic API Executor to a specific typed SDK method for a 'check' operation. Typed methods are preferred when available. ```java // API Executor ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.POST, "/stores/{store_id}/check") .body(req) .build(); client.apiExecutor().send(request, CheckResponse.class).get(); // Typed SDK (when available) client.check(req).get(); ``` -------------------------------- ### Query OpenFGA Metrics with Prometheus Source: https://github.com/openfga/java-sdk/blob/main/examples/opentelemetry/README.md Examples of Prometheus queries to analyze OpenFGA request and query durations, and credentials request rates. These queries help in understanding the performance and behavior of the FGA client. ```promql # Average request duration by method rate(fga_client_request_duration_sum[5m]) / rate(fga_client_request_duration_count[5m]) ``` ```promql # Request rate by HTTP status code rate(fga_client_request_duration_count[5m]) ``` ```promql # 95th percentile request duration histogram_quantile(0.95, rate(fga_client_request_duration_bucket[5m])) ``` -------------------------------- ### Java Batch Check Example Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md This snippet shows how to perform a batch check using the OpenFgaApi Java SDK. Ensure the API client is configured with the correct base path and that the store ID and request body are properly set. Error handling for API exceptions is included. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | BatchCheckRequest body = new BatchCheckRequest(); // BatchCheckRequest | try { CompletableFuture> response = apiInstance.batchCheckWithHttpInfo(storeId, body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#batchCheck"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#batchCheck"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### Streaming Endpoint Example Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Example of using StreamingApiExecutor to process a streaming response, collecting objects into a list. ```APIDOC ### Streaming endpoint (StreamingApiExecutor) ```java ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.POST, "/stores/{store_id}/streamed-list-objects") .body(new ListObjectsRequest().user("user:anne").relation("viewer").type("document")) .build(); List objects = new ArrayList<>(); client.streamingApiExecutor(StreamedListObjectsResponse.class) .stream(request, response -> objects.add(response.getObject())) .thenRun(() -> System.out.println("Received " + objects.size() + " objects")); ``` ``` -------------------------------- ### Create Store Source: https://github.com/openfga/java-sdk/blob/main/README.md Initialize a store. Passing ClientCreateStoreOptions is optional. ```APIDOC ## Create Store ### Description Initialize a store. ### Method POST ### Endpoint /stores ### Parameters #### Request Body - **name** (string) - Required - The name of the store to create. #### Request Example ```java var request = new CreateStoreRequest().name("FGA Demo"); var options = new ClientCreateStoreOptions().additionalHeaders(Map.of("Some-Http-Header", "Some value")); var store = fgaClient.createStore(request, options).get(); ``` ### Response #### Success Response (200) - **id** (string) - The unique identifier of the newly created store. - **name** (string) - The name of the store. - **created_at** (string) - The timestamp when the store was created. - **updated_at** (string) - The timestamp when the store was last updated. #### Response Example ```json { "id": "01FQH7V8BEG3GPQW93KTRFR8JB", "name": "FGA Demo Store", "created_at": "2022-01-01T00:00:00.000Z", "updated_at": "2022-01-01T00:00:00.000Z" } ``` ``` -------------------------------- ### Create Store with HTTP Info - Java Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to create a new OpenFGA store using the Java SDK. It includes handling API responses and exceptions, and setting the base path for the API client. Use this when you need to programmatically create stores and inspect the full HTTP response details. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.client.ApiResponse; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); CreateStoreRequest body = new CreateStoreRequest(); // CreateStoreRequest | try { CompletableFuture> response = apiInstance.createStoreWithHttpInfo(body); System.out.println("Status code: " + response.get().getStatusCode()); System.out.println("Response headers: " + response.get().getHeaders()); System.out.println("Response body: " + response.get().getData()); } catch (InterruptedException | ExecutionException e) { ApiException apiException = (ApiException)e.getCause(); System.err.println("Exception when calling OpenFgaApi#createStore"); System.err.println("Status code: " + apiException.getCode()); System.err.println("Response headers: " + apiException.getResponseHeaders()); System.err.println("Reason: " + apiException.getResponseBody()); e.printStackTrace(); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#createStore"); System.err.println("Status code: " + e.getCode()); System.err.println("Response headers: " + e.getResponseHeaders()); System.err.println("Reason: " + e.getResponseBody()); e.printStackTrace(); } } } ``` -------------------------------- ### Send GET Request with Typed Response Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Demonstrates sending a GET request using the ApiExecutor and processing a typed response, specifically printing the status code. ```java ApiExecutorRequestBuilder request = ApiExecutorRequestBuilder.builder(HttpMethod.GET, "/stores/{store_id}/feature") .pathParam("store_id", storeId) .build(); client.apiExecutor().send(request, FeatureResponse.class) .thenAccept(r -> System.out.println("Status: " + r.getStatusCode())); ``` -------------------------------- ### Create Store with Options Source: https://github.com/openfga/java-sdk/blob/main/README.md Initialize a new store with a given name. Optional `ClientCreateStoreOptions` can be used for additional headers. The store ID should be stored and used to configure the client for subsequent API calls. ```java var request = new CreateStoreRequest().name("FGA Demo"); var options = new ClientCreateStoreOptions().additionalHeaders(Map.of("Some-Http-Header", "Some value")); var store = fgaClient.createStore(request, options).get(); // store.getId() = "01FQH7V8BEG3GPQW93KTRFR8JB" // store the store.getId() in database // update the storeId of the client instance fgaClient.setStoreId(store.getId()); // continue calling the API normally ``` -------------------------------- ### ApiResponse Get Headers Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Retrieves the response headers from an ApiResponse as a map. ```java Map> getHeaders() // Response headers ``` -------------------------------- ### ApiResponse Get Status Code Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Retrieves the HTTP status code from an ApiResponse. ```java int getStatusCode() // HTTP status ``` -------------------------------- ### ApiResponse Get Raw Response Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Retrieves the raw JSON body of the response from an ApiResponse. ```java String getRawResponse() // Raw JSON body ``` -------------------------------- ### Create Store with OpenFgaApi Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Shows how to create a new store using the OpenFgaApi Java SDK. This is used to store authorization models and relationship tuples. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); CreateStoreRequest body = new CreateStoreRequest(); // CreateStoreRequest | try { CompletableFuture result = apiInstance.createStore(body); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#createStore"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### ApiResponse Get Data Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Retrieves the deserialized data from an ApiResponse, cast to the expected type T. ```java T getData() // Deserialized data ``` -------------------------------- ### List Objects with Java SDK Source: https://github.com/openfga/java-sdk/blob/main/docs/OpenFgaApi.md Demonstrates how to use the listObjects method from the OpenFgaApi class. Ensure the ApiClient is configured with the correct base path. Handles potential API exceptions. ```java import dev.openfga.sdk.api.client.ApiClient; import dev.openfga.sdk.api.client.ApiException; import dev.openfga.sdk.api.configuration.Configuration; import dev.openfga.sdk.api.client.models.*; import dev.openfga.sdk.api.OpenFgaApi; import java.util.concurrent.CompletableFuture; public class Example { public static void main(String[] args) { ApiClient defaultClient = Configuration.getDefaultApiClient(); defaultClient.setBasePath("http://localhost"); OpenFgaApi apiInstance = new OpenFgaApi(defaultClient); String storeId = "storeId_example"; // String | ListObjectsRequest body = new ListObjectsRequest(); // ListObjectsRequest | try { CompletableFuture result = apiInstance.listObjects(storeId, body); System.out.println(result.get()); } catch (ApiException e) { System.err.println("Exception when calling OpenFgaApi#listObjects"); System.err.println("Status code: " + e.getCode()); System.err.println("Reason: " + e.getResponseBody()); System.err.println("Response headers: " + e.getResponseHeaders()); e.printStackTrace(); } } } ``` -------------------------------- ### Raw JSON Response Example Source: https://github.com/openfga/java-sdk/blob/main/docs/ApiExecutor.md Demonstrates how to retrieve the raw JSON response body from an ApiExecutor call. ```APIDOC ### Raw JSON Response (ApiExecutor) ```java ApiResponse response = client.apiExecutor().send(request).get(); String json = response.getRawResponse(); ``` ```