|
| 1 | +package so.trophy.resources.leaderboards; |
| 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 com.fasterxml.jackson.core.type.TypeReference; |
| 10 | +import so.trophy.core.ClientOptions; |
| 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.errors.NotFoundError; |
| 16 | +import so.trophy.errors.UnauthorizedError; |
| 17 | +import so.trophy.errors.UnprocessableEntityError; |
| 18 | +import java.io.IOException; |
| 19 | +import java.lang.Object; |
| 20 | +import java.lang.String; |
| 21 | +import java.util.List; |
| 22 | +import okhttp3.Headers; |
| 23 | +import okhttp3.HttpUrl; |
| 24 | +import okhttp3.OkHttpClient; |
| 25 | +import okhttp3.Request; |
| 26 | +import okhttp3.Response; |
| 27 | +import okhttp3.ResponseBody; |
| 28 | +import so.trophy.resources.leaderboards.requests.LeaderboardsGetRequest; |
| 29 | +import so.trophy.resources.leaderboards.requests.UsersLeaderboardsRequest; |
| 30 | +import so.trophy.types.ErrorBody; |
| 31 | +import so.trophy.types.LeaderboardResponse; |
| 32 | +import so.trophy.types.LeaderboardResponseWithRankings; |
| 33 | +import so.trophy.types.UserLeaderboardResponse; |
| 34 | + |
| 35 | +public class LeaderboardsClient { |
| 36 | + protected final ClientOptions clientOptions; |
| 37 | + |
| 38 | + public LeaderboardsClient(ClientOptions clientOptions) { |
| 39 | + this.clientOptions = clientOptions; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Get all active leaderboards for your organization. |
| 44 | + */ |
| 45 | + public List<LeaderboardResponse> all() { |
| 46 | + return all(null); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get all active leaderboards for your organization. |
| 51 | + */ |
| 52 | + public List<LeaderboardResponse> all(RequestOptions requestOptions) { |
| 53 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 54 | + |
| 55 | + .addPathSegments("leaderboards") |
| 56 | + .build(); |
| 57 | + Request okhttpRequest = new Request.Builder() |
| 58 | + .url(httpUrl) |
| 59 | + .method("GET", null) |
| 60 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 61 | + .addHeader("Content-Type", "application/json") |
| 62 | + .addHeader("Accept", "application/json") |
| 63 | + .build(); |
| 64 | + OkHttpClient client = clientOptions.httpClient(); |
| 65 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 66 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 67 | + } |
| 68 | + try (Response response = client.newCall(okhttpRequest).execute()) { |
| 69 | + ResponseBody responseBody = response.body(); |
| 70 | + if (response.isSuccessful()) { |
| 71 | + return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), new TypeReference<List<LeaderboardResponse>>() {}); |
| 72 | + } |
| 73 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 74 | + try { |
| 75 | + switch (response.code()) { |
| 76 | + case 401:throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 77 | + case 422:throw new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 78 | + } |
| 79 | + } |
| 80 | + catch (JsonProcessingException ignored) { |
| 81 | + // unable to map error response, throwing generic error |
| 82 | + } |
| 83 | + throw new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 84 | + } |
| 85 | + catch (IOException e) { |
| 86 | + throw new TrophyApiException("Network error executing HTTP request", e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get a specific leaderboard by its key. |
| 92 | + */ |
| 93 | + public LeaderboardResponseWithRankings get(String key) { |
| 94 | + return get(key,LeaderboardsGetRequest.builder().build()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get a specific leaderboard by its key. |
| 99 | + */ |
| 100 | + public LeaderboardResponseWithRankings get(String key, LeaderboardsGetRequest request) { |
| 101 | + return get(key,request,null); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Get a specific leaderboard by its key. |
| 106 | + */ |
| 107 | + public LeaderboardResponseWithRankings get(String key, LeaderboardsGetRequest request, |
| 108 | + RequestOptions requestOptions) { |
| 109 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 110 | + |
| 111 | + .addPathSegments("leaderboards") |
| 112 | + .addPathSegment(key);if (request.getOffset().isPresent()) { |
| 113 | + httpUrl.addQueryParameter("offset", request.getOffset().get().toString()); |
| 114 | + } |
| 115 | + if (request.getLimit().isPresent()) { |
| 116 | + httpUrl.addQueryParameter("limit", request.getLimit().get().toString()); |
| 117 | + } |
| 118 | + if (request.getRun().isPresent()) { |
| 119 | + httpUrl.addQueryParameter("run", request.getRun().get()); |
| 120 | + } |
| 121 | + if (request.getUserId().isPresent()) { |
| 122 | + httpUrl.addQueryParameter("userId", request.getUserId().get()); |
| 123 | + } |
| 124 | + Request.Builder _requestBuilder = new Request.Builder() |
| 125 | + .url(httpUrl.build()) |
| 126 | + .method("GET", null) |
| 127 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 128 | + .addHeader("Content-Type", "application/json").addHeader("Accept", "application/json"); |
| 129 | + Request okhttpRequest = _requestBuilder.build(); |
| 130 | + OkHttpClient client = clientOptions.httpClient(); |
| 131 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 132 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 133 | + } |
| 134 | + try (Response response = client.newCall(okhttpRequest).execute()) { |
| 135 | + ResponseBody responseBody = response.body(); |
| 136 | + if (response.isSuccessful()) { |
| 137 | + return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), LeaderboardResponseWithRankings.class); |
| 138 | + } |
| 139 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 140 | + try { |
| 141 | + switch (response.code()) { |
| 142 | + case 401:throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 143 | + case 404:throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 144 | + case 422:throw new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 145 | + } |
| 146 | + } |
| 147 | + catch (JsonProcessingException ignored) { |
| 148 | + // unable to map error response, throwing generic error |
| 149 | + } |
| 150 | + throw new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 151 | + } |
| 152 | + catch (IOException e) { |
| 153 | + throw new TrophyApiException("Network error executing HTTP request", e); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 159 | + */ |
| 160 | + public UserLeaderboardResponse usersLeaderboards(String userId, String key) { |
| 161 | + return usersLeaderboards(userId,key,UsersLeaderboardsRequest.builder().build()); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 166 | + */ |
| 167 | + public UserLeaderboardResponse usersLeaderboards(String userId, String key, |
| 168 | + UsersLeaderboardsRequest request) { |
| 169 | + return usersLeaderboards(userId,key,request,null); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get a user's rank, value, and history for a specific leaderboard. |
| 174 | + */ |
| 175 | + public UserLeaderboardResponse usersLeaderboards(String userId, String key, |
| 176 | + UsersLeaderboardsRequest request, RequestOptions requestOptions) { |
| 177 | + HttpUrl.Builder httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()).newBuilder() |
| 178 | + |
| 179 | + .addPathSegments("users") |
| 180 | + .addPathSegment(userId) |
| 181 | + .addPathSegments("leaderboards") |
| 182 | + .addPathSegment(key);if (request.getRun().isPresent()) { |
| 183 | + httpUrl.addQueryParameter("run", request.getRun().get()); |
| 184 | + } |
| 185 | + Request.Builder _requestBuilder = new Request.Builder() |
| 186 | + .url(httpUrl.build()) |
| 187 | + .method("GET", null) |
| 188 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 189 | + .addHeader("Content-Type", "application/json").addHeader("Accept", "application/json"); |
| 190 | + Request okhttpRequest = _requestBuilder.build(); |
| 191 | + OkHttpClient client = clientOptions.httpClient(); |
| 192 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 193 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 194 | + } |
| 195 | + try (Response response = client.newCall(okhttpRequest).execute()) { |
| 196 | + ResponseBody responseBody = response.body(); |
| 197 | + if (response.isSuccessful()) { |
| 198 | + return ObjectMappers.JSON_MAPPER.readValue(responseBody.string(), UserLeaderboardResponse.class); |
| 199 | + } |
| 200 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 201 | + try { |
| 202 | + switch (response.code()) { |
| 203 | + case 401:throw new UnauthorizedError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 204 | + case 404:throw new NotFoundError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 205 | + case 422:throw new UnprocessableEntityError(ObjectMappers.JSON_MAPPER.readValue(responseBodyString, ErrorBody.class)); |
| 206 | + } |
| 207 | + } |
| 208 | + catch (JsonProcessingException ignored) { |
| 209 | + // unable to map error response, throwing generic error |
| 210 | + } |
| 211 | + throw new TrophyApiApiException("Error with status code " + response.code(), response.code(), ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class)); |
| 212 | + } |
| 213 | + catch (IOException e) { |
| 214 | + throw new TrophyApiException("Network error executing HTTP request", e); |
| 215 | + } |
| 216 | + } |
| 217 | + } |
0 commit comments