|
32 | 32 | import okhttp3.Response; |
33 | 33 | import okhttp3.ResponseBody; |
34 | 34 | import org.jetbrains.annotations.NotNull; |
| 35 | +import so.trophy.resources.admin.streaks.requests.ResetStreaksRequest; |
35 | 36 | import so.trophy.resources.admin.streaks.requests.RestoreStreaksRequest; |
| 37 | +import so.trophy.types.ResetStreaksResponse; |
36 | 38 | import so.trophy.types.RestoreStreaksResponse; |
37 | 39 |
|
38 | 40 | public class AsyncRawStreaksClient { |
@@ -122,4 +124,85 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
122 | 124 | }); |
123 | 125 | return future; |
124 | 126 | } |
125 | | - } |
| 127 | + |
| 128 | + /** |
| 129 | + * Reset the current streak to zero for multiple users. |
| 130 | + */ |
| 131 | + public CompletableFuture<TrophyApiHttpResponse<ResetStreaksResponse>> reset( |
| 132 | + ResetStreaksRequest request) { |
| 133 | + return reset(request,null); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Reset the current streak to zero for multiple users. |
| 138 | + */ |
| 139 | + public CompletableFuture<TrophyApiHttpResponse<ResetStreaksResponse>> reset( |
| 140 | + ResetStreaksRequest request, RequestOptions requestOptions) { |
| 141 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getAdminURL()).newBuilder() |
| 142 | + |
| 143 | + .addPathSegments("streaks/reset");if (requestOptions != null) { |
| 144 | + requestOptions.getQueryParameters().forEach((_key, _value) -> { |
| 145 | + httpUrl.addQueryParameter(_key, _value); |
| 146 | + } ); |
| 147 | + } |
| 148 | + RequestBody body; |
| 149 | + try { |
| 150 | + body = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); |
| 151 | + } |
| 152 | + catch(JsonProcessingException e) { |
| 153 | + throw new TrophyApiException("Failed to serialize request", e); |
| 154 | + } |
| 155 | + Request okhttpRequest = new Request.Builder() |
| 156 | + .url(httpUrl.build()) |
| 157 | + .method("POST", body) |
| 158 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 159 | + .addHeader("Content-Type", "application/json") |
| 160 | + .addHeader("Accept", "application/json") |
| 161 | + .build(); |
| 162 | + OkHttpClient client = clientOptions.httpClient(); |
| 163 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 164 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 165 | + } |
| 166 | + if (requestOptions != null && requestOptions.getMaxRetries().isPresent()) { |
| 167 | + okhttpRequest = okhttpRequest.newBuilder().tag(RetryInterceptor.MaxRetriesOverride.class, new RetryInterceptor.MaxRetriesOverride(requestOptions.getMaxRetries().get())).build(); |
| 168 | + } |
| 169 | + CompletableFuture<TrophyApiHttpResponse<ResetStreaksResponse>> future = new CompletableFuture<>(); |
| 170 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 171 | + @Override |
| 172 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 173 | + try (ResponseBody responseBody = response.body()) { |
| 174 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 175 | + if (response.isSuccessful()) { |
| 176 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ResetStreaksResponse.class), response)); |
| 177 | + return; |
| 178 | + } |
| 179 | + try { |
| 180 | + switch (response.code()) { |
| 181 | + case 400:future.completeExceptionally(new BadRequestError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 182 | + return; |
| 183 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 184 | + return; |
| 185 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 186 | + return; |
| 187 | + } |
| 188 | + } |
| 189 | + catch (JsonProcessingException ignored) { |
| 190 | + // unable to map error response, throwing generic error |
| 191 | + } |
| 192 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 193 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), errorBody, response)); |
| 194 | + return; |
| 195 | + } |
| 196 | + catch (IOException e) { |
| 197 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + @Override |
| 202 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 203 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 204 | + } |
| 205 | + }); |
| 206 | + return future; |
| 207 | + } |
| 208 | + } |
0 commit comments