|
| 1 | +package com.seam.api; |
| 2 | + |
| 3 | +import com.seam.api.core.ClientOptions; |
| 4 | +import com.seam.api.resources.accesscodes.requests.AccessCodesUpdatePutRequest; |
| 5 | +import com.seam.api.resources.actionattempts.requests.ActionAttemptsGetRequest; |
| 6 | +import com.seam.api.types.AccessCodesUpdatePutResponse; |
| 7 | +import com.seam.api.types.ActionAttemptsGetResponse; |
| 8 | +import com.seam.api.types.ActionAttemptsGetResponseActionAttemptSuccess; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +public class AccessCodesClient extends com.seam.api.resources.accesscodes.AccessCodesClient { |
| 12 | + |
| 13 | + private final SeamApiClient rootClient; |
| 14 | + |
| 15 | + public AccessCodesClient(ClientOptions clientOptions, SeamApiClient rootClient) { |
| 16 | + super(clientOptions); |
| 17 | + this.rootClient = rootClient; |
| 18 | + } |
| 19 | + |
| 20 | + public Object updateAndWaitUntilReady(AccessCodesUpdatePutRequest request) throws InterruptedException { |
| 21 | + AccessCodesUpdatePutResponse response = updatePut(request); |
| 22 | + |
| 23 | + if (response.getActionAttempt().isSuccess()) { |
| 24 | + String actionAttemptId = |
| 25 | + response.getActionAttempt().getSuccess().get().getActionAttemptId(); |
| 26 | + var actionAttempt = pollUntilReady(actionAttemptId); |
| 27 | + if (actionAttempt.getResult().isPresent() |
| 28 | + && actionAttempt.getResult().get() instanceof Map) { |
| 29 | + var accessCode = ((Map<String, Object>) actionAttempt.getResult().get()).get("access_code"); |
| 30 | + return accessCode; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + throw new RuntimeException("AccessCodeUpdate failed. See response=" + response); |
| 35 | + } |
| 36 | + |
| 37 | + private ActionAttemptsGetResponseActionAttemptSuccess pollUntilReady(String actionAttemptId) |
| 38 | + throws InterruptedException { |
| 39 | + ActionAttemptsGetResponse response = null; |
| 40 | + while (response == null || response.getActionAttempt().isPending()) { |
| 41 | + response = rootClient |
| 42 | + .actionAttempts() |
| 43 | + .get(ActionAttemptsGetRequest.builder() |
| 44 | + .actionAttemptId(actionAttemptId) |
| 45 | + .build()); |
| 46 | + Thread.sleep(2500); |
| 47 | + } |
| 48 | + |
| 49 | + if (response.getActionAttempt().isError()) { |
| 50 | + var errorResponse = response.getActionAttempt().getError().get(); |
| 51 | + throw new ActionAttemptFailedException( |
| 52 | + errorResponse.getActionAttemptId(), errorResponse.getError().getMessage()); |
| 53 | + } |
| 54 | + |
| 55 | + return response.getActionAttempt().getSuccess().get(); |
| 56 | + } |
| 57 | + |
| 58 | + public static class ActionAttemptFailedException extends RuntimeException { |
| 59 | + |
| 60 | + public final String actionAttemptId; |
| 61 | + public final String errorMessage; |
| 62 | + |
| 63 | + public ActionAttemptFailedException(String actionAttemptId, String errorMessage) { |
| 64 | + this.actionAttemptId = actionAttemptId; |
| 65 | + this.errorMessage = errorMessage; |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments