Skip to content

Commit

Permalink
Merge pull request Azure#45 from jianghaolu/autorest_1343
Browse files Browse the repository at this point in the history
Paging and LRO fixes from AutoRest
  • Loading branch information
jianghaolu committed Aug 10, 2016
2 parents e32f486 + ca25c1c commit 7c46138
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ public <T, THeader> ServiceResponseWithHeaders<T, THeader> getPutOrPatchResultWi
*/
public <T> AsyncPollingTask<T> getPutOrPatchResultAsync(Response<ResponseBody> response, Type resourceType, ServiceCall<T> serviceCall, ServiceCallback<T> callback) {
if (response == null) {
callback.failure(new ServiceException("response is null."));
CloudException t = new CloudException("response is null.");
if (callback != null) {
callback.failure(t);
}
serviceCall.failure(t);
return null;
}

Expand All @@ -178,15 +182,21 @@ public <T> AsyncPollingTask<T> getPutOrPatchResultAsync(Response<ResponseBody> r
responseBody.close();
}
} catch (Exception e) { /* ignore serialization errors on top of service errors */ }
callback.failure(exception);
if (callback != null) {
callback.failure(exception);
}
serviceCall.failure(exception);
return null;
}

PollingState<T> pollingState;
try {
pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter());
} catch (IOException e) {
callback.failure(e);
if (callback != null) {
callback.failure(e);
}
serviceCall.failure(e);
return null;
}
String url = response.raw().request().url().toString();
Expand Down Expand Up @@ -215,17 +225,24 @@ public <T, THeader> AsyncPollingTask<T> getPutOrPatchResultWithHeadersAsync(Resp
return this.getPutOrPatchResultAsync(response, resourceType, serviceCall, new ServiceCallback<T>() {
@Override
public void failure(Throwable t) {
callback.failure(t);
if (callback != null) {
callback.failure(t);
}
serviceCall.failure(t);
}

@Override
public void success(ServiceResponse<T> result) {
try {
callback.success(new ServiceResponseWithHeaders<>(
ServiceResponseWithHeaders<T, THeader> clientResponse = new ServiceResponseWithHeaders<>(
result.getBody(),
restClient().mapperAdapter().<THeader>deserialize(restClient().mapperAdapter().serialize(result.getResponse().headers()), headerType),
result.getResponse()
));
);
if (callback != null) {
callback.success(clientResponse);
}
serviceCall.success(clientResponse);
} catch (IOException e) {
failure(e);
}
Expand Down Expand Up @@ -331,7 +348,11 @@ public <T, THeader> ServiceResponseWithHeaders<T, THeader> getPostOrDeleteResult
*/
public <T> AsyncPollingTask<T> getPostOrDeleteResultAsync(Response<ResponseBody> response, Type resourceType, ServiceCall<T> serviceCall, ServiceCallback<T> callback) {
if (response == null) {
callback.failure(new ServiceException("response is null."));
CloudException t = new CloudException("response is null.");
if (callback != null) {
callback.failure(t);
}
serviceCall.failure(t);
return null;
}

Expand All @@ -351,15 +372,21 @@ public <T> AsyncPollingTask<T> getPostOrDeleteResultAsync(Response<ResponseBody>
responseBody.close();
}
} catch (Exception e) { /* ignore serialization errors on top of service errors */ }
callback.failure(exception);
if (callback != null) {
callback.failure(exception);
}
serviceCall.failure(exception);
return null;
}

PollingState<T> pollingState;
try {
pollingState = new PollingState<>(response, this.getLongRunningOperationRetryTimeout(), resourceType, restClient().mapperAdapter());
} catch (IOException e) {
callback.failure(e);
if (callback != null) {
callback.failure(e);
}
serviceCall.failure(e);
return null;
}

Expand Down Expand Up @@ -387,17 +414,24 @@ public <T, THeader> AsyncPollingTask<T> getPostOrDeleteResultWithHeadersAsync(Re
return this.getPostOrDeleteResultAsync(response, resourceType, serviceCall, new ServiceCallback<T>() {
@Override
public void failure(Throwable t) {
callback.failure(t);
if (callback != null) {
callback.failure(t);
}
serviceCall.failure(t);
}

@Override
public void success(ServiceResponse<T> result) {
try {
callback.success(new ServiceResponseWithHeaders<>(
ServiceResponseWithHeaders<T, THeader> clientResponse = new ServiceResponseWithHeaders<>(
result.getBody(),
restClient().mapperAdapter().<THeader>deserialize(restClient().mapperAdapter().serialize(result.getResponse().headers()), headerType),
result.getResponse()
));
);
if (callback != null) {
callback.success(clientResponse);
}
serviceCall.success(clientResponse);
} catch (IOException e) {
failure(e);
}
Expand Down Expand Up @@ -534,22 +568,34 @@ private <T> void updateStateFromGetResourceOperation(PollingState<T> pollingStat
*
* @param pollingState the polling state for the current operation.
* @param url the url to poll from
* @param serviceCall the future based service call
* @param callback the user callback to call when operation terminates.
* @param <T> the return type of the caller.
* @return the task describing the asynchronous polling.
*/
private <T> Call<ResponseBody> updateStateFromGetResourceOperationAsync(final PollingState<T> pollingState, String url, final ServiceCallback<T> callback) {
private <T> Call<ResponseBody> updateStateFromGetResourceOperationAsync(final PollingState<T> pollingState, String url, final ServiceCall<T> serviceCall, final ServiceCallback<T> callback) {
return pollAsync(url, new ServiceCallback<ResponseBody>() {
@Override
public void failure(Throwable t) {
callback.failure(t);
if (callback != null) {
callback.failure(t);
}
if (serviceCall != null) {
serviceCall.failure(t);
}
}

@Override
public void success(ServiceResponse<ResponseBody> result) {
try {
pollingState.updateFromResponseOnPutPatch(result.getResponse());
callback.success(new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse()));
ServiceResponse<T> clientResponse = new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse());
if (callback != null) {
callback.success(clientResponse);
}
if (serviceCall != null) {
serviceCall.success(clientResponse);
}
} catch (Throwable t) {
failure(t);
}
Expand Down Expand Up @@ -784,7 +830,10 @@ class PutPatchPollingTask<T> extends AsyncPollingTask<T> {
this.pollingCallback = new ServiceCallback<T>() {
@Override
public void failure(Throwable t) {
clientCallback.failure(t);
if (clientCallback != null) {
clientCallback.failure(t);
}
serviceCall.failure(t);
}

@Override
Expand All @@ -806,15 +855,23 @@ public void run() {
&& !pollingState.getLocationHeaderLink().isEmpty()) {
this.serviceCall.newCall(updateStateFromLocationHeaderOnPutAsync(pollingState, pollingCallback));
} else {
this.serviceCall.newCall(updateStateFromGetResourceOperationAsync(pollingState, url, pollingCallback));
this.serviceCall.newCall(updateStateFromGetResourceOperationAsync(pollingState, url, null, pollingCallback));
}
} else {
if (AzureAsyncOperation.SUCCESS_STATUS.equals(pollingState.getStatus()) && pollingState.getResource() == null) {
this.serviceCall.newCall(updateStateFromGetResourceOperationAsync(pollingState, url, clientCallback));
this.serviceCall.newCall(updateStateFromGetResourceOperationAsync(pollingState, url, serviceCall, clientCallback));
} else if (AzureAsyncOperation.getFailedStatuses().contains(pollingState.getStatus())) {
clientCallback.failure(new ServiceException("Async operation failed"));
ServiceException t = new ServiceException("Async operation failed");
if (clientCallback != null) {
clientCallback.failure(t);
}
serviceCall.failure(t);
} else {
clientCallback.success(new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse()));
ServiceResponse<T> clientResponse = new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse());
if (clientCallback != null) {
clientCallback.success(clientResponse);
}
serviceCall.success(clientResponse);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private class ParallelServiceCall extends ServiceCall<T> {
*/
public void cancel() {
for (ServiceCall<?> call : this.serviceCalls) {
call.cancel();
call.cancel(true);
}
}

Expand All @@ -153,7 +153,7 @@ public void cancel() {
*/
public boolean isCancelled() {
for (ServiceCall<?> call : this.serviceCalls) {
if (!call.isCanceled()) {
if (!call.isCancelled()) {
return false;
}
}
Expand Down
23 changes: 14 additions & 9 deletions client-runtime/src/main/java/com/microsoft/rest/ServiceCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,23 @@ public Call<?> getCall() {
}

/**
* Cancel the Retrofit call if possible.
* Cancel the Retrofit call if possible. Parameter
* 'mayInterruptIfRunning is ignored.
*
* @param mayInterruptIfRunning ignored
*/
public void cancel() {
call.cancel();
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
if (isCancelled()) {
return false;
} else {
call.cancel();
return true;
}
}

/**
* If the Retrofit call has been canceled.
*
* @return true if the call has been canceled; false otherwise.
*/
public boolean isCanceled() {
@Override
public boolean isCancelled() {
return call.isCanceled();
}

Expand Down

0 comments on commit 7c46138

Please sign in to comment.