|
29 | 29 | import okhttp3.Response; |
30 | 30 | import okhttp3.ResponseBody; |
31 | 31 | import so.trophy.resources.users.requests.UsersAchievementsRequest; |
| 32 | +import so.trophy.resources.users.requests.UsersLeaderboardsRequest; |
32 | 33 | import so.trophy.resources.users.requests.UsersMetricEventSummaryRequest; |
33 | 34 | import so.trophy.resources.users.requests.UsersPointsEventSummaryRequest; |
34 | 35 | import so.trophy.resources.users.requests.UsersPointsRequest; |
|
43 | 44 | import so.trophy.types.UpdatedUser; |
44 | 45 | import so.trophy.types.UpsertedUser; |
45 | 46 | import so.trophy.types.User; |
| 47 | +import so.trophy.types.UserLeaderboardResponse; |
46 | 48 |
|
47 | 49 | public class UsersClient { |
48 | 50 | protected final ClientOptions clientOptions; |
@@ -677,4 +679,65 @@ public List<UsersPointsEventSummaryResponseItem> pointsEventSummary(String id, S |
677 | 679 | throw new TrophyApiException("Network error executing HTTP request", e); |
678 | 680 | } |
679 | 681 | } |
680 | | - } |
| 682 | + |
| 683 | + /** |
| 684 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 685 | + */ |
| 686 | + public UserLeaderboardResponse leaderboards(String id, String key) { |
| 687 | + return leaderboards(id,key,UsersLeaderboardsRequest.builder().build()); |
| 688 | + } |
| 689 | + |
| 690 | + /** |
| 691 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 692 | + */ |
| 693 | + public UserLeaderboardResponse leaderboards(String id, String key, |
| 694 | + UsersLeaderboardsRequest request) { |
| 695 | + return leaderboards(id,key,request,null); |
| 696 | + } |
| 697 | + |
| 698 | + /** |
| 699 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 700 | + */ |
| 701 | + public UserLeaderboardResponse leaderboards(String id, String key, |
| 702 | + UsersLeaderboardsRequest request, RequestOptions requestOptions) { |
| 703 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 704 | + |
| 705 | + .addPathSegments("users") |
| 706 | + .addPathSegment(id) |
| 707 | + .addPathSegments("leaderboards") |
| 708 | + .addPathSegment(key);if (request.getRun().isPresent()) { |
| 709 | + httpUrl.addQueryParameter("run", request.getRun().get()); |
| 710 | + } |
| 711 | + Request.Builder _requestBuilder = new Request.Builder() |
| 712 | + .url(httpUrl.build()) |
| 713 | + .method("GET", null) |
| 714 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 715 | + .addHeader("Content-Type", "application/json").addHeader("Accept", "application/json"); |
| 716 | + Request okhttpRequest = _requestBuilder.build(); |
| 717 | + OkHttpClient client = clientOptions.httpClient(); |
| 718 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 719 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 720 | + } |
| 721 | + try (Response response = client.newCall(okhttpRequest).execute()) { |
| 722 | + ResponseBody responseBody = response.body(); |
| 723 | + if (response.isSuccessful()) { |
| 724 | + return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UserLeaderboardResponse.class); |
| 725 | + } |
| 726 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 727 | + try { |
| 728 | + switch (response.code()) { |
| 729 | + case 401:throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 730 | + case 404:throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 731 | + case 422:throw new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 732 | + } |
| 733 | + } |
| 734 | + catch (JsonProcessingException ignored) { |
| 735 | + // unable to map error response, throwing generic error |
| 736 | + } |
| 737 | + throw new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 738 | + } |
| 739 | + catch (IOException e) { |
| 740 | + throw new TrophyApiException("Network error executing HTTP request", e); |
| 741 | + } |
| 742 | + } |
| 743 | + } |
0 commit comments