Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methodKey to Request & RetryableException #2714

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ public class Example {
public Exception decode(String methodKey, Response response) {
// wrapper 401 to RetryableException in order to retry
if (response.status() == 401) {
return new RetryableException(response.status(), response.reason(), response.request().httpMethod(), null, response.request());
return new RetryableException(response.status(), response.reason(), response.request().httpMethod(), methodKey, null, response.request());
}
return defaultErrorDecoder.decode(methodKey, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ class ExceptionGenerator {
.headers(testHeaders)
.request(
Request.create(
Request.HttpMethod.GET, "http://test", testHeaders, Request.Body.empty(), null))
Request.HttpMethod.GET,
"Wikipedia#search(String)",
"http://test",
testHeaders,
Request.Body.empty(),
null))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ Response testResponse(int status, String body, Map<String, Collection<String>> h
.headers(headers)
.request(
Request.create(
Request.HttpMethod.GET, "http://test", headers, Request.Body.empty(), null))
Request.HttpMethod.GET,
"Wikipedia#search(String)",
"http://test",
headers,
Request.Body.empty(),
null))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class AnnotationErrorDecoderExceptionConstructorsTest
private static final feign.Request REQUEST =
feign.Request.create(
feign.Request.HttpMethod.GET,
"Wikipedia#search(String)",
"http://test",
Collections.emptyMap(),
Request.Body.empty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ public void buildResponse() {
Response.builder()
.status(200)
.reason("OK")
.request(Request.create(HttpMethod.GET, "/", Collections.emptyMap(), null, Util.UTF_8))
.request(
Request.create(
HttpMethod.GET,
"Wikipedia#search(String)",
"/",
Collections.emptyMap(),
null,
Util.UTF_8))
.headers(Collections.emptyMap())
.body(carsJson(Integer.parseInt(size)), Util.UTF_8)
.build();
Expand Down
1 change: 1 addition & 0 deletions core/src/main/java/feign/FeignException.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ static FeignException errorExecuting(Request request, IOException cause) {
-1,
format("%s executing %s %s", cause.getMessage(), request.httpMethod(), request.url()),
request.httpMethod(),
request.methodKey(),
cause,
nonRetryable,
request);
Expand Down
30 changes: 25 additions & 5 deletions core/src/main/java/feign/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,26 @@ public String toString() {
* No parameters can be null except {@code body} and {@code charset}. All parameters must be
* effectively immutable, via safe copies, not mutating or otherwise.
*
* @deprecated {@link #create(HttpMethod, String, Map, byte[], Charset)}
* @deprecated {@link #create(HttpMethod, String, String, Map, byte[], Charset)}
*/
@Deprecated
public static Request create(
String method,
String methodKey,
String url,
Map<String, Collection<String>> headers,
byte[] body,
Charset charset) {
checkNotNull(method, "httpMethod of %s", method);
final HttpMethod httpMethod = HttpMethod.valueOf(method.toUpperCase());
return create(httpMethod, url, headers, body, charset, null);
return create(httpMethod, methodKey, url, headers, body, charset, null);
}

/**
* Builds a Request. All parameters must be effectively immutable, via safe copies.
*
* @param httpMethod for the request.
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
* @param url for the request.
* @param headers to include.
* @param body of the request, can be {@literal null}
Expand All @@ -114,17 +116,19 @@ public static Request create(
@Deprecated
public static Request create(
HttpMethod httpMethod,
String methodKey,
String url,
Map<String, Collection<String>> headers,
byte[] body,
Charset charset) {
return create(httpMethod, url, headers, Body.create(body, charset), null);
return create(httpMethod, methodKey, url, headers, Body.create(body, charset), null);
}

/**
* Builds a Request. All parameters must be effectively immutable, via safe copies.
*
* @param httpMethod for the request.
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
* @param url for the request.
* @param headers to include.
* @param body of the request, can be {@literal null}
Expand All @@ -133,33 +137,37 @@ public static Request create(
*/
public static Request create(
HttpMethod httpMethod,
String methodKey,
String url,
Map<String, Collection<String>> headers,
byte[] body,
Charset charset,
RequestTemplate requestTemplate) {
return create(httpMethod, url, headers, Body.create(body, charset), requestTemplate);
return create(httpMethod, methodKey, url, headers, Body.create(body, charset), requestTemplate);
}

/**
* Builds a Request. All parameters must be effectively immutable, via safe copies.
*
* @param httpMethod for the request.
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
* @param url for the request.
* @param headers to include.
* @param body of the request, can be {@literal null}
* @return a Request
*/
public static Request create(
HttpMethod httpMethod,
String methodKey,
String url,
Map<String, Collection<String>> headers,
Body body,
RequestTemplate requestTemplate) {
return new Request(httpMethod, url, headers, body, requestTemplate);
return new Request(httpMethod, methodKey, url, headers, body, requestTemplate);
}

private final HttpMethod httpMethod;
private final String methodKey;
private final String url;
private final Map<String, Collection<String>> headers;
private final Body body;
Expand All @@ -170,18 +178,21 @@ public static Request create(
* Creates a new Request.
*
* @param method of the request.
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
* @param url for the request.
* @param headers for the request.
* @param body for the request, optional.
* @param requestTemplate used to build the request.
*/
Request(
HttpMethod method,
String methodKey,
String url,
Map<String, Collection<String>> headers,
Body body,
RequestTemplate requestTemplate) {
this.httpMethod = checkNotNull(method, "httpMethod of %s", method.name());
this.methodKey = checkNotNull(methodKey, "methodKey of %s", methodKey);
this.url = checkNotNull(url, "url");
this.headers = checkNotNull(headers, "headers of %s %s", method, url);
this.body = body;
Expand Down Expand Up @@ -209,6 +220,15 @@ public HttpMethod httpMethod() {
return this.httpMethod;
}

/**
* Method key from {@link Feign#configKey(Class, java.lang.reflect.Method)} for this request.
*
* @return the methodKey string
*/
public String methodKey() {
return this.methodKey;
}

/**
* URL for the request.
*
Expand Down
28 changes: 27 additions & 1 deletion core/src/main/java/feign/RequestTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public final class RequestTemplate implements Serializable {
private UriTemplate uriTemplate;
private BodyTemplate bodyTemplate;
private HttpMethod method;
private String methodKey;
private transient Charset charset = Util.UTF_8;
private Request.Body body = Request.Body.empty();
private boolean decodeSlash = true;
Expand All @@ -81,6 +82,7 @@ public RequestTemplate() {
* @param uriTemplate for the template.
* @param bodyTemplate for the template, may be {@literal null}
* @param method of the request.
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}
* @param charset for the request.
* @param body of the request, may be {@literal null}
* @param decodeSlash if the request uri should encode slash characters.
Expand All @@ -94,6 +96,7 @@ private RequestTemplate(
UriTemplate uriTemplate,
BodyTemplate bodyTemplate,
HttpMethod method,
String methodKey,
Charset charset,
Request.Body body,
boolean decodeSlash,
Expand All @@ -106,6 +109,7 @@ private RequestTemplate(
this.bodyTemplate = bodyTemplate;
this.method = method;
this.charset = charset;
this.methodKey = methodKey;
this.body = body;
this.decodeSlash = decodeSlash;
this.collectionFormat =
Expand All @@ -128,6 +132,7 @@ public static RequestTemplate from(RequestTemplate requestTemplate) {
requestTemplate.uriTemplate,
requestTemplate.bodyTemplate,
requestTemplate.method,
requestTemplate.methodKey,
requestTemplate.charset,
requestTemplate.body,
requestTemplate.decodeSlash,
Expand Down Expand Up @@ -157,6 +162,7 @@ public RequestTemplate(RequestTemplate toCopy) {
this.target = toCopy.target;
this.fragment = toCopy.fragment;
this.method = toCopy.method;
this.methodKey = toCopy.methodKey;
this.queries.putAll(toCopy.queries);
this.headers.putAll(toCopy.headers);
this.charset = toCopy.charset;
Expand Down Expand Up @@ -288,7 +294,7 @@ public Request request() {
if (!this.resolved) {
throw new IllegalStateException("template has not been resolved.");
}
return Request.create(this.method, this.url(), this.headers(), this.body, this);
return Request.create(this.method, this.methodKey, this.url(), this.headers(), this.body, this);
}

/**
Expand Down Expand Up @@ -330,6 +336,26 @@ public String method() {
return (method != null) ? method.name() : null;
}

/**
* Set the method key.
*
* @param methodKey value of {@link Feign#configKey(Class, java.lang.reflect.Method)}.
* @return a RequestTemplate for chaining.
*/
public RequestTemplate methodKey(String methodKey) {
this.methodKey = methodKey;
return this;
}

/**
* The request method key as value of {@link Feign#configKey(Class, java.lang.reflect.Method)}.
*
* @return method key.
*/
public String methodKey() {
return methodKey;
}

/**
* Set whether do encode slash {@literal /} characters when resolving this template.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ static class BuildTemplateByResolvingArgs implements RequestTemplate.Factory {
public RequestTemplate create(Object[] argv) {
RequestTemplate mutable = RequestTemplate.from(metadata.template());
mutable.feignTarget(target);
mutable.methodKey(metadata.configKey());
if (metadata.urlIndex() != null) {
int urlIndex = metadata.urlIndex();
checkArgument(argv[urlIndex] != null, "URI parameter %s was null", urlIndex);
Expand Down
29 changes: 27 additions & 2 deletions core/src/main/java/feign/RetryableException.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class RetryableException extends FeignException {

private final Long retryAfter;
private final HttpMethod httpMethod;
private final String methodKey;

/**
* @param retryAfter usually corresponds to the {@link feign.Util#RETRY_AFTER} header. If you
Expand All @@ -39,11 +40,13 @@ public RetryableException(
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Throwable cause,
Long retryAfter,
Request request) {
super(status, message, request, cause);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter;
}

Expand All @@ -52,11 +55,13 @@ public RetryableException(
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Throwable cause,
Date retryAfter,
Request request) {
super(status, message, request, cause);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
}

Expand All @@ -65,17 +70,29 @@ public RetryableException(
* don't want to retry, set null.
*/
public RetryableException(
int status, String message, HttpMethod httpMethod, Long retryAfter, Request request) {
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Long retryAfter,
Request request) {
super(status, message, request);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter;
}

@Deprecated
public RetryableException(
int status, String message, HttpMethod httpMethod, Date retryAfter, Request request) {
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Date retryAfter,
Request request) {
super(status, message, request);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
}

Expand All @@ -86,12 +103,14 @@ public RetryableException(
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Long retryAfter,
Request request,
byte[] responseBody,
Map<String, Collection<String>> responseHeaders) {
super(status, message, request, responseBody, responseHeaders);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter;
}

Expand All @@ -100,12 +119,14 @@ public RetryableException(
int status,
String message,
HttpMethod httpMethod,
String methodKey,
Date retryAfter,
Request request,
byte[] responseBody,
Map<String, Collection<String>> responseHeaders) {
super(status, message, request, responseBody, responseHeaders);
this.httpMethod = httpMethod;
this.methodKey = methodKey;
this.retryAfter = retryAfter != null ? retryAfter.getTime() : null;
}

Expand All @@ -120,4 +141,8 @@ public Long retryAfter() {
public HttpMethod method() {
return this.httpMethod;
}

public String methodKey() {
return this.methodKey;
}
}
Loading