-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce error types and retries
- Loading branch information
1 parent
12af785
commit 479b82c
Showing
17 changed files
with
582 additions
and
451 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
src/main/java/dev/openfga/sdk/api/client/HttpRequestAttempt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package dev.openfga.sdk.api.client; | ||
|
||
import static dev.openfga.sdk.util.StringUtil.isNullOrWhitespace; | ||
|
||
import dev.openfga.sdk.api.configuration.BaseConfiguration; | ||
import dev.openfga.sdk.errors.*; | ||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.concurrent.*; | ||
|
||
public class HttpRequestAttempt<T> { | ||
private final ApiClient apiClient; | ||
private final BaseConfiguration configuration; | ||
private final Class<T> clazz; | ||
private final String name; | ||
private final HttpRequest request; | ||
|
||
public HttpRequestAttempt( | ||
HttpRequest request, String name, Class<T> clazz, ApiClient apiClient, BaseConfiguration configuration) | ||
throws FgaInvalidParameterException { | ||
if (configuration.getMaxRetries() == null) { | ||
throw new FgaInvalidParameterException("maxRetries", "Configuration"); | ||
} | ||
this.apiClient = apiClient; | ||
this.configuration = configuration; | ||
this.name = name; | ||
this.request = request; | ||
this.clazz = clazz; | ||
} | ||
|
||
public CompletableFuture<T> attemptHttpRequest() throws ApiException { | ||
int retryNumber = 0; | ||
return attemptHttpRequest(apiClient.getHttpClient(), retryNumber); | ||
} | ||
|
||
private CompletableFuture<T> attemptHttpRequest(HttpClient httpClient, int retryNumber) { | ||
return httpClient | ||
.sendAsync(request, HttpResponse.BodyHandlers.ofString()) | ||
.thenCompose(response -> { | ||
int status = response.statusCode(); | ||
String responseBody = response.body(); | ||
|
||
try { | ||
checkStatus(name, response); | ||
} catch (FgaApiRateLimitExceededError | FgaApiInternalError e) { | ||
if (retryNumber < configuration.getMaxRetries()) { | ||
HttpClient delayingClient = getDelayedHttpClient(); | ||
return attemptHttpRequest(delayingClient, retryNumber + 1); | ||
} | ||
return CompletableFuture.failedFuture(e); | ||
} catch (ApiException e) { | ||
return CompletableFuture.failedFuture(e); | ||
} | ||
|
||
if (status != HttpURLConnection.HTTP_OK | ||
&& status != HttpURLConnection.HTTP_CREATED | ||
&& status != HttpURLConnection.HTTP_NO_CONTENT) { | ||
// An HTTP failure that is not modeled in checkStatus(..., ...) below. | ||
return CompletableFuture.failedFuture(new ApiException(name, response)); | ||
} | ||
|
||
if (clazz == Void.class && isNullOrWhitespace(responseBody)) { | ||
return CompletableFuture.completedFuture(null); | ||
} | ||
|
||
try { | ||
T body = apiClient.getObjectMapper().readValue(responseBody, clazz); | ||
return CompletableFuture.completedFuture(body); | ||
} catch (IOException e) { | ||
// Malformed response. | ||
return CompletableFuture.failedFuture(new ApiException(e)); | ||
} | ||
}); | ||
} | ||
|
||
private HttpClient getDelayedHttpClient() { | ||
Duration retryDelay = configuration.getMinimumRetryDelay(); | ||
return apiClient | ||
.getHttpClientBuilder() | ||
.executor(CompletableFuture.delayedExecutor(retryDelay.toNanos(), TimeUnit.NANOSECONDS)) | ||
.build(); | ||
} | ||
|
||
private static void checkStatus(String name, HttpResponse<String> response) | ||
throws FgaApiValidationError, FgaApiAuthenticationError, FgaApiNotFoundError, FgaApiRateLimitExceededError, | ||
FgaApiInternalError { | ||
|
||
int status = response.statusCode(); | ||
String body = response.body(); | ||
|
||
switch (status) { | ||
case HttpURLConnection.HTTP_BAD_REQUEST: | ||
case 422: // HTTP 422 Unprocessable Entity | ||
throw new FgaApiValidationError(name, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_UNAUTHORIZED: | ||
case HttpURLConnection.HTTP_FORBIDDEN: | ||
throw new FgaApiAuthenticationError(name, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_NOT_FOUND: | ||
throw new FgaApiNotFoundError(name, status, response.headers(), body); | ||
|
||
case 429: // HTTP 429 Too Many Requests | ||
throw new FgaApiRateLimitExceededError(name, status, response.headers(), body); | ||
|
||
case HttpURLConnection.HTTP_INTERNAL_ERROR: | ||
throw new FgaApiInternalError(name, status, response.headers(), body); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiAuthenticationError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiAuthenticationError extends ApiException { | ||
public FgaApiAuthenticationError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiAuthenticationError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiInternalError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiInternalError extends ApiException { | ||
public FgaApiInternalError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiInternalError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiNotFoundError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiNotFoundError extends ApiException { | ||
public FgaApiNotFoundError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiNotFoundError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/openfga/sdk/errors/FgaApiRateLimitExceededError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.openfga.sdk.errors; | ||
|
||
import java.net.http.HttpHeaders; | ||
|
||
public class FgaApiRateLimitExceededError extends ApiException { | ||
public FgaApiRateLimitExceededError( | ||
String message, Throwable throwable, int code, HttpHeaders responseHeaders, String responseBody) { | ||
super(message, code, responseHeaders, responseBody); | ||
} | ||
|
||
public FgaApiRateLimitExceededError(String message, int code, HttpHeaders responseHeaders, String responseBody) { | ||
this(message, (Throwable) null, code, responseHeaders, responseBody); | ||
} | ||
} |
Oops, something went wrong.