|
21 | 21 | import com.auth0.client.mgmt.types.ClientGrantResponseContent; |
22 | 22 | import com.auth0.client.mgmt.types.CreateClientGrantRequestContent; |
23 | 23 | import com.auth0.client.mgmt.types.CreateClientGrantResponseContent; |
| 24 | +import com.auth0.client.mgmt.types.GetClientGrantResponseContent; |
24 | 25 | import com.auth0.client.mgmt.types.ListClientGrantPaginatedResponseContent; |
25 | 26 | import com.auth0.client.mgmt.types.ListClientGrantsRequestParameters; |
26 | 27 | import com.auth0.client.mgmt.types.UpdateClientGrantRequestContent; |
@@ -278,6 +279,91 @@ public void onFailure(@NotNull Call call, @NotNull IOException e) { |
278 | 279 | return future; |
279 | 280 | } |
280 | 281 |
|
| 282 | + /** |
| 283 | + * Retrieve a single <a href="https://auth0.com/docs/get-started/applications/application-access-to-apis-client-grants">client grant</a>, including the |
| 284 | + * scopes associated with the application/API pair. |
| 285 | + */ |
| 286 | + public CompletableFuture<ManagementApiHttpResponse<GetClientGrantResponseContent>> get(String id) { |
| 287 | + return get(id, null); |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Retrieve a single <a href="https://auth0.com/docs/get-started/applications/application-access-to-apis-client-grants">client grant</a>, including the |
| 292 | + * scopes associated with the application/API pair. |
| 293 | + */ |
| 294 | + public CompletableFuture<ManagementApiHttpResponse<GetClientGrantResponseContent>> get( |
| 295 | + String id, RequestOptions requestOptions) { |
| 296 | + HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl()) |
| 297 | + .newBuilder() |
| 298 | + .addPathSegments("client-grants") |
| 299 | + .addPathSegment(id) |
| 300 | + .build(); |
| 301 | + Request okhttpRequest = new Request.Builder() |
| 302 | + .url(httpUrl) |
| 303 | + .method("GET", null) |
| 304 | + .headers(Headers.of(clientOptions.headers(requestOptions))) |
| 305 | + .addHeader("Accept", "application/json") |
| 306 | + .build(); |
| 307 | + OkHttpClient client = clientOptions.httpClient(); |
| 308 | + if (requestOptions != null && requestOptions.getTimeout().isPresent()) { |
| 309 | + client = clientOptions.httpClientWithTimeout(requestOptions); |
| 310 | + } |
| 311 | + CompletableFuture<ManagementApiHttpResponse<GetClientGrantResponseContent>> future = new CompletableFuture<>(); |
| 312 | + client.newCall(okhttpRequest).enqueue(new Callback() { |
| 313 | + @Override |
| 314 | + public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException { |
| 315 | + try (ResponseBody responseBody = response.body()) { |
| 316 | + String responseBodyString = responseBody != null ? responseBody.string() : "{}"; |
| 317 | + if (response.isSuccessful()) { |
| 318 | + future.complete(new ManagementApiHttpResponse<>( |
| 319 | + ObjectMappers.JSON_MAPPER.readValue( |
| 320 | + responseBodyString, GetClientGrantResponseContent.class), |
| 321 | + response)); |
| 322 | + return; |
| 323 | + } |
| 324 | + try { |
| 325 | + switch (response.code()) { |
| 326 | + case 401: |
| 327 | + future.completeExceptionally(new UnauthorizedError( |
| 328 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 329 | + response)); |
| 330 | + return; |
| 331 | + case 403: |
| 332 | + future.completeExceptionally(new ForbiddenError( |
| 333 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 334 | + response)); |
| 335 | + return; |
| 336 | + case 404: |
| 337 | + future.completeExceptionally(new NotFoundError( |
| 338 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 339 | + response)); |
| 340 | + return; |
| 341 | + case 429: |
| 342 | + future.completeExceptionally(new TooManyRequestsError( |
| 343 | + ObjectMappers.JSON_MAPPER.readValue(responseBodyString, Object.class), |
| 344 | + response)); |
| 345 | + return; |
| 346 | + } |
| 347 | + } catch (JsonProcessingException ignored) { |
| 348 | + // unable to map error response, throwing generic error |
| 349 | + } |
| 350 | + Object errorBody = ObjectMappers.parseErrorBody(responseBodyString); |
| 351 | + future.completeExceptionally(new ManagementApiException( |
| 352 | + "Error with status code " + response.code(), response.code(), errorBody, response)); |
| 353 | + return; |
| 354 | + } catch (IOException e) { |
| 355 | + future.completeExceptionally(new ManagementException("Network error executing HTTP request", e)); |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public void onFailure(@NotNull Call call, @NotNull IOException e) { |
| 361 | + future.completeExceptionally(new ManagementException("Network error executing HTTP request", e)); |
| 362 | + } |
| 363 | + }); |
| 364 | + return future; |
| 365 | + } |
| 366 | + |
281 | 367 | /** |
282 | 368 | * Delete the <a href="https://www.auth0.com/docs/get-started/authentication-and-authorization-flow/client-credentials-flow">Client Credential Flow</a> from your machine-to-machine application. |
283 | 369 | */ |
|
0 commit comments