Skip to content

Commit

Permalink
Use HttpMethod instead of String (Azure#342)
Browse files Browse the repository at this point in the history
* Add HttpMethod class

* Use HttpMethod instead of strings
  • Loading branch information
Dan Schulte authored Jan 9, 2018
1 parent 56e004f commit 0689b3e
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.microsoft.azure.v2.credentials.http.MockHttpClient;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpRequest;
import com.microsoft.rest.v2.util.FlowableUtil;
import org.junit.Test;
Expand All @@ -22,7 +23,7 @@ public void refreshToken() throws IOException {

assertEquals(1, httpClient.requests().size());
final HttpRequest request = httpClient.requests().get(0);
assertEquals("POST", request.httpMethod());
assertEquals(HttpMethod.POST, request.httpMethod());
assertEquals("com.microsoft.azure.v2.credentials.RefreshTokenClient$RefreshTokenService.refreshToken", request.callerMethod());
assertEquals("http://my.base.url/mockTenant/oauth2/token", request.url());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.microsoft.rest.v2.RestProxy;
import com.microsoft.rest.v2.SwaggerMethodParser;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpRequest;
import com.microsoft.rest.v2.http.HttpResponse;
import io.reactivex.Single;
Expand Down Expand Up @@ -61,7 +62,7 @@ else if (pollingSucceeded) {
throw new IllegalStateException("Polling is completed and did not succeed. Cannot create a polling request.");
}

return new HttpRequest(fullyQualifiedMethodName(), "GET", pollUrl);
return new HttpRequest(fullyQualifiedMethodName(), HttpMethod.GET, pollUrl);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.microsoft.azure.v2.serializer.AzureJacksonAdapter;
import com.microsoft.rest.v2.credentials.ServiceClientCredentials;
import com.microsoft.rest.v2.http.HttpClient;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpPipeline;
import com.microsoft.rest.v2.http.HttpPipelineBuilder;
import com.microsoft.rest.v2.http.NettyClient;
Expand Down Expand Up @@ -286,7 +287,7 @@ public Single<? extends PollStrategy> apply(HttpResponse response) {
final Long parsedDelayInMilliseconds = PollStrategy.delayInMillisecondsFrom(originalHttpResponse);
final long delayInMilliseconds = parsedDelayInMilliseconds != null ? parsedDelayInMilliseconds : AzureProxy.defaultDelayInMilliseconds();

final String originalHttpRequestMethod = originalHttpRequest.httpMethod();
final HttpMethod originalHttpRequestMethod = originalHttpRequest.httpMethod();

PollStrategy pollStrategy = null;
if (httpStatusCode == 200) {
Expand All @@ -298,7 +299,7 @@ public Single<? extends PollStrategy> apply(HttpResponse response) {
result = createProvisioningStateOrCompletedPollStrategy(originalHttpRequest, originalHttpResponse, methodParser, delayInMilliseconds);
}
}
else if (originalHttpRequestMethod.equalsIgnoreCase("PUT") || originalHttpRequestMethod.equalsIgnoreCase("PATCH")) {
else if (originalHttpRequestMethod == HttpMethod.PUT || originalHttpRequestMethod == HttpMethod.PATCH) {
if (httpStatusCode == 201) {
pollStrategy = AzureAsyncOperationPollStrategy.tryToCreate(AzureProxy.this, methodParser, originalHttpRequest, originalHttpResponse, delayInMilliseconds);
if (pollStrategy == null) {
Expand Down Expand Up @@ -341,10 +342,10 @@ else if (originalHttpRequestMethod.equalsIgnoreCase("PUT") || originalHttpReques
private Single<PollStrategy> createProvisioningStateOrCompletedPollStrategy(final HttpRequest httpRequest, HttpResponse httpResponse, final SwaggerMethodParser methodParser, final long delayInMilliseconds) {
Single<PollStrategy> result;

final String httpRequestMethod = httpRequest.httpMethod();
if (httpRequestMethod.equalsIgnoreCase("DELETE")
|| httpRequestMethod.equalsIgnoreCase("GET")
|| httpRequestMethod.equalsIgnoreCase("HEAD")
final HttpMethod httpRequestMethod = httpRequest.httpMethod();
if (httpRequestMethod == HttpMethod.DELETE
|| httpRequestMethod == HttpMethod.GET
|| httpRequestMethod == HttpMethod.HEAD
|| !methodParser.expectsResponseBody()) {
result = Single.<PollStrategy>just(new CompletedPollStrategy(AzureProxy.this, methodParser, httpResponse));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.microsoft.rest.v2.RestProxy;
import com.microsoft.rest.v2.SwaggerMethodParser;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpRequest;
import com.microsoft.rest.v2.http.HttpResponse;
import io.reactivex.Single;
Expand Down Expand Up @@ -38,7 +39,7 @@ private LocationPollStrategy(RestProxy restProxy, SwaggerMethodParser methodPars

@Override
public HttpRequest createPollRequest() {
return new HttpRequest(fullyQualifiedMethodName(), "GET", locationUrl);
return new HttpRequest(fullyQualifiedMethodName(), HttpMethod.GET, locationUrl);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.microsoft.rest.v2.RestProxy;
import com.microsoft.rest.v2.SwaggerMethodParser;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpRequest;
import com.microsoft.rest.v2.http.HttpResponse;
import io.reactivex.Single;
Expand All @@ -31,7 +32,7 @@ public class ProvisioningStatePollStrategy extends PollStrategy {

@Override
HttpRequest createPollRequest() {
return new HttpRequest(originalRequest.callerMethod(), "GET", originalRequest.url());
return new HttpRequest(originalRequest.callerMethod(), HttpMethod.GET, originalRequest.url());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.microsoft.rest.v2.http.HttpClient;
import com.microsoft.rest.v2.http.HttpHeader;
import com.microsoft.rest.v2.http.HttpHeaders;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpRequest;
import com.microsoft.rest.v2.http.HttpResponse;
import com.microsoft.rest.v2.util.FlowableUtil;
Expand Down Expand Up @@ -124,7 +125,7 @@ else if (requestPathLower.startsWith("/status/")) {
}
}
else if (requestHost.equalsIgnoreCase("mock.azure.com")) {
if (request.httpMethod().equalsIgnoreCase("GET")) {
if (request.httpMethod() == HttpMethod.GET) {
if (requestPathLower.contains("/mockprovider/mockresources/")) {
++getRequests;
--pollsRemaining;
Expand Down Expand Up @@ -171,7 +172,7 @@ else if (pollType.equalsIgnoreCase(LocationPollStrategy.HEADER_NAME)) {
}
}
}
else if (request.httpMethod().equalsIgnoreCase("PUT")) {
else if (request.httpMethod() == HttpMethod.PUT) {
++createRequests;

final Map<String, String> requestQueryMap = queryToMap(requestUrl.getQuery());
Expand Down Expand Up @@ -232,7 +233,7 @@ else if (pollType.equalsIgnoreCase(LocationPollStrategy.HEADER_NAME)) {
}
}
}
else if (request.httpMethod().equalsIgnoreCase("DELETE")) {
else if (request.httpMethod() == HttpMethod.DELETE) {
++deleteRequests;

final Map<String,String> requestQueryMap = queryToMap(requestUrl.getQuery());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.microsoft.rest.v2.http.FlowableHttpRequestBody;
import com.microsoft.rest.v2.http.HttpHeader;
import com.microsoft.rest.v2.http.HttpHeaders;
import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpPipeline;
import com.microsoft.rest.v2.http.HttpPipelineBuilder;
import com.microsoft.rest.v2.http.HttpRequest;
Expand Down Expand Up @@ -471,11 +472,11 @@ private boolean isFlowableByteArray(TypeToken entityTypeToken) {
private Maybe<?> handleBodyReturnTypeAsync(final HttpResponse response, final SwaggerMethodParser methodParser, final Type entityType) {
final TypeToken entityTypeToken = TypeToken.of(entityType);
final int responseStatusCode = response.statusCode();
final String httpMethod = methodParser.httpMethod();
final HttpMethod httpMethod = methodParser.httpMethod();
final Type returnValueWireType = methodParser.returnValueWireType();

final Maybe<?> asyncResult;
if (httpMethod.equalsIgnoreCase("HEAD")
if (httpMethod == HttpMethod.HEAD
&& (entityTypeToken.isSubtypeOf(boolean.class) || entityTypeToken.isSubtypeOf(Boolean.class))) {
boolean isSuccess = (responseStatusCode / 100) == 2;
asyncResult = Maybe.just(isSuccess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.microsoft.rest.v2.annotations.QueryParam;
import com.microsoft.rest.v2.http.HttpHeader;
import com.microsoft.rest.v2.http.HttpHeaders;
import com.microsoft.rest.v2.http.HttpMethod;
import io.reactivex.Observable;
import io.reactivex.Single;

Expand All @@ -43,7 +44,7 @@
public class SwaggerMethodParser {
private final String rawHost;
private final String fullyQualifiedMethodName;
private String httpMethod;
private HttpMethod httpMethod;
private String relativePath;
private final List<Substitution> hostSubstitutions = new ArrayList<>();
private final List<Substitution> pathSubstitutions = new ArrayList<>();
Expand Down Expand Up @@ -73,22 +74,22 @@ public class SwaggerMethodParser {
fullyQualifiedMethodName = swaggerInterface.getName() + "." + swaggerMethod.getName();

if (swaggerMethod.isAnnotationPresent(GET.class)) {
setHttpMethodAndRelativePath("GET", swaggerMethod.getAnnotation(GET.class).value());
setHttpMethodAndRelativePath(HttpMethod.GET, swaggerMethod.getAnnotation(GET.class).value());
}
else if (swaggerMethod.isAnnotationPresent(PUT.class)) {
setHttpMethodAndRelativePath("PUT", swaggerMethod.getAnnotation(PUT.class).value());
setHttpMethodAndRelativePath(HttpMethod.PUT, swaggerMethod.getAnnotation(PUT.class).value());
}
else if (swaggerMethod.isAnnotationPresent(HEAD.class)) {
setHttpMethodAndRelativePath("HEAD", swaggerMethod.getAnnotation(HEAD.class).value());
setHttpMethodAndRelativePath(HttpMethod.HEAD, swaggerMethod.getAnnotation(HEAD.class).value());
}
else if (swaggerMethod.isAnnotationPresent(DELETE.class)) {
setHttpMethodAndRelativePath("DELETE", swaggerMethod.getAnnotation(DELETE.class).value());
setHttpMethodAndRelativePath(HttpMethod.DELETE, swaggerMethod.getAnnotation(DELETE.class).value());
}
else if (swaggerMethod.isAnnotationPresent(POST.class)) {
setHttpMethodAndRelativePath("POST", swaggerMethod.getAnnotation(POST.class).value());
setHttpMethodAndRelativePath(HttpMethod.POST, swaggerMethod.getAnnotation(POST.class).value());
}
else if (swaggerMethod.isAnnotationPresent(PATCH.class)) {
setHttpMethodAndRelativePath("PATCH", swaggerMethod.getAnnotation(PATCH.class).value());
setHttpMethodAndRelativePath(HttpMethod.PATCH, swaggerMethod.getAnnotation(PATCH.class).value());
}
else {
final ArrayList<Class<? extends Annotation>> requiredAnnotationOptions = new ArrayList<>();
Expand Down Expand Up @@ -197,7 +198,7 @@ public String fullyQualifiedMethodName() {
* Get the HTTP method that will be used to complete the Swagger method's request.
* @return The HTTP method that will be used to complete the Swagger method's request.
*/
public String httpMethod() {
public HttpMethod httpMethod() {
return httpMethod;
}

Expand Down Expand Up @@ -431,7 +432,7 @@ private static boolean restResponseTypeExpectsBody(ParameterizedType restRespons
* @param relativePath The path in the URL that will be used to complete the Swagger method's
* request.
*/
private void setHttpMethodAndRelativePath(String httpMethod, String relativePath) {
private void setHttpMethodAndRelativePath(HttpMethod httpMethod, String relativePath) {
this.httpMethod = httpMethod;
this.relativePath = relativePath;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* 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.v2.http;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.microsoft.rest.v2.ExpandableStringEnum;

import java.util.Collection;

/**
* The different HTTP methods of a HttpRequest.
*/
public class HttpMethod extends ExpandableStringEnum<HttpMethod> {
/**
* The HTTP GET method.
*/
public static final HttpMethod GET = fromString("GET");

/**
* The HTTP PUT method.
*/
public static final HttpMethod PUT = fromString("PUT");

/**
* The HTTP POST method.
*/
public static final HttpMethod POST = fromString("POST");

/**
* The HTTP PATCH method.
*/
public static final HttpMethod PATCH = fromString("PATCH");

/**
* The HTTP DELETE method.
*/
public static final HttpMethod DELETE = fromString("DELETE");

/**
* The HTTP HEAD method.
*/
public static final HttpMethod HEAD = fromString("HEAD");

/**
* Creates or finds a HttpMethod from its string representation.
*
* @param name a name to look for.
* @return the corresponding HttpMethod.
*/
@JsonCreator
public static HttpMethod fromString(String name) {
return fromString(name, HttpMethod.class);
}

/**
* @return known HttpMethod values.
*/
public static Collection<HttpMethod> values() {
return values(HttpMethod.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class HttpRequest {
private String callerMethod;
private String httpMethod;
private HttpMethod httpMethod;
private String url;
private HttpHeaders headers;
private HttpRequestBody body;
Expand All @@ -28,7 +28,7 @@ public class HttpRequest {
* @param httpMethod The HTTP method to use with this request.
* @param url The URL where this HTTP request should be sent to.
*/
public HttpRequest(String callerMethod, String httpMethod, String url) {
public HttpRequest(String callerMethod, HttpMethod httpMethod, String url) {
this.callerMethod = callerMethod;
this.httpMethod = httpMethod;
this.url = url;
Expand All @@ -44,7 +44,7 @@ public HttpRequest(String callerMethod, String httpMethod, String url) {
* @param headers The HTTP headers to use with this request.
* @param body The body of this HTTP request.
*/
public HttpRequest(String callerMethod, String httpMethod, String url, HttpHeaders headers, HttpRequestBody body) {
public HttpRequest(String callerMethod, HttpMethod httpMethod, String url, HttpHeaders headers, HttpRequestBody body) {
this.callerMethod = callerMethod;
this.httpMethod = httpMethod;
this.url = url;
Expand Down Expand Up @@ -74,7 +74,7 @@ public HttpRequest withCallerMethod(String callerMethod) {
* Get the HTTP method that this request will use.
* @return The HTTP method that this request will use.
*/
public String httpMethod() {
public HttpMethod httpMethod() {
return httpMethod;
}

Expand All @@ -83,7 +83,7 @@ public String httpMethod() {
* @param httpMethod The HTTP method to use, e.g. "GET".
* @return This HttpRequest so that multiple operations can be chained together.
*/
public HttpRequest withHttpMethod(String httpMethod) {
public HttpRequest withHttpMethod(HttpMethod httpMethod) {
this.httpMethod = httpMethod;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public boolean isDisposed() {
}
});

if (request.httpMethod().equalsIgnoreCase("HEAD")) {
if (request.httpMethod() == com.microsoft.rest.v2.http.HttpMethod.HEAD) {
// Use HttpClientCodec for HEAD operations
if (channel.pipeline().get("HttpClientCodec") == null) {
channel.pipeline().remove(HttpRequestEncoder.class);
Expand All @@ -261,7 +261,7 @@ public boolean isDisposed() {
}

final DefaultHttpRequest raw = new DefaultHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.valueOf(request.httpMethod()),
HttpMethod.valueOf(request.httpMethod().toString()),
request.url());

for (HttpHeader header : request.headers()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.microsoft.rest.v2.credentials.BasicAuthenticationCredentials;
import com.microsoft.rest.v2.credentials.TokenCredentials;

import com.microsoft.rest.v2.http.HttpMethod;
import com.microsoft.rest.v2.http.HttpPipeline;
import com.microsoft.rest.v2.policy.CredentialsPolicy;
import com.microsoft.rest.v2.http.HttpRequest;
Expand Down Expand Up @@ -46,7 +47,7 @@ public Single<HttpResponse> sendAsync(HttpRequest request) {
new CredentialsPolicy.Factory(credentials),
auditorFactory);

HttpRequest request = new HttpRequest("basicCredentialsTest", "GET", "http://localhost");
HttpRequest request = new HttpRequest("basicCredentialsTest", HttpMethod.GET, "http://localhost");
pipeline.sendRequestAsync(request).blockingGet();
}

Expand All @@ -73,7 +74,7 @@ public Single<HttpResponse> sendAsync(HttpRequest request) {
new CredentialsPolicy.Factory(credentials),
auditorFactory);

HttpRequest request = new HttpRequest("basicCredentialsTest", "GET", "http://localhost");
HttpRequest request = new HttpRequest("basicCredentialsTest", HttpMethod.GET, "http://localhost");
pipeline.sendRequestAsync(request).blockingGet();
}
}
Loading

0 comments on commit 0689b3e

Please sign in to comment.