Skip to content

Commit

Permalink
Merge pull request #24 from openfga/refactor/java-dont-require-apiclient
Browse files Browse the repository at this point in the history
refactor: simplify constructors, make client options consistent, bump to 0.1.0
  • Loading branch information
rhamzeh authored Sep 29, 2023
2 parents 971b93b + eeb1a76 commit 12af785
Show file tree
Hide file tree
Showing 19 changed files with 212 additions and 74 deletions.
10 changes: 6 additions & 4 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,18 @@ src/main/java/dev/openfga/sdk/api/configuration/ClientConfiguration.java
src/main/java/dev/openfga/sdk/api/configuration/ClientCredentials.java
src/main/java/dev/openfga/sdk/api/configuration/ClientExpandOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientListObjectsOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientListStoresOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAssertionsOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelsOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadChangesOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientReadOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientWriteAssertionsOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ClientWriteOptions.java
src/main/java/dev/openfga/sdk/api/configuration/Configuration.java
src/main/java/dev/openfga/sdk/api/configuration/ConfigurationOverride.java
src/main/java/dev/openfga/sdk/api/configuration/Credentials.java
src/main/java/dev/openfga/sdk/api/configuration/CredentialsMethod.java
src/main/java/dev/openfga/sdk/api/configuration/ListStoresOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelsOptions.java
src/main/java/dev/openfga/sdk/api/configuration/ReadChangesOptions.java
src/main/java/dev/openfga/sdk/api/model/AbstractOpenApiSchema.java
src/main/java/dev/openfga/sdk/api/model/Any.java
src/main/java/dev/openfga/sdk/api/model/Assertion.java
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v0.1.0

### [0.1.0](https://github.com/openfga/java-sdk/compare/v0.0.5...v0.1.0) (2023-09-27)

- [BREAKING] refactor(client): simplify OpenFgaClient and OpenFgaApi constructors to not require
an ApiClient. This is a breaking change as it changed the ordering of parameters in constructors.
- [BREAKING] refactor(client): all options classes for OpenFgaClient are now consistently prefixed
with "Client"
- chore(client): add ClientReadAssertionsOptions and ClientWriteAssertionsOptions for their
respective Client APIs.

## v0.0.5

### [0.0.5](https://github.com/openfga/java-sdk/compare/v0.0.4...v0.0.5) (2023-09-27)
Expand Down
44 changes: 21 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ It can be used with the following:
* Gradle (Groovy)

```groovy
implementation 'dev.openfga:openfga-sdk:0.0.5'
implementation 'dev.openfga:openfga-sdk:0.1.0'
```

* Gradle (Kotlin)

```kotlin
implementation("dev.openfga:openfga-sdk:0.0.5")
implementation("dev.openfga:openfga-sdk:0.1.0")
```

* Apache Maven
Expand All @@ -89,26 +89,26 @@ implementation("dev.openfga:openfga-sdk:0.0.5")
<dependency>
<groupId>dev.openfga</groupId>
<artifactId>openfga-sdk</artifactId>
<version>0.0.5</version>
<version>0.1.0</version>
</dependency>
```

* Ivy

```xml
<dependency org="dev.openfga" name="openfga-sdk" rev="0.0.5"/>
<dependency org="dev.openfga" name="openfga-sdk" rev="0.1.0"/>
```

* SBT

```scala
libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.0.5"
libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.1.0"
```

* Leiningen

```edn
[dev.openfga/openfga-sdk "0.0.5"]
[dev.openfga/openfga-sdk "0.1.0"]
```


Expand All @@ -122,20 +122,18 @@ libraryDependencies += "dev.openfga" % "openfga-sdk" % "0.0.5"

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.ApiClient;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
import java.net.http.HttpClient;

public class Example {
public static void main(String[] args) throws Exception {
var clientConfig = new ClientConfiguration()
var config = new ClientConfiguration()
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")); // Optional, can be overridden per request
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());

var fgaClient = new OpenFgaClient(apiClient, clientConfig);
var fgaClient = new OpenFgaClient(config);
var response = fgaClient.readAuthorizationModels().get();
}
}
Expand All @@ -145,7 +143,6 @@ public class Example {

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.ApiClient;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ApiToken;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
Expand All @@ -154,16 +151,15 @@ import java.net.http.HttpClient;

public class Example {
public static void main(String[] args) throws Exception {
var clientConfig = new ClientConfiguration()
var config = new ClientConfiguration()
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
.credentials(new Credentials(
new ApiToken(System.getenv("OPENFGA_API_TOKEN")) // will be passed as the "Authorization: Bearer ${ApiToken}" request header
));
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());

var fgaClient = new OpenFgaClient(apiClient, clientConfig);
var fgaClient = new OpenFgaClient(config);
var response = fgaClient.readAuthorizationModels().get();
}
}
Expand All @@ -173,7 +169,6 @@ public class Example {

```java
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.openfga.sdk.api.client.ApiClient;
import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
import dev.openfga.sdk.api.configuration.ClientCredentials;
Expand All @@ -182,7 +177,7 @@ import java.net.http.HttpClient;

public class Example {
public static void main(String[] args) throws Exception {
var clientConfig = new ClientConfiguration()
var config = new ClientConfiguration()
.apiUrl(System.getenv("OPENFGA_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("OPENFGA_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("OPENFGA_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
Expand All @@ -193,9 +188,8 @@ public class Example {
.clientId(System.getenv("OPENFGA_CLIENT_ID"))
.clientSecret(System.getenv("OPENFGA_CLIENT_SECRET"))
));
var apiClient = new ApiClient(HttpClient.newBuilder(), new ObjectMapper());

var fgaClient = new OpenFgaClient(apiClient, clientConfig);
var fgaClient = new OpenFgaClient(config);
var response = fgaClient.readAuthorizationModels().get();
}
}
Expand All @@ -219,7 +213,7 @@ Get a paginated list of stores.
[API Documentation](https://openfga.dev/api/service/docs/api#/Stores/ListStores)

```java
var options = new ListStoresOptions()
var options = new ClientListStoresOptions()
.pageSize(10)
.continuationToken("...");
var stores = fgaClient.listStores(options);
Expand Down Expand Up @@ -282,7 +276,7 @@ Read all authorization models in the store.
[API Documentation](https://openfga.dev/api/service#/Authorization%20Models/ReadAuthorizationModels)

```java
var options = new ReadAuthorizationModelsOptions()
var options = new ClientReadAuthorizationModelsOptions()
.pageSize(10)
.continuationToken("...");
var response = fgaClient.readAuthorizationModels(options).get();
Expand Down Expand Up @@ -345,7 +339,7 @@ Read a particular authorization model.
[API Documentation](https://openfga.dev/api/service#/Authorization%20Models/ReadAuthorizationModel)

```java
var options = new ReadAuthorizationModelOptions()
var options = new ClientReadAuthorizationModelOptions()
// You can rely on the model id set in the configuration or override it for this specific request
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");

Expand Down Expand Up @@ -572,7 +566,9 @@ Read assertions for a particular authorization model.
[API Documentation](https://openfga.dev/api/service#/Assertions/Read%20Assertions)

```java
var response = fgaClient.readAssertions().get();
var options = new ClientReadAssertionsOptions()
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
var response = fgaClient.readAssertions(options).get();
```

##### Write Assertions
Expand All @@ -582,14 +578,16 @@ Update the assertions for a particular authorization model.
[API Documentation](https://openfga.dev/api/service#/Assertions/Write%20Assertions)

```java
var options = new ClientWriteAssertionsOptions()
.authorizationModelId("01GXSA8YR785C4FYS3C0RTG7B1");
var assertions = List.of(
new ClientAssertion()
.user("user:81684243-9356-4421-8fbf-a4f8d36aa31b")
.relation("viewer")
._object("document:roadmap")
.expectation(true)
);
fgaClient.writeAssertions(assertions).get();
fgaClient.writeAssertions(assertions, options).get();
```


Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ plugins {
apply from: 'publish.gradle'

group = 'dev.openfga'
version = '0.0.5'
version = '0.1.0'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publishing {
pom {
group = 'dev.openfga'
name = 'openfga-sdk'
version = '0.0.5'
version = '0.1.0'
description = 'This is an autogenerated Java SDK for OpenFGA. It provides a wrapper around the [OpenFGA API definition](https://openfga.dev/api).'
url = 'https://openfga.dev'
licenses {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/dev/openfga/sdk/api/OpenFgaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ public class OpenFgaApi {
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;

public OpenFgaApi(ApiClient apiClient, Configuration configuration) throws FgaInvalidParameterException {
public OpenFgaApi(Configuration configuration) throws FgaInvalidParameterException {
this(configuration, new ApiClient());
}

public OpenFgaApi(Configuration configuration, ApiClient apiClient) throws FgaInvalidParameterException {
memberVarHttpClient = apiClient.getHttpClient();
memberVarObjectMapper = apiClient.getObjectMapper();
this.configuration = configuration;
Expand Down
58 changes: 47 additions & 11 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ public class OpenFgaClient {
private static final String CLIENT_METHOD_HEADER = "X-OpenFGA-Client-Method";
private static final int DEFAULT_MAX_METHOD_PARALLEL_REQS = 10;

public OpenFgaClient(ApiClient apiClient, ClientConfiguration configuration) throws FgaInvalidParameterException {
public OpenFgaClient(ClientConfiguration configuration) throws FgaInvalidParameterException {
this(configuration, new ApiClient());
}

public OpenFgaClient(ClientConfiguration configuration, ApiClient apiClient) throws FgaInvalidParameterException {
this.apiClient = apiClient;
this.configuration = configuration;
this.api = new OpenFgaApi(apiClient, configuration);
this.api = new OpenFgaApi(configuration, apiClient);
}

/* ***********
Expand All @@ -50,7 +54,7 @@ public void setAuthorizationModelId(String authorizationModelId) {

public void setConfiguration(ClientConfiguration configuration) throws FgaInvalidParameterException {
this.configuration = configuration;
this.api = new OpenFgaApi(apiClient, configuration);
this.api = new OpenFgaApi(configuration, apiClient);
}

/* ********
Expand All @@ -65,7 +69,7 @@ public CompletableFuture<ListStoresResponse> listStores() throws FgaInvalidParam
return call(() -> api.listStores(null, null));
}

public CompletableFuture<ListStoresResponse> listStores(ListStoresOptions options)
public CompletableFuture<ListStoresResponse> listStores(ClientListStoresOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
return call(() -> api.listStores(options.getPageSize(), options.getContinuationToken()));
Expand Down Expand Up @@ -121,7 +125,7 @@ public CompletableFuture<ReadAuthorizationModelsResponse> readAuthorizationModel
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAuthorizationModelsResponse> readAuthorizationModels(
ReadAuthorizationModelsOptions options) throws FgaInvalidParameterException {
ClientReadAuthorizationModelsOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();

Expand Down Expand Up @@ -171,7 +175,7 @@ public CompletableFuture<ReadAuthorizationModelResponse> readAuthorizationModel(
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID are null, empty, or whitespace
*/
public CompletableFuture<ReadAuthorizationModelResponse> readAuthorizationModel(
ReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
ClientReadAuthorizationModelOptions options) throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = options.getAuthorizationModelIdChecked();
Expand Down Expand Up @@ -201,7 +205,7 @@ public CompletableFuture<ReadAuthorizationModelResponse> readLatestAuthorization
*
* @throws FgaInvalidParameterException When the Store ID is null, empty, or whitespace
*/
public CompletableFuture<ReadChangesResponse> readChanges(ReadChangesOptions options)
public CompletableFuture<ReadChangesResponse> readChanges(ClientReadChangesOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
Expand Down Expand Up @@ -464,10 +468,26 @@ public CompletableFuture<ListObjectsResponse> listObjects(
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAssertionsResponse> readAssertions() throws FgaInvalidParameterException {
// TODO: Add version of this function that accepts ClientReadAssertionsOptions
return readAssertions(null);
}

/**
* ReadAssertions - Read assertions for a particular authorization model
*
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
public CompletableFuture<ReadAssertionsResponse> readAssertions(ClientReadAssertionsOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelIdChecked();

String authorizationModelId;
if (options != null && options.hasValidAuthorizationModelId()) {
authorizationModelId = options.getAuthorizationModelId();
} else {
authorizationModelId = configuration.getAuthorizationModelIdChecked();
}

return call(() -> api.readAssertions(storeId, authorizationModelId));
}

Expand All @@ -478,10 +498,26 @@ public CompletableFuture<ReadAssertionsResponse> readAssertions() throws FgaInva
*/
public CompletableFuture<Void> writeAssertions(List<ClientAssertion> assertions)
throws FgaInvalidParameterException {
// TODO: Add version of this function that accepts ClientWriteAssertionsOptions
return writeAssertions(assertions, null);
}

/**
* WriteAssertions - Updates assertions for a particular authorization model
*
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
public CompletableFuture<Void> writeAssertions(
List<ClientAssertion> assertions, ClientWriteAssertionsOptions options)
throws FgaInvalidParameterException {
configuration.assertValid();
String storeId = configuration.getStoreIdChecked();
String authorizationModelId = configuration.getAuthorizationModelIdChecked();

String authorizationModelId;
if (options != null && options.hasValidAuthorizationModelId()) {
authorizationModelId = options.getAuthorizationModelId();
} else {
authorizationModelId = configuration.getAuthorizationModelIdChecked();
}

WriteAssertionsRequest body = new WriteAssertionsRequest().assertions(ClientAssertion.asAssertions(assertions));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

package dev.openfga.sdk.api.configuration;

public class ListStoresOptions {
public class ClientListStoresOptions {
private Integer pageSize;
private String continuationToken;

public ListStoresOptions pageSize(Integer pageSize) {
public ClientListStoresOptions pageSize(Integer pageSize) {
this.pageSize = pageSize;
return this;
}
Expand All @@ -25,7 +25,7 @@ public Integer getPageSize() {
return pageSize;
}

public ListStoresOptions continuationToken(String continuationToken) {
public ClientListStoresOptions continuationToken(String continuationToken) {
this.continuationToken = continuationToken;
return this;
}
Expand Down
Loading

0 comments on commit 12af785

Please sign in to comment.