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

Using feign with multiple possible 20x codes #2461

Open
randysecrist opened this issue Jun 27, 2024 · 1 comment
Open

Using feign with multiple possible 20x codes #2461

randysecrist opened this issue Jun 27, 2024 · 1 comment
Labels
feedback provided Feedback has been provided to the author

Comments

@randysecrist
Copy link

Using Feign 11.9.1 ... (yes - a bit old I know ...)

I have an API that I can't change that returns either 200 or 202 depending on what happens on the backend. Client needs to know which response status code was returned. Body will always be empty.

What is the best way to handle this? (Using feign/kotlin)

Currently - I have something super hacky (i'm not proude of this) working - Client side I have a definition using:

@RequestLine("PUT /some/rando/api")
    fun callRandoApi(
        @RequestBody request: RandoRequest,
    ): Response<EmptyResponse>

where:

class EmptyResponse // this models an empty body just fine I guess
data class Response<T>(val status: Int, val body: T) // models passing the response code back always

and a custom decoder:

class JacksonDecoderWithStatus<T> : JacksonDecoder() {
override fun decode(response: feign.Response, type: Type): Response<T> {
        // use the inner type
        val innerType: Type = (type as ParameterizedTypeImpl).actualTypeArguments.first() // yuck
        @Suppress("UNCHECKED_CAST")
        return Response(
            status = response.status(),
            body = super.decode(response, innerType) as T, // somewhat - gross
        )
    }
}

while the feign builder gets:

decoder = JacksonDecoderWithStatus<EmptyResponse>()

I realize that nothing really connects the decoder type to the response inner type and I don't like using the reflect stuff directly ... ; I have a inline reified function I was playing with as well ..

It seems like there should be a more straightforward way to do this ... Am I wrong?

@kdavisk6
Copy link
Member

kdavisk6 commented Sep 11, 2024

What may be more appropriate in your case is to use a ResponseMapper. This allows you to manipulate the response before decoding. Consider using this instead, along with a simple Decoder to get this mutated feign.Response into your EmptyResponse

@kdavisk6 kdavisk6 added the feedback provided Feedback has been provided to the author label Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feedback provided Feedback has been provided to the author
Projects
None yet
Development

No branches or pull requests

2 participants