Skip to content

Commit

Permalink
Merge pull request #133 from jianghaolu/beta4hotfix
Browse files Browse the repository at this point in the history
allow posts with body
  • Loading branch information
jianghaolu authored Jan 5, 2017
2 parents c1c0ba8 + 2f66aab commit 86f1d6f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public Observable<PollingState<T>> call(Response<ResponseBody> response) {
* @param pollingState the polling state for the current operation.
* @param <T> the return type of the caller.
*/
private <T> Observable<PollingState<T>> updateStateFromAzureAsyncOperationHeaderAsync(final PollingState<T> pollingState) {
private <T> Observable<PollingState<T>> updateStateFromAzureAsyncOperationHeaderOnPutAsync(final PollingState<T> pollingState) {
return pollAsync(pollingState.getAzureAsyncOperationHeaderLink())
.flatMap(new Func1<Response<ResponseBody>, Observable<PollingState<T>>>() {
@Override
Expand All @@ -471,7 +471,7 @@ public Observable<PollingState<T>> call(Response<ResponseBody> response) {
bodyString = response.body().string();
body = restClient().mapperAdapter().deserialize(bodyString, AzureAsyncOperation.class);
} catch (IOException e) {
// null body will be handlded later
// null body will be handled later
} finally {
response.body().close();
}
Expand All @@ -491,6 +491,51 @@ public Observable<PollingState<T>> call(Response<ResponseBody> response) {
});
}

/**
* Polls from the 'Azure-AsyncOperation' header and updates the polling
* state with the polling response.
*
* @param pollingState the polling state for the current operation.
* @param <T> the return type of the caller.
*/
private <T> Observable<PollingState<T>> updateStateFromAzureAsyncOperationHeaderOnPostOrDeleteAsync(final PollingState<T> pollingState) {
return pollAsync(pollingState.getAzureAsyncOperationHeaderLink())
.flatMap(new Func1<Response<ResponseBody>, Observable<PollingState<T>>>() {
@Override
public Observable<PollingState<T>> call(Response<ResponseBody> response) {
AzureAsyncOperation body = null;
String bodyString = "";
if (response.body() != null) {
try {
bodyString = response.body().string();
body = restClient().mapperAdapter().deserialize(bodyString, AzureAsyncOperation.class);
} catch (IOException e) {
// null body will be handled later
} finally {
response.body().close();
}
}

if (body == null || body.getStatus() == null) {
CloudException exception = new CloudException("polling response does not contain a valid body: " + bodyString);
exception.setResponse(response);
return Observable.error(exception);
}

pollingState.setStatus(body.getStatus());
pollingState.setResponse(response);
T resource = null;
try {
resource = restClient().mapperAdapter().deserialize(bodyString, pollingState.getResourceType());
} catch (IOException e) {
// Ignore and let resource be null
}
pollingState.setResource(resource);
return Observable.just(pollingState);
}
});
}

/**
* Polls from the URL provided.
*
Expand Down Expand Up @@ -555,7 +600,7 @@ private CloudException createExceptionFromResponse(Response<ResponseBody> respon
private <T> Observable<PollingState<T>> putOrPatchPollingDispatcher(PollingState<T> pollingState, String url) {
if (pollingState.getAzureAsyncOperationHeaderLink() != null
&& !pollingState.getAzureAsyncOperationHeaderLink().isEmpty()) {
return updateStateFromAzureAsyncOperationHeaderAsync(pollingState);
return updateStateFromAzureAsyncOperationHeaderOnPutAsync(pollingState);
} else if (pollingState.getLocationHeaderLink() != null
&& !pollingState.getLocationHeaderLink().isEmpty()) {
return updateStateFromLocationHeaderOnPutAsync(pollingState);
Expand All @@ -567,7 +612,7 @@ private <T> Observable<PollingState<T>> putOrPatchPollingDispatcher(PollingState
private <T> Observable<PollingState<T>> postOrDeletePollingDispatcher(PollingState<T> pollingState) {
if (pollingState.getAzureAsyncOperationHeaderLink() != null
&& !pollingState.getAzureAsyncOperationHeaderLink().isEmpty()) {
return updateStateFromAzureAsyncOperationHeaderAsync(pollingState);
return updateStateFromAzureAsyncOperationHeaderOnPostOrDeleteAsync(pollingState);
} else if (pollingState.getLocationHeaderLink() != null
&& !pollingState.getLocationHeaderLink().isEmpty()) {
return updateStateFromLocationHeaderOnPostOrDeleteAsync(pollingState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.microsoft.rest.serializer.JacksonMapperAdapter;
import okhttp3.ResponseBody;
import retrofit2.Response;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
* An instance of this class defines the state of a long running operation.
*
Expand Down Expand Up @@ -162,6 +161,12 @@ public String getStatus() {
return status;
}

/**
* @return the resource type
*/
public Type getResourceType() {
return resourceType;
}

/**
* Sets the polling status.
Expand Down

0 comments on commit 86f1d6f

Please sign in to comment.