|
36 | 36 | import so.trophy.resources.points.requests.PointsSummaryRequest; |
37 | 37 | import so.trophy.types.ErrorBody; |
38 | 38 | import so.trophy.types.PointsBoost; |
| 39 | +import so.trophy.types.PointsLevel; |
| 40 | +import so.trophy.types.PointsLevelSummaryResponseItem; |
39 | 41 | import so.trophy.types.PointsRange; |
40 | 42 | import so.trophy.types.PointsSystemResponse; |
41 | 43 |
|
@@ -259,4 +261,137 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
259 | 261 | }); |
260 | 262 | return future; |
261 | 263 | } |
| 264 | + |
| 265 | + /** |
| 266 | + * Get all levels for a points system. |
| 267 | + */ |
| 268 | + public CompletableFuture<TrophyApiHttpResponse<List<PointsLevel>>> levels(String key) { |
| 269 | + return levels(key,null); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Get all levels for a points system. |
| 274 | + */ |
| 275 | + public CompletableFuture<TrophyApiHttpResponse<List<PointsLevel>>> levels(String key, |
| 276 | + RequestOptions requestOptions) { |
| 277 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getApiURL()).newBuilder() |
| 278 | + |
| 279 | + .addPathSegments("points") |
| 280 | + .addPathSegment(key) |
| 281 | + .addPathSegments("levels") |
| 282 | + .build(); |
| 283 | + Request okhttpRequest = new Request.Builder() |
| 284 | + .url(httpUrl) |
| 285 | + .method("GET", null) |
| 286 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 287 | + .addHeader("Accept", "application/json") |
| 288 | + .build(); |
| 289 | + OkHttpClient client = clientOptions.httpClient(); |
| 290 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 291 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 292 | + } |
| 293 | + CompletableFuture<TrophyApiHttpResponse<List<PointsLevel>>> future = new CompletableFuture<>(); |
| 294 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 295 | + @Override |
| 296 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 297 | + try (ResponseBody responseBody = response.body()) { |
| 298 | + if (response.isSuccessful()) { |
| 299 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), new TypeReference<List<PointsLevel>>() {}), response)); |
| 300 | + return; |
| 301 | + } |
| 302 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 303 | + try { |
| 304 | + switch (response.code()) { |
| 305 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 306 | + return; |
| 307 | + case 404:future.completeExceptionally(new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 308 | + return; |
| 309 | + } |
| 310 | + } |
| 311 | + catch (JsonProcessingException ignored) { |
| 312 | + // unable to map error response, throwing generic error |
| 313 | + } |
| 314 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 315 | + return; |
| 316 | + } |
| 317 | + catch (IOException e) { |
| 318 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + @Override |
| 323 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 324 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 325 | + } |
| 326 | + }); |
| 327 | + return future; |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * Get a breakdown of the number of users at each level in a points system. |
| 332 | + */ |
| 333 | + public CompletableFuture<TrophyApiHttpResponse<List<PointsLevelSummaryResponseItem>>> levelSummary( |
| 334 | + String key) { |
| 335 | + return levelSummary(key,null); |
| 336 | + } |
| 337 | + |
| 338 | + /** |
| 339 | + * Get a breakdown of the number of users at each level in a points system. |
| 340 | + */ |
| 341 | + public CompletableFuture<TrophyApiHttpResponse<List<PointsLevelSummaryResponseItem>>> levelSummary( |
| 342 | + String key, RequestOptions requestOptions) { |
| 343 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getApiURL()).newBuilder() |
| 344 | + |
| 345 | + .addPathSegments("points") |
| 346 | + .addPathSegment(key) |
| 347 | + .addPathSegments("level-summary") |
| 348 | + .build(); |
| 349 | + Request okhttpRequest = new Request.Builder() |
| 350 | + .url(httpUrl) |
| 351 | + .method("GET", null) |
| 352 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 353 | + .addHeader("Accept", "application/json") |
| 354 | + .build(); |
| 355 | + OkHttpClient client = clientOptions.httpClient(); |
| 356 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 357 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 358 | + } |
| 359 | + CompletableFuture<TrophyApiHttpResponse<List<PointsLevelSummaryResponseItem>>> future = new CompletableFuture<>(); |
| 360 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 361 | + @Override |
| 362 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 363 | + try (ResponseBody responseBody = response.body()) { |
| 364 | + if (response.isSuccessful()) { |
| 365 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), new TypeReference<List<PointsLevelSummaryResponseItem>>() {}), response)); |
| 366 | + return; |
| 367 | + } |
| 368 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 369 | + try { |
| 370 | + switch (response.code()) { |
| 371 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 372 | + return; |
| 373 | + case 404:future.completeExceptionally(new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 374 | + return; |
| 375 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 376 | + return; |
| 377 | + } |
| 378 | + } |
| 379 | + catch (JsonProcessingException ignored) { |
| 380 | + // unable to map error response, throwing generic error |
| 381 | + } |
| 382 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 383 | + return; |
| 384 | + } |
| 385 | + catch (IOException e) { |
| 386 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 387 | + } |
| 388 | + } |
| 389 | + |
| 390 | + @Override |
| 391 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 392 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 393 | + } |
| 394 | + }); |
| 395 | + return future; |
| 396 | + } |
262 | 397 | } |
0 commit comments