From ef8e9d82ff5ca1a09f0fd6cbeca2f95e5781727d Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Wed, 4 Oct 2017 13:19:57 -0700 Subject: [PATCH 1/5] Make RestProxy sync scenarios go through async scenarios (one code path) --- .../java/com/microsoft/azure/AzureProxy.java | 88 +++++++------------ .../java/com/microsoft/rest/RestProxy.java | 73 ++++++--------- 2 files changed, 61 insertions(+), 100 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureProxy.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureProxy.java index 897144a79e8a..e0b2bd84ba09 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureProxy.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureProxy.java @@ -17,13 +17,11 @@ import com.microsoft.rest.http.HttpClient; import com.microsoft.rest.http.HttpRequest; import com.microsoft.rest.http.HttpResponse; -import rx.Completable; import rx.Observable; import rx.Single; import rx.functions.Func0; import rx.functions.Func1; -import java.io.IOException; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; @@ -104,62 +102,14 @@ public static A create(Class swaggerInterface, String baseUrl, final Http } @Override - protected Object handleSyncHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, SwaggerMethodParser methodParser) throws IOException, InterruptedException { + protected Object handleAsyncHttpResponse(final HttpRequest httpRequest, Single asyncHttpResponse, final SwaggerMethodParser methodParser, final Type returnType) { final SerializerAdapter serializer = serializer(); - final PollStrategy pollStrategy = createPollStrategy(httpRequest, httpResponse, serializer); - if (pollStrategy != null) { - while (!pollStrategy.isDone()) { - pollStrategy.delay(); + Object result; - final HttpRequest pollRequest = pollStrategy.createPollRequest(); - httpResponse = sendHttpRequest(pollRequest); - - pollStrategy.updateFrom(httpResponse); - } - } - - return super.handleSyncHttpResponse(httpRequest, httpResponse, methodParser); - } - - @Override - protected Object handleAsyncHttpResponse(final HttpRequest httpRequest, Single asyncHttpResponse, final SwaggerMethodParser methodParser) { - final SerializerAdapter serializer = serializer(); - - Object result = null; - - final Type returnType = methodParser.returnType(); final TypeToken returnTypeToken = TypeToken.of(returnType); - if (returnTypeToken.isSubtypeOf(Completable.class) || returnTypeToken.isSubtypeOf(Single.class)) { - asyncHttpResponse = asyncHttpResponse - .flatMap(new Func1>() { - @Override - public Single call(HttpResponse httpResponse) { - final PollStrategy pollStrategy = createPollStrategy(httpRequest, httpResponse, serializer); - - Single result; - if (pollStrategy == null || pollStrategy.isDone()) { - result = Single.just(httpResponse); - } - else { - result = sendPollRequestWithDelay(pollStrategy) - .repeat() - .takeUntil(new Func1() { - @Override - public Boolean call(HttpResponse ignored) { - return pollStrategy.isDone(); - } - }) - .last() - .toSingle(); - } - return result; - } - }); - result = super.handleAsyncHttpResponse(httpRequest, asyncHttpResponse, methodParser); - } - else if (returnTypeToken.isSubtypeOf(Observable.class)) { + if (returnTypeToken.isSubtypeOf(Observable.class)) { final Type operationStatusType = ((ParameterizedType) returnType).getActualTypeArguments()[0]; final TypeToken operationStatusTypeToken = TypeToken.of(operationStatusType); if (!operationStatusTypeToken.isSubtypeOf(OperationStatus.class)) { @@ -206,6 +156,34 @@ public Boolean call(OperationStatus operationStatus) { }); } } + else { + asyncHttpResponse = asyncHttpResponse + .flatMap(new Func1>() { + @Override + public Single call(HttpResponse httpResponse) { + final PollStrategy pollStrategy = createPollStrategy(httpRequest, httpResponse, serializer); + + Single result; + if (pollStrategy == null || pollStrategy.isDone()) { + result = Single.just(httpResponse); + } + else { + result = sendPollRequestWithDelay(pollStrategy) + .repeat() + .takeUntil(new Func1() { + @Override + public Boolean call(HttpResponse ignored) { + return pollStrategy.isDone(); + } + }) + .last() + .toSingle(); + } + return result; + } + }); + result = super.handleAsyncHttpResponse(httpRequest, asyncHttpResponse, methodParser, returnType); + } return result; } @@ -245,15 +223,13 @@ private static PollStrategy createPollStrategy(HttpRequest httpRequest, HttpResp private Observable> toCompletedOperationStatusObservable(String provisioningState, HttpRequest httpRequest, HttpResponse httpResponse, SwaggerMethodParser methodParser, Type operationStatusResultType) { Observable> result; try { - final Object resultObject = super.handleSyncHttpResponse(httpRequest, httpResponse, methodParser, operationStatusResultType); + final Object resultObject = super.handleAsyncHttpResponse(httpRequest, Single.just(httpResponse), methodParser, operationStatusResultType); result = Observable.just(new OperationStatus<>(resultObject, provisioningState)); } catch (RestException e) { if (ProvisioningState.SUCCEEDED.equals(provisioningState)) { provisioningState = ProvisioningState.FAILED; } result = Observable.just(new OperationStatus<>(e, provisioningState)); - } catch (IOException e) { - result = Observable.error(e); } return result; } diff --git a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java index dd644aa43f35..fb1177bfb71f 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/RestProxy.java @@ -15,6 +15,7 @@ import com.microsoft.rest.http.HttpResponse; import com.microsoft.rest.http.UrlBuilder; import rx.Completable; +import rx.Observable; import rx.Single; import rx.exceptions.Exceptions; import rx.functions.Func1; @@ -71,16 +72,6 @@ protected SerializerAdapter serializer() { return serializer; } - /** - * Send the provided request and block until the response is received. - * @param request The HTTP request to send. - * @return The HTTP response received. - * @throws IOException On network issues. - */ - public HttpResponse sendHttpRequest(HttpRequest request) throws IOException { - return httpClient.sendRequest(request); - } - /** * Send the provided request asynchronously, applying any request policies provided to the HttpClient instance. * @param request The HTTP request to send. @@ -96,17 +87,9 @@ public Object invoke(Object proxy, final Method method, Object[] args) throws IO final HttpRequest request = createHttpRequest(methodParser, args); - Object result; - if (methodParser.isAsync()) { - final Single asyncResponse = sendHttpRequestAsync(request); - result = handleAsyncHttpResponse(request, asyncResponse, methodParser); - } - else { - final HttpResponse response = sendHttpRequest(request); - result = handleSyncHttpResponse(request, response, methodParser); - } + final Single asyncResponse = sendHttpRequestAsync(request); - return result; + return handleAsyncHttpResponse(request, asyncResponse, methodParser, methodParser.returnType()); } /** @@ -181,16 +164,6 @@ else if (bodyContentObject instanceof String) { return request; } - protected Object handleSyncHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, SwaggerMethodParser methodParser) throws IOException, InterruptedException { - final Type returnType = methodParser.returnType(); - return handleSyncHttpResponse(httpRequest, httpResponse, methodParser, returnType); - } - - protected Object handleSyncHttpResponse(HttpRequest httpRequest, HttpResponse httpResponse, SwaggerMethodParser methodParser, Type returnType) throws IOException { - ensureExpectedStatus(httpResponse, methodParser).toBlocking().value(); - return toProxyReturnValue(httpResponse, methodParser, returnType).toBlocking().value(); - } - private Exception instantiateUnexpectedException(SwaggerMethodParser methodParser, HttpResponse response, String responseContent) { final int responseStatusCode = response.statusCode(); final Class exceptionType = methodParser.exceptionType(); @@ -226,18 +199,18 @@ public HttpResponse call(String responseBody) { } }); } else { - asyncResult = Single.just(response); + asyncResult = Single.just(response); } return asyncResult; } - private Single toProxyReturnValue(HttpResponse response, SwaggerMethodParser methodParser, final Type entityType) { + private Single toProxyReturnValueAsync(HttpResponse response, String httpMethod, final Type entityType) { final TypeToken entityTypeToken = TypeToken.of(entityType); final Single asyncResult; if (entityTypeToken.isSubtypeOf(void.class) || entityTypeToken.isSubtypeOf(Void.class)) { asyncResult = Single.just(null); - } else if (methodParser.httpMethod().equalsIgnoreCase("HEAD") + } else if (httpMethod.equalsIgnoreCase("HEAD") && (entityTypeToken.isSubtypeOf(boolean.class) || entityTypeToken.isSubtypeOf(Boolean.class))) { boolean isSuccess = response.statusCode() / 100 == 2; asyncResult = Single.just(isSuccess); @@ -265,34 +238,46 @@ public Single call(String responseBodyString) { return asyncResult; } - protected Object handleAsyncHttpResponse(HttpRequest httpRequest, Single asyncHttpResponse, final SwaggerMethodParser methodParser) { + protected Object handleAsyncHttpResponse(HttpRequest httpRequest, Single asyncHttpResponse, final SwaggerMethodParser methodParser, final Type returnType) { Object result; - final Type returnType = methodParser.returnType(); final TypeToken returnTypeToken = TypeToken.of(returnType); - final Single asyncExpectedResponse = asyncHttpResponse.flatMap(new Func1>() { - @Override - public Single call(HttpResponse response) { - return ensureExpectedStatus(response, methodParser); - } - }); + final Single asyncExpectedResponse = asyncHttpResponse + .flatMap(new Func1>() { + @Override + public Single call(HttpResponse response) { + return ensureExpectedStatus(response, methodParser); + } + }); if (returnTypeToken.isSubtypeOf(Completable.class)) { result = Completable.fromSingle(asyncExpectedResponse); } else if (returnTypeToken.isSubtypeOf(Single.class)) { - final Type singleTypeParam = ((ParameterizedType) methodParser.returnType()).getActualTypeArguments()[0]; + final Type singleTypeParam = ((ParameterizedType) returnType).getActualTypeArguments()[0]; result = asyncExpectedResponse.flatMap(new Func1>() { @Override public Single call(HttpResponse response) { - return toProxyReturnValue(response, methodParser, singleTypeParam); + return toProxyReturnValueAsync(response, methodParser.httpMethod(), singleTypeParam); } }); } - else { + else if (returnTypeToken.isSubtypeOf(Observable.class)) { throw new InvalidReturnTypeException("RestProxy does not support swagger interface methods (such as " + methodParser.fullyQualifiedMethodName() + "()) with a return type of " + returnType.toString()); } + else { + // The return value is not an asynchronous type (Completable, Single, or Observable), so + // block the deserialization until a value is received. + result = asyncExpectedResponse + .flatMap(new Func1>() { + @Override + public Single call(HttpResponse httpResponse) { + return toProxyReturnValueAsync(httpResponse, methodParser.httpMethod(), returnType); + } + }) + .toBlocking().value(); + } return result; } From bf5db22421998f46409efe9ae4a950f6e74f172c Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Wed, 4 Oct 2017 13:25:59 -0700 Subject: [PATCH 2/5] Remove unused SwaggerMethodParser.isAsync() method --- .../java/com/microsoft/rest/SwaggerMethodParser.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java b/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java index e49421ce15d2..6e71385eabab 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java +++ b/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java @@ -335,15 +335,6 @@ public Type returnType() { return returnType; } - /** - * Get whether or not this parser's swagger method is asynchronous. - * @return Whether or not this parser's swagger method is asynchronous. - */ - public boolean isAsync() { - final TypeToken returnTypeToken = TypeToken.of(returnType); - return returnTypeToken.isSubtypeOf(Completable.class) || returnTypeToken.isSubtypeOf(Single.class) || returnTypeToken.isSubtypeOf(Observable.class); - } - /** * Set both the HTTP method and the path that will be used to complete the Swagger method's * request. From 8671e65800ffa3e133e3d43aa34ac67c493d41a4 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Wed, 4 Oct 2017 13:37:22 -0700 Subject: [PATCH 3/5] Remove more unused methods and fix checkstyle errors --- .../AzureAsyncOperationPollStrategy.java | 5 ---- .../microsoft/azure/LocationPollStrategy.java | 17 ++---------- .../com/microsoft/azure/PollStrategy.java | 26 ------------------- .../microsoft/rest/SwaggerMethodParser.java | 4 --- 4 files changed, 2 insertions(+), 50 deletions(-) diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureAsyncOperationPollStrategy.java b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureAsyncOperationPollStrategy.java index e232186837a1..aa5d1275fa37 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/AzureAsyncOperationPollStrategy.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/AzureAsyncOperationPollStrategy.java @@ -67,11 +67,6 @@ else if (pollingSucceeded) { return new HttpRequest(fullyQualifiedMethodName, "GET", pollUrl); } - @Override - public void updateFrom(HttpResponse httpPollResponse) throws IOException { - updateFromAsync(httpPollResponse).toBlocking().value(); - } - @Override public Single updateFromAsync(final HttpResponse httpPollResponse) { updateDelayInMillisecondsFrom(httpPollResponse); diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/LocationPollStrategy.java b/azure-client-runtime/src/main/java/com/microsoft/azure/LocationPollStrategy.java index 5019ca61cfd2..9c2db17736ef 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/LocationPollStrategy.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/LocationPollStrategy.java @@ -10,8 +10,6 @@ import com.microsoft.rest.http.HttpResponse; import rx.Single; -import java.io.IOException; - /** * A PollStrategy type that uses the Location header value to check the status of a long running * operation. @@ -40,7 +38,7 @@ public HttpRequest createPollRequest() { } @Override - public void updateFrom(HttpResponse httpPollResponse) throws IOException { + public Single updateFromAsync(HttpResponse httpPollResponse) { final int httpStatusCode = httpPollResponse.statusCode(); if (httpStatusCode == 202) { locationUrl = httpPollResponse.headerValue(HEADER_NAME); @@ -49,18 +47,7 @@ public void updateFrom(HttpResponse httpPollResponse) throws IOException { else { done = true; } - } - - @Override - public Single updateFromAsync(HttpResponse httpPollResponse) { - Single result; - try { - updateFrom(httpPollResponse); - result = Single.just(httpPollResponse); - } catch (IOException e) { - result = Single.error(e); - } - return result; + return Single.just(httpPollResponse); } @Override diff --git a/azure-client-runtime/src/main/java/com/microsoft/azure/PollStrategy.java b/azure-client-runtime/src/main/java/com/microsoft/azure/PollStrategy.java index f77972f60042..a6d88f82e9d3 100644 --- a/azure-client-runtime/src/main/java/com/microsoft/azure/PollStrategy.java +++ b/azure-client-runtime/src/main/java/com/microsoft/azure/PollStrategy.java @@ -10,7 +10,6 @@ import com.microsoft.rest.http.HttpResponse; import rx.Single; -import java.io.IOException; import java.util.concurrent.TimeUnit; /** @@ -25,14 +24,6 @@ abstract class PollStrategy { this.delayInMilliseconds = delayInMilliseconds; } - /** - * Get the number of milliseconds to delay before sending the next poll request. - * @return The number of milliseconds to delay. - */ - final long delayInMilliseconds() { - return delayInMilliseconds; - } - /** * Set the delay in milliseconds to 0. */ @@ -55,17 +46,6 @@ final void updateDelayInMillisecondsFrom(HttpResponse httpPollResponse) { } } - /** - * If this PollStrategy has a retryAfterSeconds value, delay (and block) the current thread for - * the number of seconds that are in the retryAfterSeconds value. If this PollStrategy doesn't - * have a retryAfterSeconds value, then just return. - */ - void delay() throws InterruptedException { - if (delayInMilliseconds > 0) { - Thread.sleep(delayInMilliseconds); - } - } - /** * If this OperationStatus has a retryAfterSeconds value, return an Single that is delayed by the * number of seconds that are in the retryAfterSeconds value. If this OperationStatus doesn't have @@ -103,12 +83,6 @@ protected void setProvisioningState(String provisioningState) { */ abstract HttpRequest createPollRequest(); - /** - * Update the status of this PollStrategy from the provided HTTP poll response. - * @param httpPollResponse The response of the most recent poll request. - */ - abstract void updateFrom(HttpResponse httpPollResponse) throws IOException; - /** * Update the status of this PollStrategy from the provided HTTP poll response. * @param httpPollResponse The response of the most recent poll request. diff --git a/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java b/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java index 6e71385eabab..00eb71b29801 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java +++ b/client-runtime/src/main/java/com/microsoft/rest/SwaggerMethodParser.java @@ -8,7 +8,6 @@ import com.google.common.escape.Escaper; import com.google.common.net.UrlEscapers; -import com.google.common.reflect.TypeToken; import com.microsoft.rest.annotations.BodyParam; import com.microsoft.rest.annotations.DELETE; import com.microsoft.rest.annotations.ExpectedResponses; @@ -25,9 +24,6 @@ import com.microsoft.rest.annotations.QueryParam; import com.microsoft.rest.http.HttpHeader; import com.microsoft.rest.http.HttpHeaders; -import rx.Completable; -import rx.Observable; -import rx.Single; import java.lang.annotation.Annotation; import java.lang.reflect.Method; From 0e3ca7e1923b63eb4eb7d2efbd2f82d71b293778 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Wed, 4 Oct 2017 13:43:04 -0700 Subject: [PATCH 4/5] Remove HttpClient.sendRequest() --- .../java/com/microsoft/rest/http/HttpClient.java | 12 ------------ .../java/com/microsoft/rest/CredentialsTests.java | 4 ++-- .../com/microsoft/rest/RequestIdPolicyTests.java | 6 +++--- .../java/com/microsoft/rest/RetryPolicyTests.java | 8 ++++---- .../test/java/com/microsoft/rest/UserAgentTests.java | 6 +++--- 5 files changed, 12 insertions(+), 24 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java index 614b1d6ed3eb..13971ff10aee 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpClient.java @@ -9,7 +9,6 @@ import com.microsoft.rest.policy.RequestPolicy; import rx.Single; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -52,17 +51,6 @@ public final Single sendRequestAsync(HttpRequest request) { return next.sendAsync(request); } - /** - * Send the provided request and block until the response is received. - * @param request The HTTP request to send. - * @return The HTTP response received. - * @throws IOException On network issues. - */ - public final HttpResponse sendRequest(HttpRequest request) throws IOException { - final Single asyncResult = sendRequestAsync(request); - return asyncResult.toBlocking().value(); - } - /** * Send the provided request asynchronously through the concrete HTTP client implementation. * @param request The HTTP request to send. diff --git a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java index 8f94b42058c8..5d1f18957aec 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/CredentialsTests.java @@ -41,7 +41,7 @@ public Single sendAsync(HttpRequest request) { HttpClient client = new MockHttpClient(Arrays.asList(new CredentialsPolicy.Factory(credentials), auditorFactory)); HttpRequest request = new HttpRequest("basicCredentialsTest", "GET", "http://localhost"); - client.sendRequest(request); + client.sendRequestAsync(request).toBlocking().value(); } @Test @@ -65,6 +65,6 @@ public Single sendAsync(HttpRequest request) { HttpClient client = new MockHttpClient(Arrays.asList(new CredentialsPolicy.Factory(credentials), auditorFactory)); HttpRequest request = new HttpRequest("basicCredentialsTest", "GET", "http://localhost"); - client.sendRequest(request); + client.sendRequestAsync(request).toBlocking().value(); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/RequestIdPolicyTests.java b/client-runtime/src/test/java/com/microsoft/rest/RequestIdPolicyTests.java index 6b8f0d346d15..91b044430b96 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RequestIdPolicyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RequestIdPolicyTests.java @@ -85,8 +85,8 @@ public Single sendRequestInternalAsync(HttpRequest request) { } }; - client.sendRequest(new HttpRequest("newRequestIdForEachCall", "GET", "http://localhost/")); - client.sendRequest(new HttpRequest("newRequestIdForEachCall", "GET", "http://localhost/")); + client.sendRequestAsync(new HttpRequest("newRequestIdForEachCall", "GET", "http://localhost/")).toBlocking().value(); + client.sendRequestAsync(new HttpRequest("newRequestIdForEachCall", "GET", "http://localhost/")).toBlocking().value(); } @Test @@ -111,6 +111,6 @@ public Single sendRequestInternalAsync(HttpRequest request) { } }; - client.sendRequest(new HttpRequest("sameRequestIdForRetry", "GET", "http://localhost/")); + client.sendRequestAsync(new HttpRequest("sameRequestIdForRetry", "GET", "http://localhost/")).toBlocking().value(); } } diff --git a/client-runtime/src/test/java/com/microsoft/rest/RetryPolicyTests.java b/client-runtime/src/test/java/com/microsoft/rest/RetryPolicyTests.java index f2c45bbb9cf0..a236d3fc74e2 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/RetryPolicyTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/RetryPolicyTests.java @@ -31,11 +31,11 @@ protected Single sendRequestInternalAsync(HttpRequest request) { } }; - HttpResponse response = client.sendRequest( + HttpResponse response = client.sendRequestAsync( new HttpRequest( "exponentialRetryEndOn501", "GET", - "http://localhost/")); + "http://localhost/")).toBlocking().value(); Assert.assertEquals(501, response.statusCode()); } @@ -53,11 +53,11 @@ public Single sendRequestInternalAsync(HttpRequest request) { } }; - HttpResponse response = client.sendRequest( + HttpResponse response = client.sendRequestAsync( new HttpRequest( "exponentialRetryMax", "GET", - "http://localhost/")); + "http://localhost/")).toBlocking().value(); Assert.assertEquals(500, response.statusCode()); } diff --git a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java index 7b47b3aa891b..1228ea3aa05c 100644 --- a/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java +++ b/client-runtime/src/test/java/com/microsoft/rest/UserAgentTests.java @@ -33,9 +33,9 @@ public Single sendRequestInternalAsync(HttpRequest request) { } }; - HttpResponse response = client.sendRequest(new HttpRequest( + HttpResponse response = client.sendRequestAsync(new HttpRequest( "defaultUserAgentTests", - "GET", "http://localhost")); + "GET", "http://localhost")).toBlocking().value(); Assert.assertEquals(200, response.statusCode()); } @@ -51,7 +51,7 @@ public Single sendRequestInternalAsync(HttpRequest request) { } }; - HttpResponse response = client.sendRequest(new HttpRequest("customUserAgentTests", "GET", "http://localhost")); + HttpResponse response = client.sendRequestAsync(new HttpRequest("customUserAgentTests", "GET", "http://localhost")).toBlocking().value(); Assert.assertEquals(200, response.statusCode()); } } From 449ff5d2fff7c336ed062fa0076f4e213ed0cfc1 Mon Sep 17 00:00:00 2001 From: Dan Schulte Date: Wed, 4 Oct 2017 13:49:22 -0700 Subject: [PATCH 5/5] Remove sync methods from HttpResponse --- .../com/microsoft/rest/http/HttpResponse.java | 33 ------------------- .../microsoft/rest/policy/LoggingPolicy.java | 4 +-- 2 files changed, 2 insertions(+), 35 deletions(-) diff --git a/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java b/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java index 2857e59e5aa4..736570a2a1aa 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java +++ b/client-runtime/src/main/java/com/microsoft/rest/http/HttpResponse.java @@ -8,7 +8,6 @@ import rx.Single; -import java.io.IOException; import java.io.InputStream; /** @@ -36,16 +35,6 @@ public abstract class HttpResponse { */ public abstract HttpHeaders headers(); - /** - * Get this response object's body as an InputStream. If this response object doesn't have a - * body, then null will be returned. - * @return This response object's body as an InputStream. If this response object doesn't have a - * body, then null will be returned. - */ - public InputStream bodyAsInputStream() { - return bodyAsInputStreamAsync().toBlocking().value(); - } - /** * Get this response object's body as an InputStream. If this response object doesn't have a * body, then null will be returned. @@ -54,17 +43,6 @@ public InputStream bodyAsInputStream() { */ public abstract Single bodyAsInputStreamAsync(); - /** - * Get this response object's body as a byte[]. If this response object doesn't have a body, - * then null will be returned. - * @return This response object's body as a byte[]. If this response object doesn't have a body, - * then null will be returned. - * @throws IOException On network error. - */ - public byte[] bodyAsByteArray() throws IOException { - return bodyAsByteArrayAsync().toBlocking().value(); - } - /** * Get this response object's body as a byte[]. If this response object doesn't have a body, * then null will be returned. @@ -73,17 +51,6 @@ public byte[] bodyAsByteArray() throws IOException { */ public abstract Single bodyAsByteArrayAsync(); - /** - * Get this response object's body as a string. If this response object doesn't have a body, - * then null will be returned. - * @return This response object's body as a string. If this response object doesn't have a body, - * then null will be returned. - * @throws IOException On network or serialization error. - */ - public String bodyAsString() throws IOException { - return bodyAsStringAsync().toBlocking().value(); - } - /** * Get this response object's body as a string. If this response object doesn't have a body, * then null will be returned. diff --git a/client-runtime/src/main/java/com/microsoft/rest/policy/LoggingPolicy.java b/client-runtime/src/main/java/com/microsoft/rest/policy/LoggingPolicy.java index afa7a55c7eb7..8208edd1250b 100644 --- a/client-runtime/src/main/java/com/microsoft/rest/policy/LoggingPolicy.java +++ b/client-runtime/src/main/java/com/microsoft/rest/policy/LoggingPolicy.java @@ -140,8 +140,8 @@ private void logResponse(Logger logger, HttpResponse response, String url, long String contentTypeHeader = response.headerValue("Content-Type"); if ("application/json".equals(contentTypeHeader)) { try { - log(logger, response.bodyAsString()); - } catch (IOException e) { + log(logger, response.bodyAsStringAsync().toBlocking().value()); + } catch (Throwable e) { log(logger, "Error occurred when logging body: " + e.getMessage()); } } else {