|
| 1 | +package so.trophy.resources.admin.streaks; |
| 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 so.trophy.core.ClientOptions; |
| 10 | +import so.trophy.core.MediaTypes; |
| 11 | +import so.trophy.core.ObjectMappers; |
| 12 | +import so.trophy.core.RequestOptions; |
| 13 | +import so.trophy.core.TrophyApiApiException; |
| 14 | +import so.trophy.core.TrophyApiException; |
| 15 | +import so.trophy.core.TrophyApiHttpResponse; |
| 16 | +import so.trophy.errors.BadRequestError; |
| 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.concurrent.CompletableFuture; |
| 24 | +import okhttp3.Call; |
| 25 | +import okhttp3.Callback; |
| 26 | +import okhttp3.Headers; |
| 27 | +import okhttp3.HttpUrl; |
| 28 | +import okhttp3.OkHttpClient; |
| 29 | +import okhttp3.Request; |
| 30 | +import okhttp3.RequestBody; |
| 31 | +import okhttp3.Response; |
| 32 | +import okhttp3.ResponseBody; |
| 33 | +import org.jetbrains.annotations.NotNull; |
| 34 | +import so.trophy.resources.admin.streaks.requests.RestoreStreaksRequest; |
| 35 | +import so.trophy.types.ErrorBody; |
| 36 | +import so.trophy.types.RestoreStreaksResponse; |
| 37 | + |
| 38 | +public class AsyncRawStreaksClient { |
| 39 | + protected final ClientOptions clientOptions; |
| 40 | + |
| 41 | + public AsyncRawStreaksClient(ClientOptions clientOptions) { |
| 42 | + this.clientOptions = clientOptions; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Restore streaks for multiple users to the maximum length in the last 90 days (in the case of daily streaks), one year (in the case of weekly streaks), or two years (in the case of monthly streaks). |
| 47 | + */ |
| 48 | + public CompletableFuture<TrophyApiHttpResponse<RestoreStreaksResponse>> restore( |
| 49 | + RestoreStreaksRequest request) { |
| 50 | + return restore(request,null); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Restore streaks for multiple users to the maximum length in the last 90 days (in the case of daily streaks), one year (in the case of weekly streaks), or two years (in the case of monthly streaks). |
| 55 | + */ |
| 56 | + public CompletableFuture<TrophyApiHttpResponse<RestoreStreaksResponse>> restore( |
| 57 | + RestoreStreaksRequest request, RequestOptions requestOptions) { |
| 58 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getAdminURL()).newBuilder() |
| 59 | + |
| 60 | + .addPathSegments("streaks/restore") |
| 61 | + .build(); |
| 62 | + RequestBody body; |
| 63 | + try { |
| 64 | + body = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); |
| 65 | + } |
| 66 | + catch(JsonProcessingException e) { |
| 67 | + throw new TrophyApiException("Failed to serialize request", e); |
| 68 | + } |
| 69 | + Request okhttpRequest = new Request.Builder() |
| 70 | + .url(httpUrl) |
| 71 | + .method("POST", body) |
| 72 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 73 | + .addHeader("Content-Type", "application/json") |
| 74 | + .addHeader("Accept", "application/json") |
| 75 | + .build(); |
| 76 | + OkHttpClient client = clientOptions.httpClient(); |
| 77 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 78 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 79 | + } |
| 80 | + CompletableFuture<TrophyApiHttpResponse<RestoreStreaksResponse>> future = new CompletableFuture<>(); |
| 81 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 82 | + @Override |
| 83 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 84 | + try (ResponseBody responseBody = response.body()) { |
| 85 | + if (response.isSuccessful()) { |
| 86 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), RestoreStreaksResponse.class), response)); |
| 87 | + return; |
| 88 | + } |
| 89 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 90 | + try { |
| 91 | + switch (response.code()) { |
| 92 | + case 400:future.completeExceptionally(new BadRequestError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 93 | + return; |
| 94 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 95 | + return; |
| 96 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 97 | + return; |
| 98 | + } |
| 99 | + } |
| 100 | + catch (JsonProcessingException ignored) { |
| 101 | + // unable to map error response, throwing generic error |
| 102 | + } |
| 103 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 104 | + return; |
| 105 | + } |
| 106 | + catch (IOException e) { |
| 107 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 113 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 114 | + } |
| 115 | + }); |
| 116 | + return future; |
| 117 | + } |
| 118 | +} |
0 commit comments