Skip to content

Commit

Permalink
refactor: simplify constructors, make client options consistent, bump…
Browse files Browse the repository at this point in the history
… to 0.1.0 (#215)
  • Loading branch information
rhamzeh authored Sep 29, 2023
2 parents 303994a + 8bca8ae commit 230c669
Show file tree
Hide file tree
Showing 16 changed files with 198 additions and 93 deletions.
11 changes: 11 additions & 0 deletions config/clients/java/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## v0.1.0

### [0.1.0](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/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://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.0.4...v0.0.5) (2023-09-27)
Expand Down
58 changes: 33 additions & 25 deletions config/clients/java/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"gitRepoId": "java-sdk",
"artifactId": "openfga-sdk",
"groupId": "dev.openfga",
"packageVersion": "0.0.5",
"packageVersion": "0.1.0",
"apiPackage": "dev.openfga.sdk.api",
"authPackage": "dev.openfga.sdk.api.auth",
"clientPackage": "dev.openfga.sdk.api.client",
Expand Down Expand Up @@ -131,10 +131,42 @@
"destinationFilename": "src/test/java/dev/openfga/sdk/api/configuration/ClientCredentialsTest.java",
"templateType": "SupportingFiles"
},
"config-ClientExpandOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientExpandOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientListObjectsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientListObjectsOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientListStoresOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientListStoresOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadAssertionsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadAssertionsOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadAuthorizationModelOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadAuthorizationModelsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadAuthorizationModelsOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadChangesOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadChangesOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientReadOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientReadOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientWriteAssertionsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientWriteAssertionsOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientWriteOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientWriteOptions.java",
"templateType": "SupportingFiles"
Expand All @@ -159,30 +191,6 @@
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/CredentialsMethod.java",
"templateType": "SupportingFiles"
},
"config-ClientExpandOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientExpandOptions.java",
"templateType": "SupportingFiles"
},
"config-ClientListObjectsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ClientListObjectsOptions.java",
"templateType": "SupportingFiles"
},
"config-ListStoresOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ListStoresOptions.java",
"templateType": "SupportingFiles"
},
"config-ReadAuthorizationModelOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelOptions.java",
"templateType": "SupportingFiles"
},
"config-ReadAuthorizationModelsOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ReadAuthorizationModelsOptions.java",
"templateType": "SupportingFiles"
},
"config-ReadChangesOptions.java.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/api/configuration/ReadChangesOptions.java",
"templateType": "SupportingFiles"
},
"Pair.mustache" : {
"destinationFilename": "src/main/java/dev/openfga/sdk/util/Pair.java",
"templateType": "SupportingFiles"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public class OpenFgaApiIntegrationTest {
@BeforeEach
public void initializeApi() throws Exception {
Configuration apiConfig = new Configuration().apiUrl("http://localhost:8080");
ApiClient apiClient = new ApiClient(HttpClient.newBuilder(), mapper);
api = new OpenFgaApi(apiClient, apiConfig);
api = new OpenFgaApi(apiConfig);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion config/clients/java/template/OpenFgaApiTest.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class OpenFgaApiTest {
when(mockApiClient.getObjectMapper()).thenReturn(mapper);
when(mockApiClient.getHttpClient()).thenReturn(mockHttpClient);
fga = new OpenFgaApi(mockApiClient, mockConfiguration);
fga = new OpenFgaApi(mockConfiguration, mockApiClient);
}

/**
Expand Down
14 changes: 9 additions & 5 deletions config/clients/java/template/README_calling_api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Get a paginated list of stores.
[API Documentation]({{apiDocsUrl}}/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 @@ -70,7 +70,7 @@ Read all authorization models in the store.
[API Documentation]({{apiDocsUrl}}#/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 @@ -133,7 +133,7 @@ Read a particular authorization model.
[API Documentation]({{apiDocsUrl}}#/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 @@ -360,7 +360,9 @@ Read assertions for a particular authorization model.
[API Documentation]({{apiDocsUrl}}#/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 @@ -370,12 +372,14 @@ Update the assertions for a particular authorization model.
[API Documentation]({{apiDocsUrl}}#/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();
```
18 changes: 6 additions & 12 deletions config/clients/java/template/README_initializing.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

```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("{{appUpperCaseName}}_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("{{appUpperCaseName}}_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("{{appUpperCaseName}}_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 @@ -25,7 +23,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 @@ -34,16 +31,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("{{appUpperCaseName}}_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("{{appUpperCaseName}}_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("{{appUpperCaseName}}_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
.credentials(new Credentials(
new ApiToken(System.getenv("{{appUpperCaseName}}_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 @@ -53,7 +49,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 @@ -62,7 +57,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("{{appUpperCaseName}}_API_URL")) // If not specified, will default to "https://localhost:8080"
.storeId(System.getenv("{{appUpperCaseName}}_STORE_ID")) // Not required when calling createStore() or listStores()
.authorizationModelId(System.getenv("{{appUpperCaseName}}_AUTHORIZATION_MODEL_ID")) // Optional, can be overridden per request
Expand All @@ -73,9 +68,8 @@ public class Example {
.clientId(System.getenv("{{appUpperCaseName}}_CLIENT_ID"))
.clientSecret(System.getenv("{{appUpperCaseName}}_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 Down
55 changes: 44 additions & 11 deletions config/clients/java/template/client-OpenFgaClient.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public class OpenFgaClient {
private static final String CLIENT_METHOD_HEADER = "{{clientMethodHeader}}";
private static final int DEFAULT_MAX_METHOD_PARALLEL_REQS = {{clientMaxMethodParallelRequests}};

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 @@ -39,7 +43,7 @@ public class OpenFgaClient {

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

/* ********
Expand All @@ -54,7 +58,7 @@ public class OpenFgaClient {
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 @@ -110,7 +114,7 @@ public class OpenFgaClient {
* @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 @@ -160,7 +164,7 @@ public class OpenFgaClient {
* @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 @@ -190,7 +194,7 @@ public class OpenFgaClient {
*
* @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 @@ -451,10 +455,25 @@ public class OpenFgaClient {
* @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 @@ -464,10 +483,24 @@ public class OpenFgaClient {
* @throws FgaInvalidParameterException When either the Store ID or Authorization Model ID is null, empty, or whitespace
*/
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 @@ -32,8 +32,7 @@ public class OpenFgaClientIntegrationTest {
@BeforeEach
public void initializeApi() throws Exception {
ClientConfiguration apiConfig = new ClientConfiguration().apiUrl("http://localhost:8080");
ApiClient apiClient = new ApiClient(HttpClient.newBuilder(), mapper);
fga = new OpenFgaClient(apiClient, apiConfig);
fga = new OpenFgaClient(apiConfig);
}

@Test
Expand Down Expand Up @@ -134,7 +133,7 @@ public class OpenFgaClientIntegrationTest {
fga.setStoreId(storeId);
String authModelId = writeAuthModel(storeId);
fga.setAuthorizationModelId(authModelId);
var options = new ReadAuthorizationModelsOptions();
var options = new ClientReadAuthorizationModelsOptions();
// When
ReadAuthorizationModelsResponse response =
Expand Down
Loading

0 comments on commit 230c669

Please sign in to comment.