|
13 | 13 | */ |
14 | 14 | package feign; |
15 | 15 |
|
| 16 | +import static feign.Util.checkNotNull; |
| 17 | +import static feign.Util.valuesOrEmpty; |
16 | 18 | import java.net.HttpURLConnection; |
17 | 19 | import java.nio.charset.Charset; |
18 | 20 | import java.util.Collection; |
19 | 21 | import java.util.Map; |
20 | | -import static feign.Util.checkNotNull; |
21 | | -import static feign.Util.valuesOrEmpty; |
22 | 22 |
|
23 | 23 | /** |
24 | 24 | * An immutable request to an http server. |
25 | 25 | */ |
26 | 26 | public final class Request { |
27 | 27 |
|
| 28 | + public enum HttpMethod { |
| 29 | + GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH |
| 30 | + } |
| 31 | + |
28 | 32 | /** |
29 | 33 | * No parameters can be null except {@code body} and {@code charset}. All parameters must be |
30 | 34 | * effectively immutable, via safe copies, not mutating or otherwise. |
| 35 | + * |
| 36 | + * @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)} |
31 | 37 | */ |
32 | 38 | public static Request create(String method, |
33 | 39 | String url, |
34 | 40 | Map<String, Collection<String>> headers, |
35 | 41 | byte[] body, |
36 | 42 | Charset charset) { |
37 | | - 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); |
38 | 46 | } |
39 | 47 |
|
40 | | - private final String 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; |
41 | 68 | private final String url; |
42 | 69 | private final Map<String, Collection<String>> headers; |
43 | 70 | private final byte[] body; |
44 | 71 | private final Charset charset; |
45 | 72 |
|
46 | | - 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, |
47 | 74 | Charset charset) { |
48 | | - this.method = checkNotNull(method, "method of %s", url); |
| 75 | + this.httpMethod = checkNotNull(method, "httpMethod of %s", method.name()); |
49 | 76 | this.url = checkNotNull(url, "url"); |
50 | 77 | this.headers = checkNotNull(headers, "headers of %s %s", method, url); |
51 | 78 | this.body = body; // nullable |
52 | 79 | this.charset = charset; // nullable |
53 | 80 | } |
54 | 81 |
|
55 | | - /* 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 | + */ |
56 | 88 | public String method() { |
57 | | - return method; |
| 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; |
58 | 99 | } |
59 | 100 |
|
60 | 101 | /* Fully resolved URL including query. */ |
@@ -89,7 +130,7 @@ public byte[] body() { |
89 | 130 | @Override |
90 | 131 | public String toString() { |
91 | 132 | StringBuilder builder = new StringBuilder(); |
92 | | - builder.append(method).append(' ').append(url).append(" HTTP/1.1\n"); |
| 133 | + builder.append(httpMethod).append(' ').append(url).append(" HTTP/1.1\n"); |
93 | 134 | for (String field : headers.keySet()) { |
94 | 135 | for (String value : valuesOrEmpty(headers, field)) { |
95 | 136 | builder.append(field).append(": ").append(value).append('\n'); |
|
0 commit comments