Skip to content

Commit

Permalink
make a http response for beginFoo method (Azure#17106)
Browse files Browse the repository at this point in the history
  • Loading branch information
weidongxu-microsoft authored Nov 3, 2020
1 parent 55690a7 commit bfd056a
Showing 1 changed file with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.management.exception.ManagementError;
Expand Down Expand Up @@ -32,6 +33,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Objects;
Expand Down Expand Up @@ -95,7 +97,12 @@ public SyncPoller<Void, T> getSyncPoller() {
Function<PollResponse<PollResult<InnerT>>, ManagementException> errorOperation = response -> {
String errorMessage;
ManagementError managementError = null;
HttpResponse errorResponse = null;
PollResult.Error lroError = response.getValue().getError();
if (response.getValue().getError() != null) {
errorResponse = new HttpResponseImpl(lroError.getResponseStatusCode(),
lroError.getResponseHeaders(), lroError.getResponseBody());

errorMessage = response.getValue().getError().getMessage();
String errorBody = response.getValue().getError().getResponseBody();
if (errorBody != null) {
Expand All @@ -120,7 +127,7 @@ public SyncPoller<Void, T> getSyncPoller() {
// fallback to default ManagementError
managementError = new ManagementError(response.getStatus().toString(), errorMessage);
}
return new ManagementException(errorMessage, null, managementError);
return new ManagementException(errorMessage, errorResponse, managementError);
};

syncPoller = new SyncPollerImpl<InnerT, T>(this.getPollerFlux().getSyncPoller(),
Expand Down Expand Up @@ -309,6 +316,54 @@ private static class ProvisioningState {
static final String CANCELED = "Canceled";
}

private static class HttpResponseImpl extends HttpResponse {
private final int statusCode;
private final byte[] responseBody;
private final HttpHeaders httpHeaders;

HttpResponseImpl(int statusCode, HttpHeaders httpHeaders, String responseBody) {
super(null);
this.statusCode = statusCode;
this.httpHeaders = httpHeaders;
this.responseBody = responseBody.getBytes(StandardCharsets.UTF_8);
}

@Override
public int getStatusCode() {
return statusCode;
}

@Override
public String getHeaderValue(String s) {
return httpHeaders.getValue(s);
}

@Override
public HttpHeaders getHeaders() {
return httpHeaders;
}

@Override
public Flux<ByteBuffer> getBody() {
return Flux.just(ByteBuffer.wrap(responseBody));
}

@Override
public Mono<byte[]> getBodyAsByteArray() {
return Mono.just(responseBody);
}

@Override
public Mono<String> getBodyAsString() {
return Mono.just(new String(responseBody, StandardCharsets.UTF_8));
}

@Override
public Mono<String> getBodyAsString(Charset charset) {
return Mono.just(new String(responseBody, charset));
}
}

public static <T, InnerT> Accepted<T> newAccepted(
ClientLogger logger,
HttpPipeline httpPipeline,
Expand Down

0 comments on commit bfd056a

Please sign in to comment.