Skip to content

Commit

Permalink
Support Future based ServiceCall in LRO & Paging
Browse files Browse the repository at this point in the history
  • Loading branch information
jianghaolu committed Aug 3, 2016
1 parent b4a6382 commit 38b9c93
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public <T, THeader> ServiceResponseWithHeaders<T, THeader> getPutOrPatchResultWi
* @param callback the user callback to call when operation terminates.
* @return the task describing the asynchronous polling.
*/
public <T> AsyncPollingTask<T> getPutOrPatchResultAsync(Response<ResponseBody> response, Type resourceType, ServiceCall serviceCall, ServiceCallback<T> callback) {
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."));
return null;
Expand Down Expand Up @@ -211,7 +211,7 @@ public <T> AsyncPollingTask<T> getPutOrPatchResultAsync(Response<ResponseBody> r
* @param callback the user callback to call when operation terminates.
* @return the task describing the asynchronous polling.
*/
public <T, THeader> AsyncPollingTask<T> getPutOrPatchResultWithHeadersAsync(Response<ResponseBody> response, Type resourceType, final Class<THeader> headerType, final ServiceCall serviceCall, final ServiceCallback<T> callback) {
public <T, THeader> AsyncPollingTask<T> getPutOrPatchResultWithHeadersAsync(Response<ResponseBody> response, Type resourceType, final Class<THeader> headerType, final ServiceCall<T> serviceCall, final ServiceCallback<T> callback) {
return this.getPutOrPatchResultAsync(response, resourceType, serviceCall, new ServiceCallback<T>() {
@Override
public void failure(Throwable t) {
Expand Down Expand Up @@ -329,7 +329,7 @@ public <T, THeader> ServiceResponseWithHeaders<T, THeader> getPostOrDeleteResult
* @param callback the user callback to call when operation terminates.
* @return the task describing the asynchronous polling.
*/
public <T> AsyncPollingTask<T> getPostOrDeleteResultAsync(Response<ResponseBody> response, Type resourceType, ServiceCall serviceCall, ServiceCallback<T> callback) {
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."));
return null;
Expand Down Expand Up @@ -383,7 +383,7 @@ public <T> AsyncPollingTask<T> getPostOrDeleteResultAsync(Response<ResponseBody>
* @param callback the user callback to call when operation terminates.
* @return the task describing the asynchronous polling.
*/
public <T, THeader> AsyncPollingTask<T> getPostOrDeleteResultWithHeadersAsync(Response<ResponseBody> response, Type resourceType, final Class<THeader> headerType, final ServiceCall serviceCall, final ServiceCallback<T> callback) {
public <T, THeader> AsyncPollingTask<T> getPostOrDeleteResultWithHeadersAsync(Response<ResponseBody> response, Type resourceType, final Class<THeader> headerType, final ServiceCall<T> serviceCall, final ServiceCallback<T> callback) {
return this.getPostOrDeleteResultAsync(response, resourceType, serviceCall, new ServiceCallback<T>() {
@Override
public void failure(Throwable t) {
Expand Down Expand Up @@ -689,7 +689,7 @@ private Call<ResponseBody> pollAsync(String url, final ServiceCallback<ResponseB
}
AsyncService service = restClient().retrofit().create(AsyncService.class);
Call<ResponseBody> call = service.get(endpoint.getFile(), serviceClientUserAgent);
call.enqueue(new ServiceResponseCallback<ResponseBody>(callback) {
call.enqueue(new ServiceResponseCallback<ResponseBody>(null, callback) {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Expand Down Expand Up @@ -750,7 +750,7 @@ private interface AsyncService {
*/
abstract class AsyncPollingTask<T> implements Runnable {
/** The {@link Call} object from Retrofit. */
protected ServiceCall serviceCall;
protected ServiceCall<T> serviceCall;
/** The polling state for the current operation. */
protected PollingState<T> pollingState;
/** The callback used for asynchronous polling. */
Expand All @@ -776,7 +776,7 @@ class PutPatchPollingTask<T> extends AsyncPollingTask<T> {
* @param serviceCall the ServiceCall object tracking Retrofit calls.
* @param clientCallback the client callback to call when a terminal status is hit.
*/
PutPatchPollingTask(final PollingState<T> pollingState, final String url, final ServiceCall serviceCall, final ServiceCallback<T> clientCallback) {
PutPatchPollingTask(final PollingState<T> pollingState, final String url, final ServiceCall<T> serviceCall, final ServiceCallback<T> clientCallback) {
this.serviceCall = serviceCall;
this.pollingState = pollingState;
this.url = url;
Expand Down Expand Up @@ -833,7 +833,7 @@ class PostDeletePollingTask<T> extends AsyncPollingTask<T> {
* @param serviceCall the ServiceCall object tracking Retrofit calls.
* @param clientCallback the client callback to call when a terminal status is hit.
*/
PostDeletePollingTask(final PollingState<T> pollingState, final ServiceCall serviceCall, final ServiceCallback<T> clientCallback) {
PostDeletePollingTask(final PollingState<T> pollingState, final ServiceCall<T> serviceCall, final ServiceCallback<T> clientCallback) {
this.serviceCall = serviceCall;
this.pollingState = pollingState;
this.clientCallback = clientCallback;
Expand Down Expand Up @@ -861,14 +861,20 @@ public void run() {
&& !pollingState.getLocationHeaderLink().isEmpty()) {
updateStateFromLocationHeaderOnPostOrDeleteAsync(pollingState, pollingCallback);
} else {
pollingCallback.failure(new ServiceException("No header in response"));
ServiceException serviceException = new ServiceException("No async header in response");
pollingCallback.failure(serviceException);
serviceCall.failure(serviceException);
}
} else {
// Check if operation failed
if (AzureAsyncOperation.getFailedStatuses().contains(pollingState.getStatus())) {
clientCallback.failure(new ServiceException("Async operation failed"));
ServiceException serviceException = new ServiceException("Async operation failed");
clientCallback.failure(serviceException);
serviceCall.failure(serviceException);
} else {
clientCallback.success(new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse()));
ServiceResponse<T> serviceResponse = new ServiceResponse<>(pollingState.getResource(), pollingState.getResponse());
clientCallback.success(serviceResponse);
serviceCall.success(serviceResponse);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* @param <T> the response body type
*/
public abstract class ServiceResponseCallback<T> implements Callback<ResponseBody> {
/**
* The client service call object.
*/
private ServiceCall<T> serviceCall;

/**
* The client callback.
*/
Expand All @@ -26,14 +31,21 @@ public abstract class ServiceResponseCallback<T> implements Callback<ResponseBod
/**
* Creates an instance of ServiceResponseCallback.
*
* @param serviceCall the client service call to call on a terminal state.
* @param serviceCallback the client callback to call on a terminal state.
*/
public ServiceResponseCallback(ServiceCallback<T> serviceCallback) {
public ServiceResponseCallback(ServiceCall<T> serviceCall, ServiceCallback<T> serviceCallback) {
this.serviceCall = serviceCall;
this.serviceCallback = serviceCallback;
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
serviceCallback.failure(new ServiceException(t));
if (serviceCallback != null) {
serviceCallback.failure(t);
}
if (serviceCall != null) {
serviceCall.failure(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* @param <T> the response body type
*/
public abstract class ServiceResponseEmptyCallback<T> implements Callback<Void> {
/**
* The client service call object.
*/
private ServiceCall<T> serviceCall;

/**
* The client callback.
*/
Expand All @@ -25,14 +30,21 @@ public abstract class ServiceResponseEmptyCallback<T> implements Callback<Void>
/**
* Creates an instance of ServiceResponseCallback.
*
* @param serviceCall the client service call to call on a terminal state.
* @param serviceCallback the client callback to call on a terminal state.
*/
public ServiceResponseEmptyCallback(ServiceCallback<T> serviceCallback) {
public ServiceResponseEmptyCallback(ServiceCall<T> serviceCall, ServiceCallback<T> serviceCallback) {
this.serviceCall = serviceCall;
this.serviceCallback = serviceCallback;
}

@Override
public void onFailure(Call<Void> call, Throwable t) {
serviceCallback.failure(new ServiceException(t));
if (serviceCallback != null) {
serviceCallback.failure(t);
}
if (serviceCall != null) {
serviceCall.failure(t);
}
}
}

0 comments on commit 38b9c93

Please sign in to comment.