forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Azure#51 from jianghaolu/09062016
Sync with SDK repo
- Loading branch information
Showing
17 changed files
with
617 additions
and
988 deletions.
There are no files selected for viewing
1,015 changes: 320 additions & 695 deletions
1,015
azure-client-runtime/src/main/java/com/microsoft/azure/AzureClient.java
Large diffs are not rendered by default.
Oops, something went wrong.
125 changes: 125 additions & 0 deletions
125
azure-client-runtime/src/main/java/com/microsoft/azure/AzureServiceCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/** | ||
* | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* | ||
*/ | ||
|
||
package com.microsoft.azure; | ||
|
||
import com.microsoft.rest.ServiceCall; | ||
import com.microsoft.rest.ServiceResponse; | ||
import com.microsoft.rest.ServiceResponseWithHeaders; | ||
import rx.Observable; | ||
import rx.Subscriber; | ||
import rx.functions.Func1; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An instance of this class provides access to the underlying REST call invocation. | ||
* This class wraps around the Retrofit Call object and allows updates to it in the | ||
* progress of a long running operation or a paging operation. | ||
* | ||
* @param <T> the type of the returning object | ||
*/ | ||
public final class AzureServiceCall<T> extends ServiceCall<T> { | ||
private AzureServiceCall() { | ||
} | ||
|
||
/** | ||
* Creates a ServiceCall from a paging operation. | ||
* | ||
* @param first the observable to the first page | ||
* @param next the observable to poll subsequent pages | ||
* @param callback the client-side callback | ||
* @param <E> the element type | ||
* @return the future based ServiceCall | ||
*/ | ||
public static <E> ServiceCall<List<E>> create(Observable<ServiceResponse<Page<E>>> first, final Func1<String, Observable<ServiceResponse<Page<E>>>> next, final ListOperationCallback<E> callback) { | ||
final AzureServiceCall<List<E>> serviceCall = new AzureServiceCall<>(); | ||
final PagingSubscriber<E> subscriber = new PagingSubscriber<>(serviceCall, next, callback); | ||
serviceCall.setSubscription(first | ||
.single() | ||
.subscribe(subscriber)); | ||
return serviceCall; | ||
} | ||
|
||
/** | ||
* Creates a ServiceCall from a paging operation that returns a header response. | ||
* | ||
* @param first the observable to the first page | ||
* @param next the observable to poll subsequent pages | ||
* @param callback the client-side callback | ||
* @param <E> the element type | ||
* @param <V> the header object type | ||
* @return the future based ServiceCall | ||
*/ | ||
public static <E, V> ServiceCall<List<E>> createWithHeaders(Observable<ServiceResponseWithHeaders<Page<E>, V>> first, final Func1<String, Observable<ServiceResponseWithHeaders<Page<E>, V>>> next, final ListOperationCallback<E> callback) { | ||
final AzureServiceCall<List<E>> serviceCall = new AzureServiceCall<>(); | ||
final PagingSubscriber<E> subscriber = new PagingSubscriber<>(serviceCall, new Func1<String, Observable<ServiceResponse<Page<E>>>>() { | ||
@Override | ||
public Observable<ServiceResponse<Page<E>>> call(String s) { | ||
return next.call(s) | ||
.map(new Func1<ServiceResponseWithHeaders<Page<E>, V>, ServiceResponse<Page<E>>>() { | ||
@Override | ||
public ServiceResponse<Page<E>> call(ServiceResponseWithHeaders<Page<E>, V> pageVServiceResponseWithHeaders) { | ||
return pageVServiceResponseWithHeaders; | ||
} | ||
}); | ||
} | ||
}, callback); | ||
serviceCall.setSubscription(first | ||
.single() | ||
.subscribe(subscriber)); | ||
return serviceCall; | ||
} | ||
|
||
/** | ||
* The subscriber that handles user callback and automatically subscribes to the next page. | ||
* | ||
* @param <E> the element type | ||
*/ | ||
private static class PagingSubscriber<E> extends Subscriber<ServiceResponse<Page<E>>> { | ||
private AzureServiceCall<List<E>> serviceCall; | ||
private Func1<String, Observable<ServiceResponse<Page<E>>>> next; | ||
private ListOperationCallback<E> callback; | ||
private ServiceResponse<Page<E>> lastResponse; | ||
|
||
PagingSubscriber(final AzureServiceCall<List<E>> serviceCall, final Func1<String, Observable<ServiceResponse<Page<E>>>> next, final ListOperationCallback<E> callback) { | ||
this.serviceCall = serviceCall; | ||
this.next = next; | ||
this.callback = callback; | ||
} | ||
|
||
@Override | ||
public void onCompleted() { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void onError(Throwable e) { | ||
serviceCall.setException(e); | ||
if (callback != null) { | ||
callback.failure(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNext(ServiceResponse<Page<E>> serviceResponse) { | ||
lastResponse = serviceResponse; | ||
ListOperationCallback.PagingBehavior behavior = ListOperationCallback.PagingBehavior.CONTINUE; | ||
if (callback != null) { | ||
behavior = callback.progress(serviceResponse.getBody().getItems()); | ||
if (behavior == ListOperationCallback.PagingBehavior.STOP || serviceResponse.getBody().getNextPageLink() == null) { | ||
callback.success(); | ||
} | ||
} | ||
if (behavior == ListOperationCallback.PagingBehavior.STOP || serviceResponse.getBody().getNextPageLink() == null) { | ||
serviceCall.set(new ServiceResponse<>(lastResponse.getBody().getItems(), lastResponse.getResponse())); | ||
} else { | ||
serviceCall.setSubscription(next.call(serviceResponse.getBody().getNextPageLink()).single().subscribe(this)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.