|
35 | 35 | import okhttp3.Response; |
36 | 36 | import okhttp3.ResponseBody; |
37 | 37 | import org.jetbrains.annotations.NotNull; |
| 38 | +import so.trophy.resources.users.requests.UpdateUserPreferencesRequest; |
38 | 39 | import so.trophy.resources.users.requests.UsersAchievementsRequest; |
39 | 40 | import so.trophy.resources.users.requests.UsersLeaderboardRequest; |
40 | 41 | import so.trophy.resources.users.requests.UsersMetricEventSummaryRequest; |
|
53 | 54 | import so.trophy.types.User; |
54 | 55 | import so.trophy.types.UserAchievementWithStatsResponse; |
55 | 56 | import so.trophy.types.UserLeaderboardResponseWithHistory; |
| 57 | +import so.trophy.types.UserPreferencesResponse; |
56 | 58 | import so.trophy.types.WrappedResponse; |
57 | 59 |
|
58 | 60 | public class AsyncRawUsersClient { |
@@ -365,6 +367,158 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
365 | 367 | return future; |
366 | 368 | } |
367 | 369 |
|
| 370 | + /** |
| 371 | + * Get a user's notification preferences. |
| 372 | + */ |
| 373 | + public CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> getPreferences( |
| 374 | + String id) { |
| 375 | + return getPreferences(id,null); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Get a user's notification preferences. |
| 380 | + */ |
| 381 | + public CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> getPreferences(String id, |
| 382 | + RequestOptions requestOptions) { |
| 383 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getApiURL()).newBuilder() |
| 384 | + |
| 385 | + .addPathSegments("users") |
| 386 | + .addPathSegment(id) |
| 387 | + .addPathSegments("preferences") |
| 388 | + .build(); |
| 389 | + Request okhttpRequest = new Request.Builder() |
| 390 | + .url(httpUrl) |
| 391 | + .method("GET", null) |
| 392 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 393 | + .addHeader("Accept", "application/json") |
| 394 | + .build(); |
| 395 | + OkHttpClient client = clientOptions.httpClient(); |
| 396 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 397 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 398 | + } |
| 399 | + CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> future = new CompletableFuture<>(); |
| 400 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 401 | + @Override |
| 402 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 403 | + try (ResponseBody responseBody = response.body()) { |
| 404 | + if (response.isSuccessful()) { |
| 405 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UserPreferencesResponse.class), response)); |
| 406 | + return; |
| 407 | + } |
| 408 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 409 | + try { |
| 410 | + switch (response.code()) { |
| 411 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 412 | + return; |
| 413 | + case 404:future.completeExceptionally(new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 414 | + return; |
| 415 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 416 | + return; |
| 417 | + } |
| 418 | + } |
| 419 | + catch (JsonProcessingException ignored) { |
| 420 | + // unable to map error response, throwing generic error |
| 421 | + } |
| 422 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 423 | + return; |
| 424 | + } |
| 425 | + catch (IOException e) { |
| 426 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 427 | + } |
| 428 | + } |
| 429 | + |
| 430 | + @Override |
| 431 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 432 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 433 | + } |
| 434 | + }); |
| 435 | + return future; |
| 436 | + } |
| 437 | + |
| 438 | + /** |
| 439 | + * Update a user's notification preferences. |
| 440 | + */ |
| 441 | + public CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> updatePreferences( |
| 442 | + String id) { |
| 443 | + return updatePreferences(id,UpdateUserPreferencesRequest.builder().build()); |
| 444 | + } |
| 445 | + |
| 446 | + /** |
| 447 | + * Update a user's notification preferences. |
| 448 | + */ |
| 449 | + public CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> updatePreferences( |
| 450 | + String id, UpdateUserPreferencesRequest request) { |
| 451 | + return updatePreferences(id,request,null); |
| 452 | + } |
| 453 | + |
| 454 | + /** |
| 455 | + * Update a user's notification preferences. |
| 456 | + */ |
| 457 | + public CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> updatePreferences( |
| 458 | + String id, UpdateUserPreferencesRequest request, RequestOptions requestOptions) { |
| 459 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getApiURL()).newBuilder() |
| 460 | + |
| 461 | + .addPathSegments("users") |
| 462 | + .addPathSegment(id) |
| 463 | + .addPathSegments("preferences") |
| 464 | + .build(); |
| 465 | + RequestBody body; |
| 466 | + try { |
| 467 | + body = RequestBody.create(ObjectMappers.JSON_MAPPER.writeValueAsBytes(request), MediaTypes.APPLICATION_JSON); |
| 468 | + } |
| 469 | + catch(JsonProcessingException e) { |
| 470 | + throw new TrophyApiException("Failed to serialize request", e); |
| 471 | + } |
| 472 | + Request okhttpRequest = new Request.Builder() |
| 473 | + .url(httpUrl) |
| 474 | + .method("PATCH", body) |
| 475 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 476 | + .addHeader("Content-Type", "application/json") |
| 477 | + .addHeader("Accept", "application/json") |
| 478 | + .build(); |
| 479 | + OkHttpClient client = clientOptions.httpClient(); |
| 480 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 481 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 482 | + } |
| 483 | + CompletableFuture<TrophyApiHttpResponse<UserPreferencesResponse>> future = new CompletableFuture<>(); |
| 484 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 485 | + @Override |
| 486 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 487 | + try (ResponseBody responseBody = response.body()) { |
| 488 | + if (response.isSuccessful()) { |
| 489 | + future.complete(new TrophyApiHttpResponse<>(ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UserPreferencesResponse.class), response)); |
| 490 | + return; |
| 491 | + } |
| 492 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 493 | + try { |
| 494 | + switch (response.code()) { |
| 495 | + case 401:future.completeExceptionally(new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 496 | + return; |
| 497 | + case 404:future.completeExceptionally(new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 498 | + return; |
| 499 | + case 422:future.completeExceptionally(new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class), response)); |
| 500 | + return; |
| 501 | + } |
| 502 | + } |
| 503 | + catch (JsonProcessingException ignored) { |
| 504 | + // unable to map error response, throwing generic error |
| 505 | + } |
| 506 | + future.completeExceptionally(new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), response)); |
| 507 | + return; |
| 508 | + } |
| 509 | + catch (IOException e) { |
| 510 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + @Override |
| 515 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 516 | + future.completeExceptionally(new TrophyApiException("Network error executing HTTP request", e)); |
| 517 | + } |
| 518 | + }); |
| 519 | + return future; |
| 520 | + } |
| 521 | + |
368 | 522 | /** |
369 | 523 | * Get a single user's progress against all active metrics. |
370 | 524 | */ |
|
0 commit comments