|
| 1 | +package so.trophy.src.main.java.resources.admin.environments; |
| 2 | + |
| 3 | +/** |
| 4 | + * This file was auto-generated by Fern from our API Definition. |
| 5 | + */ |
| 6 | + |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 9 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 10 | +import so.trophy.core.ClientOptions; |
| 11 | +import so.trophy.core.ObjectMappers; |
| 12 | +import so.trophy.core.RequestOptions; |
| 13 | +import so.trophy.core.RetryInterceptor; |
| 14 | +import so.trophy.core.TrophyApiApiException; |
| 15 | +import so.trophy.core.TrophyApiException; |
| 16 | +import so.trophy.core.TrophyApiHttpResponse; |
| 17 | +import so.trophy.errors.UnauthorizedError; |
| 18 | +import so.trophy.errors.UnprocessableEntityError; |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.Object; |
| 21 | +import java.lang.Override; |
| 22 | +import java.lang.String; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import okhttp3.Call; |
| 26 | +import okhttp3.Callback; |
| 27 | +import okhttp3.Headers; |
| 28 | +import okhttp3.HttpUrl; |
| 29 | +import okhttp3.OkHttpClient; |
| 30 | +import okhttp3.Request; |
| 31 | +import okhttp3.Response; |
| 32 | +import okhttp3.ResponseBody; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | +import so.trophy.types.AdminEnvironment; |
| 35 | + |
| 36 | +public class AsyncRawEnvironmentsClient { |
| 37 | + protected final ClientOptions clientOptions; |
| 38 | + |
| 39 | + public AsyncRawEnvironmentsClient(ClientOptions clientOptions) { |
| 40 | + this.clientOptions = clientOptions; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * List active environments. |
| 45 | + */ |
| 46 | + public CompletableFuture<TrophyApiHttpResponse<List<AdminEnvironment>>> list() { |
| 47 | + return list(null); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * List active environments. |
| 52 | + */ |
| 53 | + public CompletableFuture<TrophyApiHttpResponse<List<AdminEnvironment>>> list( |
| 54 | + RequestOptions requestOptions) { |
| 55 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getAdminURL()).newBuilder() |
| 56 | + |
| 57 | + .addPathSegments("environments");if (requestOptions != null) { |
| 58 | + requestOptions.getQueryParameters().forEach((_key, _value) -> { |
| 59 | + httpUrl.addQueryParameter(_key, _value); |
| 60 | + } ); |
| 61 | + } |
| 62 | + Request okhttpRequest = new Request.Builder() |
| 63 | + .url(httpUrl.build()) |
| 64 | + .method("GET", null) |
| 65 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 66 | + .addHeader("Accept", "application/json") |
| 67 | + .build(); |
| 68 | + OkHttpClient client = clientOptions.httpClient(); |
| 69 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 70 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 71 | + } |
| 72 | + if (requestOptions != null && requestOptions.getMaxRetries().isPresent()) { |
| 73 | + okhttpRequest = okhttpRequest.newBuilder().tag(RetryInterceptor.MaxRetriesOverride.class, new RetryInterceptor.MaxRetriesOverride(requestOptions.getMaxRetries().get())).build(); |
| 74 | + } |
| 75 | + CompletableFuture<TrophyApiHttpResponse<List<AdminEnvironment>>> future = new CompletableFuture<>(); |
| 76 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 77 | + @Override |
| 78 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 79 | + try (ResponseBody responseBody = response.body()) { |
| 80 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 81 | + if (response.isSuccessful()) { |
| 82 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, new TypeReference<List<AdminEnvironment>>() {}), response)); |
| 83 | + return; |
| 84 | + } |
| 85 | + try { |
| 86 | + switch (response.code()) { |
| 87 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 88 | + return; |
| 89 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 90 | + return; |
| 91 | + } |
| 92 | + } |
| 93 | + catch (JsonProcessingException ignored) { |
| 94 | + // unable to map error response, throwing generic error |
| 95 | + } |
| 96 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 97 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), errorBody, response)); |
| 98 | + return; |
| 99 | + } |
| 100 | + catch (IOException e) { |
| 101 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 107 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 108 | + } |
| 109 | + }); |
| 110 | + return future; |
| 111 | + } |
| 112 | + } |
0 commit comments