Skip to content

Commit

Permalink
refactor: abstract parameter assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
booniepepper committed Dec 15, 2023
1 parent 081af76 commit ad17882
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 63 deletions.
1 change: 1 addition & 0 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ src/main/java/dev/openfga/sdk/errors/FgaInvalidParameterException.java
src/main/java/dev/openfga/sdk/errors/HttpStatusCode.java
src/main/java/dev/openfga/sdk/util/Pair.java
src/main/java/dev/openfga/sdk/util/StringUtil.java
src/main/java/dev/openfga/sdk/util/Validation.java
src/test-integration/java/dev/openfga/sdk/api/OpenFgaApiIntegrationTest.java
src/test-integration/java/dev/openfga/sdk/api/client/OpenFgaClientIntegrationTest.java
src/test-integration/java/package-info.java
Expand Down
55 changes: 25 additions & 30 deletions src/main/java/dev/openfga/sdk/api/OpenFgaApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package dev.openfga.sdk.api;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;
import static dev.openfga.sdk.util.Validation.assertParamExists;

import dev.openfga.sdk.api.auth.*;
import dev.openfga.sdk.api.client.*;
Expand Down Expand Up @@ -110,9 +111,9 @@ private CompletableFuture<ApiResponse<CheckResponse>> check(
String storeId, CheckRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "check");
assertParamExists(storeId, "storeId", "check");

validate(body, "body", "check");
assertParamExists(body, "body", "check");

String path = "/stores/{store_id}/check".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -154,7 +155,7 @@ public CompletableFuture<ApiResponse<CreateStoreResponse>> createStore(
private CompletableFuture<ApiResponse<CreateStoreResponse>> createStore(
CreateStoreRequest body, Configuration configuration) throws ApiException, FgaInvalidParameterException {

validate(body, "body", "createStore");
assertParamExists(body, "body", "createStore");

String path = "/stores";

Expand Down Expand Up @@ -195,7 +196,7 @@ public CompletableFuture<ApiResponse<Void>> deleteStore(String storeId, Configur
private CompletableFuture<ApiResponse<Void>> deleteStore(String storeId, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "deleteStore");
assertParamExists(storeId, "storeId", "deleteStore");

String path = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -240,9 +241,9 @@ private CompletableFuture<ApiResponse<ExpandResponse>> expand(
String storeId, ExpandRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "expand");
assertParamExists(storeId, "storeId", "expand");

validate(body, "body", "expand");
assertParamExists(body, "body", "expand");

String path = "/stores/{store_id}/expand".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -284,7 +285,7 @@ public CompletableFuture<ApiResponse<GetStoreResponse>> getStore(
private CompletableFuture<ApiResponse<GetStoreResponse>> getStore(String storeId, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "getStore");
assertParamExists(storeId, "storeId", "getStore");

String path = "/stores/{store_id}".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -329,9 +330,9 @@ private CompletableFuture<ApiResponse<ListObjectsResponse>> listObjects(
String storeId, ListObjectsRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "listObjects");
assertParamExists(storeId, "storeId", "listObjects");

validate(body, "body", "listObjects");
assertParamExists(body, "body", "listObjects");

String path = "/stores/{store_id}/list-objects".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -420,9 +421,9 @@ private CompletableFuture<ApiResponse<ReadResponse>> read(
String storeId, ReadRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "read");
assertParamExists(storeId, "storeId", "read");

validate(body, "body", "read");
assertParamExists(body, "body", "read");

String path = "/stores/{store_id}/read".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -467,9 +468,9 @@ private CompletableFuture<ApiResponse<ReadAssertionsResponse>> readAssertions(
String storeId, String authorizationModelId, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "readAssertions");
assertParamExists(storeId, "storeId", "readAssertions");

validate(authorizationModelId, "authorizationModelId", "readAssertions");
assertParamExists(authorizationModelId, "authorizationModelId", "readAssertions");

String path = "/stores/{store_id}/assertions/{authorization_model_id}"
.replace("{store_id}", ApiClient.urlEncode(storeId.toString()))
Expand Down Expand Up @@ -516,9 +517,9 @@ public CompletableFuture<ApiResponse<ReadAuthorizationModelResponse>> readAuthor
private CompletableFuture<ApiResponse<ReadAuthorizationModelResponse>> readAuthorizationModel(
String storeId, String id, Configuration configuration) throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "readAuthorizationModel");
assertParamExists(storeId, "storeId", "readAuthorizationModel");

validate(id, "id", "readAuthorizationModel");
assertParamExists(id, "id", "readAuthorizationModel");

String path = "/stores/{store_id}/authorization-models/{id}"
.replace("{store_id}", ApiClient.urlEncode(storeId.toString()))
Expand Down Expand Up @@ -574,7 +575,7 @@ private CompletableFuture<ApiResponse<ReadAuthorizationModelsResponse>> readAuth
String storeId, Integer pageSize, String continuationToken, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "readAuthorizationModels");
assertParamExists(storeId, "storeId", "readAuthorizationModels");

String path = "/stores/{store_id}/authorization-models"
.replace("{store_id}", ApiClient.urlEncode(storeId.toString()));
Expand Down Expand Up @@ -636,7 +637,7 @@ private CompletableFuture<ApiResponse<ReadChangesResponse>> readChanges(
String storeId, String type, Integer pageSize, String continuationToken, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "readChanges");
assertParamExists(storeId, "storeId", "readChanges");

String path = "/stores/{store_id}/changes".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));
path = pathWithParams(path, "type", type, "page_size", pageSize, "continuation_token", continuationToken);
Expand Down Expand Up @@ -681,9 +682,9 @@ public CompletableFuture<ApiResponse<Object>> write(
private CompletableFuture<ApiResponse<Object>> write(String storeId, WriteRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "write");
assertParamExists(storeId, "storeId", "write");

validate(body, "body", "write");
assertParamExists(body, "body", "write");

String path = "/stores/{store_id}/write".replace("{store_id}", ApiClient.urlEncode(storeId.toString()));

Expand Down Expand Up @@ -734,11 +735,11 @@ private CompletableFuture<ApiResponse<Void>> writeAssertions(
String storeId, String authorizationModelId, WriteAssertionsRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "writeAssertions");
assertParamExists(storeId, "storeId", "writeAssertions");

validate(authorizationModelId, "authorizationModelId", "writeAssertions");
assertParamExists(authorizationModelId, "authorizationModelId", "writeAssertions");

validate(body, "body", "writeAssertions");
assertParamExists(body, "body", "writeAssertions");

String path = "/stores/{store_id}/assertions/{authorization_model_id}"
.replace("{store_id}", ApiClient.urlEncode(storeId.toString()))
Expand Down Expand Up @@ -785,9 +786,9 @@ private CompletableFuture<ApiResponse<WriteAuthorizationModelResponse>> writeAut
String storeId, WriteAuthorizationModelRequest body, Configuration configuration)
throws ApiException, FgaInvalidParameterException {

validate(storeId, "storeId", "writeAuthorizationModel");
assertParamExists(storeId, "storeId", "writeAuthorizationModel");

validate(body, "body", "writeAuthorizationModel");
assertParamExists(body, "body", "writeAuthorizationModel");

String path = "/stores/{store_id}/authorization-models"
.replace("{store_id}", ApiClient.urlEncode(storeId.toString()));
Expand Down Expand Up @@ -879,12 +880,6 @@ private String pathWithParams(String basePath, Object... params) {
return path.toString();
}

private void validate(Object obj, String name, String context) throws FgaInvalidParameterException {
if (obj == null || obj instanceof String && isNullOrWhitespace((String) obj)) {
throw new FgaInvalidParameterException(name, context);
}
}

/**
* Get an access token. Expects that configuration is valid (meaning it can
* pass {@link Configuration#assertValid()}) and expects that if the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.openfga.sdk.api.client;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;
import static dev.openfga.sdk.util.Validation.assertParamExists;

import dev.openfga.sdk.api.configuration.Configuration;
import dev.openfga.sdk.errors.*;
Expand Down Expand Up @@ -28,9 +29,7 @@ public class HttpRequestAttempt<T> {
public HttpRequestAttempt(
HttpRequest request, String name, Class<T> clazz, ApiClient apiClient, Configuration configuration)
throws FgaInvalidParameterException {
if (configuration.getMaxRetries() == null) {
throw new FgaInvalidParameterException("maxRetries", "Configuration");
}
assertParamExists(configuration.getMaxRetries(), "maxRetries", "Configuration");
this.apiClient = apiClient;
this.configuration = configuration;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package dev.openfga.sdk.api.configuration;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;
import static dev.openfga.sdk.util.Validation.assertParamExists;

import dev.openfga.sdk.errors.FgaInvalidParameterException;
import java.time.Duration;
Expand All @@ -22,15 +22,11 @@ public class ClientConfiguration extends Configuration {
private String authorizationModelId;

public void assertValidStoreId() throws FgaInvalidParameterException {
if (isNullOrWhitespace(storeId)) {
throw new FgaInvalidParameterException("storeId", "ClientConfiguration");
}
assertParamExists(storeId, "storeId", "ClientConfiguration");
}

public void assertValidAuthorizationModelId() throws FgaInvalidParameterException {
if (isNullOrWhitespace(authorizationModelId)) {
throw new FgaInvalidParameterException("authorizationModelId", "ClientConfiguration");
}
assertParamExists(authorizationModelId, "authorizationModelId", "ClientConfiguration");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

package dev.openfga.sdk.api.configuration;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;
import static dev.openfga.sdk.util.Validation.assertParamExists;

import dev.openfga.sdk.errors.FgaInvalidParameterException;

Expand All @@ -30,21 +30,10 @@ public ClientCredentials clientId(String clientId) {
}

public void assertValid() throws FgaInvalidParameterException {
if (isNullOrWhitespace(clientId)) {
throw new FgaInvalidParameterException("clientId", "ClientCredentials");
}

if (isNullOrWhitespace(clientSecret)) {
throw new FgaInvalidParameterException("clientSecret", "ClientCredentials");
}

if (isNullOrWhitespace(apiTokenIssuer)) {
throw new FgaInvalidParameterException("apiTokenIssuer", "ClientCredentials");
}

if (isNullOrWhitespace(apiAudience)) {
throw new FgaInvalidParameterException("apiAudience", "ClientCredentials");
}
assertParamExists(clientId, "clientId", "ClientCredentials");
assertParamExists(clientSecret, "clientSecret", "ClientCredentials");
assertParamExists(apiTokenIssuer, "apiTokenIssuer", "ClientCredentials");
assertParamExists(apiAudience, "apiAudience", "ClientCredentials");
}

public String getClientId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package dev.openfga.sdk.api.configuration;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;
import static dev.openfga.sdk.util.Validation.assertParamExists;

import dev.openfga.sdk.errors.FgaInvalidParameterException;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -68,13 +69,8 @@ public void assertValid() throws FgaInvalidParameterException {
throw new FgaInvalidParameterException("apiUrl", "Configuration", cause);
}

if (isNullOrWhitespace(uri.getScheme())) {
throw new FgaInvalidParameterException("scheme", "Configuration");
}

if (isNullOrWhitespace(uri.getHost())) {
throw new FgaInvalidParameterException("hostname", "Configuration");
}
assertParamExists(uri.getScheme(), "scheme", "Configuration");
assertParamExists(uri.getHost(), "hostname", "Configuration");
}

if (credentials != null) {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/dev/openfga/sdk/util/Validation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* OpenFGA
* A high performance and flexible authorization/permission engine built for developers and inspired by Google Zanzibar.
*
* The version of the OpenAPI document: 0.1
* Contact: [email protected]
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

package dev.openfga.sdk.util;

import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace;

import dev.openfga.sdk.errors.FgaInvalidParameterException;

public class Validation {
private Validation() {} // Instantiation prevented for utility class.

public static void assertParamExists(Object obj, String name, String context) throws FgaInvalidParameterException {
if (obj == null || obj instanceof String && isNullOrWhitespace((String) obj)) {
throw new FgaInvalidParameterException(name, context);
}
}
}

0 comments on commit ad17882

Please sign in to comment.