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

[Java][Feign] Add http status to feign result #10583

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class JacksonResponseDecoder extends JacksonDecoder {
//The HttpResponse class has a single type parameter, the Dto class itself
responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
Object body = super.decode(response, responseBodyType);
return new HttpResponse(responseHeaders, body);
return new HttpResponse(responseHeaders, body, response.status());
} else {
//The response is not encapsulated in the HttpResponse, decode the Dto as normal
return super.decode(response, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import feign.RetryableException;
import feign.codec.ErrorDecoder;

/**
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 402 may indicate an expired token
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 403 may indicate an expired token
* All the other HTTP status are handled by the {@link feign.codec.ErrorDecoder.Default} decoder
*/
public class ApiErrorDecoder implements ErrorDecoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ public class HttpResponse<T>{

private T body;

public HttpResponse(Map<String, Collection<String>> headers, T body) {
private int status;

public HttpResponse(Map<String, Collection<String>> headers, T body, int status) {
this.headers = headers;
this.body = body;
this.status = status;
}

public T getBody(){
Expand All @@ -21,4 +24,8 @@ public class HttpResponse<T>{
public Map<String, Collection<String>> getHeaders(){
return headers;
}

public int getStatus(){
return status;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hugo-ma-alves can we make HttpResponse look more like ApiResponse ?

The goal is to make it consistent across different Java http library.

(in other words, I suggest we rename HttpResponse to ApiResponse)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion.
I refactored the templates to rename the HttpResponse to ApiResponse, and to keep it coherent I also renamed the JacksonResponseDecoder to ApiResponseDecoder.

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Object decode(Response response, Type type) throws IOException {
//The HttpResponse class has a single type parameter, the Dto class itself
responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
Object body = super.decode(response, responseBodyType);
return new HttpResponse(responseHeaders, body);
return new HttpResponse(responseHeaders, body, response.status());
} else {
//The response is not encapsulated in the HttpResponse, decode the Dto as normal
return super.decode(response, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import feign.codec.ErrorDecoder;

/**
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 402 may indicate an expired token
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 403 may indicate an expired token
* All the other HTTP status are handled by the {@link feign.codec.ErrorDecoder.Default} decoder
*/
public class ApiErrorDecoder implements ErrorDecoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ public class HttpResponse<T>{

private T body;

public HttpResponse(Map<String, Collection<String>> headers, T body) {
private int status;

public HttpResponse(Map<String, Collection<String>> headers, T body, int status) {
this.headers = headers;
this.body = body;
this.status = status;
}

public T getBody(){
Expand All @@ -21,4 +24,8 @@ public T getBody(){
public Map<String, Collection<String>> getHeaders(){
return headers;
}

public int getStatus(){
return status;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Object decode(Response response, Type type) throws IOException {
//The HttpResponse class has a single type parameter, the Dto class itself
responseBodyType = ((ParameterizedType) type).getActualTypeArguments()[0];
Object body = super.decode(response, responseBodyType);
return new HttpResponse(responseHeaders, body);
return new HttpResponse(responseHeaders, body, response.status());
} else {
//The response is not encapsulated in the HttpResponse, decode the Dto as normal
return super.decode(response, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import feign.codec.ErrorDecoder;

/**
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 402 may indicate an expired token
* Error decoder that makes the HTTP 401 and 403 Retryable. Sometimes the 401 or 403 may indicate an expired token
* All the other HTTP status are handled by the {@link feign.codec.ErrorDecoder.Default} decoder
*/
public class ApiErrorDecoder implements ErrorDecoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ public class HttpResponse<T>{

private T body;

public HttpResponse(Map<String, Collection<String>> headers, T body) {
private int status;

public HttpResponse(Map<String, Collection<String>> headers, T body, int status) {
this.headers = headers;
this.body = body;
this.status = status;
}

public T getBody(){
Expand All @@ -21,4 +24,8 @@ public T getBody(){
public Map<String, Collection<String>> getHeaders(){
return headers;
}

public int getStatus(){
return status;
}
}