Skip to content

Commit 5993c5e

Browse files
committed
Refactored Request Method Attribute on Requests
* Added `HttpMethod` enum that represents the supported HTTP methods replacing String handling. * Deprecated `Request#method()` in favor of `Request#httpMethod()`
1 parent 806149c commit 5993c5e

File tree

9 files changed

+70
-28
lines changed

9 files changed

+70
-28
lines changed

core/src/main/java/feign/Client.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Default(SSLSocketFactory sslContextFactory, HostnameVerifier hostnameVeri
6565
@Override
6666
public Response execute(Request request, Options options) throws IOException {
6767
HttpURLConnection connection = convertAndSend(request, options);
68-
return convertResponse(connection, request).toBuilder().request(request).build();
68+
return convertResponse(connection, request);
6969
}
7070

7171
HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
@@ -84,7 +84,7 @@ HttpURLConnection convertAndSend(Request request, Options options) throws IOExce
8484
connection.setReadTimeout(options.readTimeoutMillis());
8585
connection.setAllowUserInteraction(false);
8686
connection.setInstanceFollowRedirects(options.isFollowRedirects());
87-
connection.setRequestMethod(request.method());
87+
connection.setRequestMethod(request.httpMethod().name());
8888

8989
Collection<String> contentEncodingValues = request.headers().get(CONTENT_ENCODING);
9090
boolean gzipEncodedRequest =

core/src/main/java/feign/FeignException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public int status() {
4343

4444
static FeignException errorReading(Request request, Response ignored, IOException cause) {
4545
return new FeignException(
46-
format("%s reading %s %s", cause.getMessage(), request.method(), request.url()),
46+
format("%s reading %s %s", cause.getMessage(), request.httpMethod().name(), request.url()),
4747
cause);
4848
}
4949

@@ -61,8 +61,8 @@ public static FeignException errorStatus(String methodKey, Response response) {
6161

6262
static FeignException errorExecuting(Request request, IOException cause) {
6363
return new RetryableException(
64-
format("%s executing %s %s", cause.getMessage(), request.method(), request.url()),
65-
request.method(),
64+
format("%s executing %s %s", cause.getMessage(), request.httpMethod(), request.url()),
65+
request.httpMethod(),
6666
cause,
6767
null);
6868
}

core/src/main/java/feign/Logger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected static String methodTag(String configKey) {
4444
protected abstract void log(String configKey, String format, Object... args);
4545

4646
protected void logRequest(String configKey, Level logLevel, Request request) {
47-
log(configKey, "---> %s %s HTTP/1.1", request.method(), request.url());
47+
log(configKey, "---> %s %s HTTP/1.1", request.httpMethod().name(), request.url());
4848
if (logLevel.ordinal() >= Level.HEADERS.ordinal()) {
4949

5050
for (String field : request.headers().keySet()) {

core/src/main/java/feign/Request.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,40 +25,77 @@
2525
*/
2626
public final class Request {
2727

28-
public enum Methods {
28+
public enum HttpMethod {
2929
GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH
3030
}
3131

3232
/**
3333
* No parameters can be null except {@code body} and {@code charset}. All parameters must be
3434
* effectively immutable, via safe copies, not mutating or otherwise.
35+
*
36+
* @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)}
3537
*/
3638
public static Request create(String method,
3739
String url,
3840
Map<String, Collection<String>> headers,
3941
byte[] body,
4042
Charset charset) {
41-
return new Request(method, url, headers, body, charset);
43+
checkNotNull(method, "httpMethod of %s", method);
44+
HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
45+
return create(httpMethod, url, headers, body, charset);
4246
}
4347

44-
private final Methods method;
48+
/**
49+
* Builds a Request. All parameters must be effectively immutable, via safe copies.
50+
*
51+
* @param httpMethod for the request.
52+
* @param url for the request.
53+
* @param headers to include.
54+
* @param body of the request, can be {@literal null}
55+
* @param charset of the request, can be {@literal null}
56+
* @return a Request
57+
*/
58+
public static Request create(HttpMethod httpMethod,
59+
String url,
60+
Map<String, Collection<String>> headers,
61+
byte[] body,
62+
Charset charset) {
63+
return new Request(httpMethod, url, headers, body, charset);
64+
65+
}
66+
67+
private final HttpMethod httpMethod;
4568
private final String url;
4669
private final Map<String, Collection<String>> headers;
4770
private final byte[] body;
4871
private final Charset charset;
4972

50-
Request(String method, String url, Map<String, Collection<String>> headers, byte[] body,
73+
Request(HttpMethod method, String url, Map<String, Collection<String>> headers, byte[] body,
5174
Charset charset) {
52-
this.method = Methods.valueOf(checkNotNull(method, "method of %s", url));
75+
this.httpMethod = checkNotNull(method, "httpMethod of %s", method.name());
5376
this.url = checkNotNull(url, "url");
5477
this.headers = checkNotNull(headers, "headers of %s %s", method, url);
5578
this.body = body; // nullable
5679
this.charset = charset; // nullable
5780
}
5881

59-
/* Method to invoke on the server. */
82+
/**
83+
* Http Method for this request.
84+
*
85+
* @return the HttpMethod string
86+
* @deprecated @see {@link #httpMethod()}
87+
*/
6088
public String method() {
61-
return method.name();
89+
return httpMethod.name();
90+
}
91+
92+
/**
93+
* Http Method for the request.
94+
*
95+
* @return the HttpMethod.
96+
*/
97+
public HttpMethod httpMethod() {
98+
return this.httpMethod;
6299
}
63100

64101
/* Fully resolved URL including query. */
@@ -93,7 +130,7 @@ public byte[] body() {
93130
@Override
94131
public String toString() {
95132
StringBuilder builder = new StringBuilder();
96-
builder.append(method).append(' ').append(url).append(" HTTP/1.1\n");
133+
builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n");
97134
for (String field : headers.keySet()) {
98135
for (String value : valuesOrEmpty(headers, field)) {
99136
builder.append(field).append(": ").append(value).append('\n');

core/src/main/java/feign/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public Builder request(Request request) {
128128

129129
/* don't keep the body, we don't want to tie up memory on large requests */
130130
this.request = Request.create(
131-
request.method(), request.url(), request.headers(), null, request.charset());
131+
request.httpMethod(), request.url(), request.headers(), null, request.charset());
132132
return this;
133133
}
134134

core/src/main/java/feign/RetryableException.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package feign;
1515

16+
import feign.Request.HttpMethod;
1617
import java.util.Date;
1718

1819
/**
@@ -24,23 +25,23 @@ public class RetryableException extends FeignException {
2425
private static final long serialVersionUID = 1L;
2526

2627
private final Long retryAfter;
27-
private final String method;
28+
private final HttpMethod httpMethod;
2829

2930
/**
3031
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
3132
*/
32-
public RetryableException(String message, String method, Throwable cause, Date retryAfter) {
33+
public RetryableException(String message, HttpMethod httpMethod, Throwable cause, Date retryAfter) {
3334
super(message, cause);
34-
this.method = method;
35+
this.httpMethod = httpMethod;
3536
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
3637
}
3738

3839
/**
3940
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header.
4041
*/
41-
public RetryableException(String message, String method, Date retryAfter) {
42+
public RetryableException(String message, HttpMethod httpMethod, Date retryAfter) {
4243
super(message);
43-
this.method = method;
44+
this.httpMethod = httpMethod;
4445
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
4546
}
4647

@@ -52,7 +53,7 @@ public Date retryAfter() {
5253
return retryAfter != null ? new Date(retryAfter) : null;
5354
}
5455

55-
public String method() {
56-
return this.method;
56+
public HttpMethod method() {
57+
return this.httpMethod;
5758
}
5859
}

core/src/main/java/feign/codec/ErrorDecoder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public Exception decode(String methodKey, Response response) {
9595
if (retryAfter != null) {
9696
return new RetryableException(
9797
exception.getMessage(),
98-
response.request().method(),
98+
response.request().httpMethod(),
9999
exception,
100100
retryAfter);
101101
}

core/src/test/java/feign/FeignTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.google.gson.Gson;
1717
import com.google.gson.reflect.TypeToken;
18+
import feign.Request.HttpMethod;
1819
import okhttp3.mockwebserver.MockResponse;
1920
import okhttp3.mockwebserver.SocketPolicy;
2021
import okhttp3.mockwebserver.MockWebServer;
@@ -483,7 +484,7 @@ public void retryableExceptionInDecoder() throws Exception {
483484
public Object decode(Response response, Type type) throws IOException {
484485
String string = super.decode(response, type).toString();
485486
if ("retry!".equals(string)) {
486-
throw new RetryableException(string, "post", null);
487+
throw new RetryableException(string, HttpMethod.POST, null);
487488
}
488489
return string;
489490
}
@@ -524,7 +525,7 @@ public void ensureRetryerClonesItself() {
524525
.errorDecoder(new ErrorDecoder() {
525526
@Override
526527
public Exception decode(String methodKey, Response response) {
527-
return new RetryableException("play it again sam!", "post", null);
528+
return new RetryableException("play it again sam!", HttpMethod.POST, null);
528529
}
529530
}).target(TestInterface.class, "http://localhost:" + server.getPort());
530531

core/src/test/java/feign/client/DefaultClientTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@
1313
*/
1414
package feign.client;
1515

16-
import feign.FeignException;
16+
import static org.hamcrest.core.Is.isA;
17+
import static org.junit.Assert.assertEquals;
18+
1719
import java.io.IOException;
1820
import java.net.ProtocolException;
21+
1922
import javax.net.ssl.HostnameVerifier;
2023
import javax.net.ssl.SSLSession;
24+
2125
import org.junit.Test;
26+
2227
import feign.Client;
2328
import feign.Feign;
2429
import feign.Feign.Builder;
2530
import feign.RetryableException;
2631
import okhttp3.mockwebserver.MockResponse;
2732
import okhttp3.mockwebserver.SocketPolicy;
28-
import static org.hamcrest.core.Is.isA;
29-
import static org.junit.Assert.assertEquals;
3033

3134
/**
3235
* Tests client-specific behavior, such as ensuring Content-Length is sent when specified.

0 commit comments

Comments
 (0)