Skip to content

Commit

Permalink
Renames RestResponse to RestResponseBase, RestContentResponse to Rest…
Browse files Browse the repository at this point in the history
…Response (Azure#570)

* Renaming RestResponse to RestResponseBase.

* Renaming RestContentResponse to RestResponse

* Renaming generic types in rest response models (TBody -> T, THeaders -> H)
  • Loading branch information
anuchandy authored Mar 7, 2019
1 parent 0ddb139 commit 307b1dd
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.microsoft.rest.v3.InvalidReturnTypeException;
import com.microsoft.rest.v3.http.ContentType;
import com.microsoft.rest.v3.RestException;
import com.microsoft.rest.v3.RestResponse;
import com.microsoft.rest.v3.RestResponseBase;
import com.microsoft.rest.v3.http.HttpPipeline;
import com.microsoft.rest.v3.http.HttpPipelineOptions;
import com.microsoft.rest.v3.serializer.SerializerAdapter;
Expand Down Expand Up @@ -419,7 +419,7 @@ public void SyncPutRequestWithUnexpectedResponseAndExceptionType() {
private interface Service10 {
@HEAD("anything")
@ExpectedResponses({200})
RestResponse<Void, Void> restResponseHead();
RestResponseBase<Void, Void> restResponseHead();


@HEAD("anything")
Expand All @@ -428,7 +428,7 @@ private interface Service10 {

@HEAD("anything")
@ExpectedResponses({200})
Mono<RestResponse<Void, Void>> restResponseHeadAsync();
Mono<RestResponseBase<Void, Void>> restResponseHeadAsync();

@HEAD("anything")
@ExpectedResponses({200})
Expand All @@ -437,7 +437,7 @@ private interface Service10 {

@Test
public void SyncRestResponseHeadRequest() {
RestResponse<?, ?> res = createService(Service10.class)
RestResponseBase<?, ?> res = createService(Service10.class)
.restResponseHead();
assertNull(res.body());
}
Expand All @@ -450,7 +450,7 @@ public void SyncVoidHeadRequest() {

@Test
public void AsyncRestResponseHeadRequest() {
RestResponse<?, ?> res = createService(Service10.class)
RestResponseBase<?, ?> res = createService(Service10.class)
.restResponseHeadAsync()
.block();

Expand Down

This file was deleted.

38 changes: 19 additions & 19 deletions client-runtime/src/main/java/com/microsoft/rest/v3/RestProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ public Mono<HttpDecodedResponse> ensureExpectedStatus(final HttpDecodedResponse
}

/**
* @param entityType the RestResponse subtype to get a constructor for.
* @return a Constructor which produces an instance of a RestResponse subtype.
* @param entityType the RestResponseBase subtype to get a constructor for.
* @return a Constructor which produces an instance of a RestResponseBase subtype.
*/
@SuppressWarnings("unchecked")
public Constructor<? extends RestResponse<?, ?>> getRestResponseConstructor(Type entityType) {
Class<? extends RestResponse<?, ?>> rawEntityType = (Class<? extends RestResponse<?, ?>>) TypeUtil.getRawClass(entityType);
public Constructor<? extends RestResponseBase<?, ?>> getRestResponseConstructor(Type entityType) {
Class<? extends RestResponseBase<?, ?>> rawEntityType = (Class<? extends RestResponseBase<?, ?>>) TypeUtil.getRawClass(entityType);
try {
Constructor<? extends RestResponse<?, ?>> ctor = null;
Constructor<? extends RestResponseBase<?, ?>> ctor = null;
for (Constructor<?> c : rawEntityType.getDeclaredConstructors()) {
// Generic constructor arguments turn into Object.
// Because some child class constructors have a more specific concrete type,
Expand All @@ -424,7 +424,7 @@ public Mono<HttpDecodedResponse> ensureExpectedStatus(final HttpDecodedResponse
&& c.getParameterTypes()[0].equals(HttpRequest.class)
&& c.getParameterTypes()[1].equals(Integer.TYPE)
&& c.getParameterTypes()[3].equals(Map.class)) {
ctor = (Constructor<? extends RestResponse<?, ?>>) c;
ctor = (Constructor<? extends RestResponseBase<?, ?>>) c;
}
}
if (ctor == null) {
Expand All @@ -441,29 +441,29 @@ private Mono<?> handleRestResponseReturnType(HttpDecodedResponse response, Swagg
//
try {
Mono<?> asyncResult;
if (TypeUtil.isTypeOrSubTypeOf(entityType, RestResponse.class)) {
// entityType = ? extends RestResponse<THeaders, TBody>
Constructor<? extends RestResponse<?, ?>> responseConstructor = getRestResponseConstructor(entityType);
if (TypeUtil.isTypeOrSubTypeOf(entityType, RestResponseBase.class)) {
// entityType = ? extends RestResponseBase<THeaders, TBody>
Constructor<? extends RestResponseBase<?, ?>> responseConstructor = getRestResponseConstructor(entityType);

Type[] deserializedTypes = TypeUtil.getTypeArguments(TypeUtil.getSuperType(entityType, RestResponse.class));
Type[] deserializedTypes = TypeUtil.getTypeArguments(TypeUtil.getSuperType(entityType, RestResponseBase.class));

HttpHeaders responseHeaders = response.sourceResponse().headers();
Object deserializedHeaders = response.decodedHeaders().block();

Type bodyType = deserializedTypes[1];
if (TypeUtil.isTypeOrSubTypeOf(bodyType, Void.class)) {
// entityType = ? extends RestResponse<THeaders, Void>
// entityType = ? extends RestResponseBase<THeaders, Void>
asyncResult = response.sourceResponse().body().ignoreElements()
.then(Mono.just(responseConstructor.newInstance(response.sourceResponse().request(), responseStatusCode, deserializedHeaders, responseHeaders.toMap(), null)));
} else {
final Map<String, String> rawHeaders = responseHeaders.toMap();
// entityType = ? extends RestResponse<THeaders, byte[]>,
// entityType = ? extends RestResponse<THeaders, Base64Url>
// entityType = ? extends RestResponse<THeaders, Flux<ByteBuf>>
// entityType = ? extends RestResponse<THeaders, Boolean>
// entityType = ? extends RestResponse<THeaders, VirtualMachine>
// entityType = ? extends RestResponseBase<THeaders, byte[]>,
// entityType = ? extends RestResponseBase<THeaders, Base64Url>
// entityType = ? extends RestResponseBase<THeaders, Flux<ByteBuf>>
// entityType = ? extends RestResponseBase<THeaders, Boolean>
// entityType = ? extends RestResponseBase<THeaders, VirtualMachine>
asyncResult = handleBodyReturnType(response, methodParser, bodyType)
.map((Function<Object, RestResponse<?, ?>>) bodyAsObject -> {
.map((Function<Object, RestResponseBase<?, ?>>) bodyAsObject -> {
try {
return responseConstructor.newInstance(response.sourceResponse().request(), responseStatusCode, deserializedHeaders, rawHeaders, bodyAsObject);
} catch (IllegalAccessException iae) {
Expand All @@ -474,7 +474,7 @@ private Mono<?> handleRestResponseReturnType(HttpDecodedResponse response, Swagg
throw reactor.core.Exceptions.propagate(ie);
}
})
.switchIfEmpty(Mono.defer((Supplier<Mono<RestResponse<?, ?>>>) () -> {
.switchIfEmpty(Mono.defer((Supplier<Mono<RestResponseBase<?, ?>>>) () -> {
try {
return Mono.just(responseConstructor.newInstance(response.sourceResponse().request(), responseStatusCode, deserializedHeaders, rawHeaders, null));
} catch (IllegalAccessException iae) {
Expand Down Expand Up @@ -551,7 +551,7 @@ public final Object handleRestReturnType(Mono<HttpDecodedResponse> asyncHttpDeco
// ProxyMethod ReturnType: Mono<Void>
result = asyncExpectedResponse.then();
} else {
// ProxyMethod ReturnType: Mono<? extends RestResponse<?, ?>>
// ProxyMethod ReturnType: Mono<? extends RestResponseBase<?, ?>>
result = asyncExpectedResponse.flatMap(response ->
handleRestResponseReturnType(response, methodParser, monoTypeParam));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,33 @@
import java.util.Map;

/**
* The response of a REST request.
* REST response with a strongly-typed content specified.
*
* @param <THeaders> The deserialized type of the response headers.
* @param <TBody> The deserialized type of the response body.
* @param <T> The deserialized type of the response content.
*/
public class RestResponse<THeaders, TBody> {
private final HttpRequest request;
private final int statusCode;
private final THeaders headers;
private final Map<String, String> rawHeaders;
private final TBody body;

public final class RestResponse<T> extends RestResponseBase<Void, T> {
/**
* Create RestResponse.
* Creates RestResponse.
*
* @param request the request which resulted in this response
* @param statusCode the status code of the HTTP response
* @param headers the deserialized headers of the HTTP response
* @param rawHeaders the raw headers of the HTTP response
* @param body the deserialized body
*/
public RestResponse(HttpRequest request, int statusCode, THeaders headers, Map<String, String> rawHeaders, TBody body) {
this.request = request;
this.statusCode = statusCode;
this.headers = headers;
this.rawHeaders = rawHeaders;
this.body = body;
}

/**
* @return the request which resulted in this RestResponse.
*/
public HttpRequest request() {
return request;
public RestResponse(HttpRequest request, int statusCode, Map<String, String> rawHeaders, T body) {
super(request, statusCode, null, rawHeaders, body);
}

/**
* @return the status code of the HTTP response.
*/
public int statusCode() {
return statusCode;
}

/**
* @return the deserialized headers of the HTTP response.
*/
public THeaders headers() {
return headers;
}

/**
* @return a Map containing the raw HTTP response headers.
*/
public Map<String, String> rawHeaders() {
return rawHeaders;
// Used for uniform reflective creation in RestProxy.
@SuppressWarnings("unused")
RestResponse(HttpRequest request, int statusCode, Void headers, Map<String, String> rawHeaders, T body) {
super(request, statusCode, headers, rawHeaders, body);
}

/**
* @return the deserialized body of the HTTP response.
* @return the deserialized body of the HTTP response
*/
public TBody body() {
return body;
public T body() {
return super.body();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* 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.rest.v3;

import com.microsoft.rest.v3.http.HttpRequest;

import java.util.Map;

/**
* The response of a REST request.
*
* @param <H> The deserialized type of the response headers.
* @param <T> The deserialized type of the response body.
*/
public class RestResponseBase<H, T> {
private final HttpRequest request;
private final int statusCode;
private final H headers;
private final Map<String, String> rawHeaders;
private final T body;

/**
* Create RestResponseBase.
*
* @param request the request which resulted in this response
* @param statusCode the status code of the HTTP response
* @param headers the deserialized headers of the HTTP response
* @param rawHeaders the raw headers of the HTTP response
* @param body the deserialized body
*/
public RestResponseBase(HttpRequest request, int statusCode, H headers, Map<String, String> rawHeaders, T body) {
this.request = request;
this.statusCode = statusCode;
this.headers = headers;
this.rawHeaders = rawHeaders;
this.body = body;
}

/**
* @return the request which resulted in this RestResponseBase.
*/
public HttpRequest request() {
return request;
}

/**
* @return the status code of the HTTP response.
*/
public int statusCode() {
return statusCode;
}

/**
* @return the deserialized headers of the HTTP response.
*/
public H headers() {
return headers;
}

/**
* @return a Map containing the raw HTTP response headers.
*/
public Map<String, String> rawHeaders() {
return rawHeaders;
}

/**
* @return the deserialized body of the HTTP response.
*/
public T body() {
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* REST response with a streaming content.
*/
public final class RestStreamResponse extends RestResponse<Void, Flux<ByteBuf>> implements Closeable {
public final class RestStreamResponse extends RestResponseBase<Void, Flux<ByteBuf>> implements Closeable {
/**
* Creates RestStreamResponse.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* REST response containing only a status code and raw headers.
*/
public final class RestVoidResponse extends RestResponse<Void, Void> {
public final class RestVoidResponse extends RestResponseBase<Void, Void> {
/**
* Creates RestVoidResponse.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,17 +476,17 @@ else if (TypeUtil.isTypeOrSubTypeOf(returnType, Mono.class) || TypeUtil.isTypeOr
final Type syncReturnType = asyncReturnType.getActualTypeArguments()[0];
if (TypeUtil.isTypeOrSubTypeOf(syncReturnType, Void.class)) {
result = false;
} else if (TypeUtil.isTypeOrSubTypeOf(syncReturnType, RestResponse.class)) {
result = restResponseTypeExpectsBody((ParameterizedType) TypeUtil.getSuperType(syncReturnType, RestResponse.class));
} else if (TypeUtil.isTypeOrSubTypeOf(syncReturnType, RestResponseBase.class)) {
result = restResponseBaseTypeExpectsBody((ParameterizedType) TypeUtil.getSuperType(syncReturnType, RestResponseBase.class));
}
} else if (TypeUtil.isTypeOrSubTypeOf(returnType, RestResponse.class)) {
result = restResponseTypeExpectsBody((ParameterizedType) TypeUtil.getSuperType(returnType, RestResponse.class));
} else if (TypeUtil.isTypeOrSubTypeOf(returnType, RestResponseBase.class)) {
result = restResponseBaseTypeExpectsBody((ParameterizedType) TypeUtil.getSuperType(returnType, RestResponseBase.class));
}

return result;
}

private static boolean restResponseTypeExpectsBody(ParameterizedType restResponseReturnType) {
private static boolean restResponseBaseTypeExpectsBody(ParameterizedType restResponseReturnType) {
boolean result = true;

final Type[] restResponseTypeArguments = restResponseReturnType.getActualTypeArguments();
Expand Down
Loading

0 comments on commit 307b1dd

Please sign in to comment.