|
20 | 20 | import org.sourcelab.buildkite.api.client.exception.BuildkiteException; |
21 | 21 | import org.sourcelab.buildkite.api.client.exception.InvalidAccessTokenException; |
22 | 22 | import org.sourcelab.buildkite.api.client.exception.InvalidAllowedIpAddressException; |
| 23 | +import org.sourcelab.buildkite.api.client.exception.InvalidPagingRequestException; |
23 | 24 | import org.sourcelab.buildkite.api.client.exception.NotFoundException; |
24 | 25 | import org.sourcelab.buildkite.api.client.http.Client; |
25 | 26 | import org.sourcelab.buildkite.api.client.http.HttpResult; |
|
31 | 32 | import org.sourcelab.buildkite.api.client.request.GetUserRequest; |
32 | 33 | import org.sourcelab.buildkite.api.client.request.ListBuildsRequest; |
33 | 34 | import org.sourcelab.buildkite.api.client.request.ListEmojisRequest; |
| 35 | +import org.sourcelab.buildkite.api.client.request.PageOptions; |
| 36 | +import org.sourcelab.buildkite.api.client.request.PageableRequest; |
34 | 37 | import org.sourcelab.buildkite.api.client.request.PingRequest; |
35 | 38 | import org.sourcelab.buildkite.api.client.request.Request; |
36 | 39 | import org.sourcelab.buildkite.api.client.response.AccessTokenResponse; |
|
39 | 42 | import org.sourcelab.buildkite.api.client.response.ErrorResponse; |
40 | 43 | import org.sourcelab.buildkite.api.client.response.ListBuildsResponse; |
41 | 44 | import org.sourcelab.buildkite.api.client.response.MetaResponse; |
| 45 | +import org.sourcelab.buildkite.api.client.response.PageableResponse; |
| 46 | +import org.sourcelab.buildkite.api.client.response.PagingLinks; |
42 | 47 | import org.sourcelab.buildkite.api.client.response.PingResponse; |
43 | 48 | import org.sourcelab.buildkite.api.client.response.parser.ErrorResponseParser; |
44 | 49 |
|
45 | 50 | import java.io.IOException; |
46 | 51 | import java.util.List; |
| 52 | +import java.util.Objects; |
47 | 53 |
|
48 | 54 | /** |
49 | 55 | * API Client for Buildkite's REST Api. |
@@ -121,23 +127,180 @@ public MetaResponse getMeta() throws BuildkiteException { |
121 | 127 | * List all of the Emojis defined in the given Organization. |
122 | 128 | * @param orgIdSlug Organization Id slug to retrieve list of emojis for. |
123 | 129 | * @return List of Emojis. |
| 130 | + * @throws BuildkiteException if API returns an error response. |
124 | 131 | */ |
125 | 132 | public List<Emoji> listEmojis(final String orgIdSlug) { |
126 | 133 | return executeRequest(new ListEmojisRequest(orgIdSlug)); |
127 | 134 | } |
128 | 135 |
|
| 136 | + /** |
| 137 | + * Retrieve all Builds accessible to the current user/API access token, across all Organizations. |
| 138 | + * Results will be paged. |
| 139 | + * |
| 140 | + * @return All Builds accessible to the current user/API access token, across all Organizations. Results |
| 141 | + * will be paged if the number of results exceeds 30. |
| 142 | + * @throws BuildkiteException if API returns an error response. |
| 143 | + */ |
129 | 144 | public ListBuildsResponse listBuilds() { |
130 | 145 | return listBuilds(BuildFilters.newBuilder().build()); |
131 | 146 | } |
132 | 147 |
|
| 148 | + /** |
| 149 | + * Retrieve all builds which match the supplied search criteria. |
| 150 | + * |
| 151 | + * @return All builds which match the supplied search criteria. Results will be paged if the number of results |
| 152 | + * exceeds 30 (or the page limit specified in the search criteria). |
| 153 | + * |
| 154 | + * @throws BuildkiteException if API returns an error response. |
| 155 | + */ |
133 | 156 | public ListBuildsResponse listBuilds(final BuildFiltersBuilder filtersBuilder) { |
134 | 157 | return listBuilds(filtersBuilder.build()); |
135 | 158 | } |
136 | 159 |
|
| 160 | + /** |
| 161 | + * Retrieve all builds which match the supplied search criteria. |
| 162 | + * |
| 163 | + * @return All builds which match the supplied search criteria. Results will be paged if the number of results |
| 164 | + * exceeds 30 (or the page limit specified in the search criteria). |
| 165 | + * |
| 166 | + * @throws BuildkiteException if API returns an error response. |
| 167 | + */ |
137 | 168 | public ListBuildsResponse listBuilds(final BuildFilters filters) { |
138 | 169 | return executeRequest(new ListBuildsRequest(filters)); |
139 | 170 | } |
140 | 171 |
|
| 172 | + /** |
| 173 | + * Retrieve the next page of results from the previously retrieved request. |
| 174 | + * |
| 175 | + * @param <T> The parsed return object representing the result. |
| 176 | + * @param response Previously retrieved result/response to retrieve the next page of results for. |
| 177 | + * @return The next page of results. |
| 178 | + * @throws InvalidPagingRequestException if no next page exists to retrieve. |
| 179 | + * @throws BuildkiteException if API returns an error response. |
| 180 | + */ |
| 181 | + public <T> T nextPage(final PageableResponse<T> response) { |
| 182 | + // Validate |
| 183 | + Objects.requireNonNull(response); |
| 184 | + final PagingLinks pagingLinks = Objects.requireNonNull(response.getPagingLinks()); |
| 185 | + if (!pagingLinks.hasNextUrl()) { |
| 186 | + throw new InvalidPagingRequestException( |
| 187 | + "Requested 'Next' page on response " + response.getClass().getSimpleName() + ", but no Next page is available." |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + // Update request with appropriate page options. |
| 192 | + final PageOptions pageOptions; |
| 193 | + try { |
| 194 | + pageOptions = PageOptions.fromUrl(pagingLinks.getNextUrl()); |
| 195 | + } catch (final IllegalArgumentException ex) { |
| 196 | + throw new InvalidPagingRequestException("Unable to parse URL for paging information", ex); |
| 197 | + } |
| 198 | + final PageableRequest<T> request = response.getOriginalRequest(); |
| 199 | + request.updatePageOptions(pageOptions); |
| 200 | + |
| 201 | + // Execute and return. |
| 202 | + return executeRequest(request); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Retrieve the previous page of results from the previously retrieved request. |
| 207 | + * |
| 208 | + * @param <T> The parsed return object representing the result. |
| 209 | + * @param response Previously retrieved result/response to retrieve the previous page of results for. |
| 210 | + * @return The previous page of results. |
| 211 | + * @throws InvalidPagingRequestException if no previous page exists to retrieve. |
| 212 | + * @throws BuildkiteException if API returns an error response. |
| 213 | + */ |
| 214 | + public <T> T previousPage(final PageableResponse<T> response) { |
| 215 | + // Validate |
| 216 | + Objects.requireNonNull(response); |
| 217 | + final PagingLinks pagingLinks = Objects.requireNonNull(response.getPagingLinks()); |
| 218 | + if (!pagingLinks.hasPrevUrl()) { |
| 219 | + throw new InvalidPagingRequestException( |
| 220 | + "Requested 'Previous' page on response " + response.getClass().getSimpleName() + ", but no Previous page is available." |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + // Update request with appropriate page options. |
| 225 | + final PageOptions pageOptions; |
| 226 | + try { |
| 227 | + pageOptions = PageOptions.fromUrl(pagingLinks.getNextUrl()); |
| 228 | + } catch (final IllegalArgumentException ex) { |
| 229 | + throw new InvalidPagingRequestException("Unable to parse URL for paging information", ex); |
| 230 | + } |
| 231 | + final PageableRequest<T> request = response.getOriginalRequest(); |
| 232 | + request.updatePageOptions(pageOptions); |
| 233 | + |
| 234 | + // Execute and return. |
| 235 | + return executeRequest(request); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Retrieve the first page of results from the previously retrieved request. |
| 240 | + * |
| 241 | + * @param <T> The parsed return object representing the result. |
| 242 | + * @param response Previously retrieved result/response to retrieve the first page of results for. |
| 243 | + * @return The first page of results. |
| 244 | + * @throws InvalidPagingRequestException if no previous page exists to retrieve. |
| 245 | + * @throws BuildkiteException if API returns an error response. |
| 246 | + */ |
| 247 | + public <T> T firstPage(final PageableResponse<T> response) { |
| 248 | + // Validate |
| 249 | + Objects.requireNonNull(response); |
| 250 | + final PagingLinks pagingLinks = Objects.requireNonNull(response.getPagingLinks()); |
| 251 | + if (!pagingLinks.hasFirstUrl()) { |
| 252 | + throw new InvalidPagingRequestException( |
| 253 | + "Requested 'First' page on response " + response.getClass().getSimpleName() + ", but no First page is available." |
| 254 | + ); |
| 255 | + } |
| 256 | + |
| 257 | + // Update request with appropriate page options. |
| 258 | + final PageOptions pageOptions; |
| 259 | + try { |
| 260 | + pageOptions = PageOptions.fromUrl(pagingLinks.getNextUrl()); |
| 261 | + } catch (final IllegalArgumentException ex) { |
| 262 | + throw new InvalidPagingRequestException("Unable to parse URL for paging information", ex); |
| 263 | + } |
| 264 | + final PageableRequest<T> request = response.getOriginalRequest(); |
| 265 | + request.updatePageOptions(pageOptions); |
| 266 | + |
| 267 | + // Execute and return. |
| 268 | + return executeRequest(request); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * Retrieve the last page of results from the previously retrieved request. |
| 273 | + * |
| 274 | + * @param <T> The parsed return object representing the result. |
| 275 | + * @param response Previously retrieved result/response to retrieve the last page of results for. |
| 276 | + * @return The last page of results. |
| 277 | + * @throws InvalidPagingRequestException if no previous page exists to retrieve. |
| 278 | + * @throws BuildkiteException if API returns an error response. |
| 279 | + */ |
| 280 | + public <T> T lastPage(final PageableResponse<T> response) { |
| 281 | + // Validate |
| 282 | + Objects.requireNonNull(response); |
| 283 | + final PagingLinks pagingLinks = Objects.requireNonNull(response.getPagingLinks()); |
| 284 | + if (!pagingLinks.hasLastUrl()) { |
| 285 | + throw new InvalidPagingRequestException( |
| 286 | + "Requested 'Last' page on response " + response.getClass().getSimpleName() + ", but no Last page is available." |
| 287 | + ); |
| 288 | + } |
| 289 | + |
| 290 | + // Update request with appropriate page options. |
| 291 | + final PageOptions pageOptions; |
| 292 | + try { |
| 293 | + pageOptions = PageOptions.fromUrl(pagingLinks.getNextUrl()); |
| 294 | + } catch (final IllegalArgumentException ex) { |
| 295 | + throw new InvalidPagingRequestException("Unable to parse URL for paging information", ex); |
| 296 | + } |
| 297 | + final PageableRequest<T> request = response.getOriginalRequest(); |
| 298 | + request.updatePageOptions(pageOptions); |
| 299 | + |
| 300 | + // Execute and return. |
| 301 | + return executeRequest(request); |
| 302 | + } |
| 303 | + |
141 | 304 | /** |
142 | 305 | * Execute the given request, returning the parsed response, or throwing the appropriate |
143 | 306 | * exception if an error was returned from the API. |
@@ -168,7 +331,7 @@ public <T> T executeRequest(final Request<T> request) throws BuildkiteException |
168 | 331 | * @throws BuildkiteException relating to specific underlying API error. |
169 | 332 | */ |
170 | 333 | private void handleError(final HttpResult errorResult) throws BuildkiteException { |
171 | | - // Attempt to parse error respone. |
| 334 | + // Attempt to parse error response. |
172 | 335 | String errorMessage = null; |
173 | 336 | try { |
174 | 337 | final ErrorResponse errorResponse = new ErrorResponseParser().parseResponse(errorResult); |
|
0 commit comments