@Override public String getName() { return completedSnapshot.getName(); }
completedSnapshot.isDone(), "given snapshot must already be completed"); final ApiFuture<MetadataT> metadataFuture = ApiFutures.immediateFuture((MetadataT) completedSnapshot.getMetadata()); final ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFuture(completedSnapshot);
@Override public ResponseT get() throws ExecutionException, InterruptedException { return (ResponseT) pollingFuture.get().getResponse(); }
@Override public ResponseT apply(OperationSnapshot operationSnapshot) { if (!operationSnapshot.getErrorCode().getCode().equals(Code.OK)) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" failed with status = " + operationSnapshot.getErrorCode() + " and message = " + operationSnapshot.getErrorMessage(), null, operationSnapshot.getErrorCode(), false); } try { return transformer.apply((Any) operationSnapshot.getResponse()); } catch (RuntimeException e) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" succeeded, but encountered a problem unpacking it.", e, operationSnapshot.getErrorCode(), false); } }
@Override public MetadataT apply(OperationSnapshot operationSnapshot) { try { return transformer.apply( operationSnapshot.getMetadata() != null ? (Any) operationSnapshot.getMetadata() : null); } catch (RuntimeException e) { throw ApiExceptionFactory.createException( "Polling operation with name \"" + operationSnapshot.getName() + "\" succeeded, but encountered a problem unpacking it.", e, operationSnapshot.getErrorCode(), false); } }
@Override public ResponseT get() throws ExecutionException { if (completedSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { return (ResponseT) completedSnapshot.getResponse(); } throw new ExecutionException( new ApiException(null, completedSnapshot.getErrorCode(), false)); }
/** * This method is supposed to be called from {@link AttemptCallable#call()} * * @param ignored The ignored request; the actual request will be composed based on the result of * the {@code initialFuture}. * @param callContext call context */ @Override public ApiFuture<OperationSnapshot> futureCall(RequestT ignored, ApiCallContext callContext) { try { if (!initialFuture.isDone() || initialFuture.isCancelled()) { return initialFuture; } // Since initialFuture is done at this point, the following call should be non-blocking OperationSnapshot initialOperation = initialFuture.get(); if (initialOperation.isDone()) { return initialFuture; } return longRunningClient .getOperationCallable() .futureCall(initialOperation.getName(), callContext); } catch (ExecutionException e) { return ApiFutures.immediateFailedFuture(e.getCause()); } catch (InterruptedException e) { return ApiFutures.immediateFailedFuture(e); } } }
@Override public Long apply(OperationSnapshot operationSnapshot) { return (Long) operationSnapshot.getMetadata(); } }
@Override public boolean shouldRetry(Throwable prevThrowable, OperationSnapshot prevResponse) { return prevThrowable == null && prevResponse != null && !prevResponse.isDone(); } }
@Override public ResponseT apply(OperationSnapshot operationSnapshot) { if (!operationSnapshot.getErrorCode().getCode().equals(Code.OK)) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" failed with status = " + operationSnapshot.getErrorCode() + " and message = " + operationSnapshot.getErrorMessage(), null, operationSnapshot.getErrorCode(), false); } try { return transformer.apply((Any) operationSnapshot.getResponse()); } catch (RuntimeException e) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" succeeded, but encountered a problem unpacking it.", e, operationSnapshot.getErrorCode(), false); } }
@Override public MetadataT apply(OperationSnapshot operationSnapshot) { try { return transformer.apply( operationSnapshot.getMetadata() != null ? (Any) operationSnapshot.getMetadata() : null); } catch (RuntimeException e) { throw ApiExceptionFactory.createException( "Polling operation with name \"" + operationSnapshot.getName() + "\" succeeded, but encountered a problem unpacking it.", e, operationSnapshot.getErrorCode(), false); } }
@Override public ResponseT get() throws ExecutionException { if (completedSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { return (ResponseT) completedSnapshot.getResponse(); } throw new ExecutionException( new ApiException(null, completedSnapshot.getErrorCode(), false)); }
/** * This method is supposed to be called from {@link AttemptCallable#call()} * * @param ignored The ignored request; the actual request will be composed based on the result of * the {@code initialFuture}. * @param callContext call context */ @Override public ApiFuture<OperationSnapshot> futureCall(RequestT ignored, ApiCallContext callContext) { try { if (!initialFuture.isDone() || initialFuture.isCancelled()) { return initialFuture; } // Since initialFuture is done at this point, the following call should be non-blocking OperationSnapshot initialOperation = initialFuture.get(); if (initialOperation.isDone()) { return initialFuture; } return longRunningClient .getOperationCallable() .futureCall(initialOperation.getName(), callContext); } catch (ExecutionException e) { return ApiFutures.immediateFailedFuture(e.getCause()); } catch (InterruptedException e) { return ApiFutures.immediateFailedFuture(e); } } }
@Override public Integer apply(OperationSnapshot operationSnapshot) { return (int) operationSnapshot.getMetadata(); } };
@Override public boolean shouldRetry(Throwable prevThrowable, OperationSnapshot prevResponse) { return prevThrowable == null && prevResponse != null && !prevResponse.isDone(); } }
@Override public Color apply(OperationSnapshot operationSnapshot) { if (!operationSnapshot.getErrorCode().getCode().equals(StatusCode.Code.OK)) { throw ApiExceptionFactory.createException( "Operation with name \"" + operationSnapshot.getName() + "\" failed with status = " + operationSnapshot.getErrorCode() + " and message = " + operationSnapshot.getErrorMessage(), null, operationSnapshot.getErrorCode(), false); } if (operationSnapshot.getResponse() == null) { return null; } if (!(operationSnapshot.getResponse() instanceof Color)) { String errorMessage = "type mismatch: expected " + Color.class.getName() + ", found " + operationSnapshot.getResponse().getClass().getName(); throw new ApiException(errorMessage, null, FakeStatusCode.of(StatusCode.Code.OK), false); } else { return (Color) operationSnapshot.getResponse(); } } }
@Override public String getName() { return completedSnapshot.getName(); }
completedSnapshot.isDone(), "given snapshot must already be completed"); final ApiFuture<MetadataT> metadataFuture = ApiFutures.immediateFuture((MetadataT) completedSnapshot.getMetadata()); final ApiFuture<OperationSnapshot> initialFuture = ApiFutures.immediateFuture(completedSnapshot);
@Override public String apply(OperationSnapshot operationSnapshot) { return (String) operationSnapshot.getResponse(); } }
@Override public Currency apply(OperationSnapshot operationSnapshot) { if (operationSnapshot.getMetadata() == null) { return null; } if (!(operationSnapshot.getMetadata() instanceof Currency)) { String errorMessage = "type mismatch: expected " + Currency.class.getName() + ", found " + operationSnapshot.getMetadata().getClass().getName(); throw new ApiException(errorMessage, null, FakeStatusCode.of(StatusCode.Code.OK), false); } else { return (Currency) operationSnapshot.getMetadata(); } } }