feign icon indicating copy to clipboard operation
feign copied to clipboard

Using feign with multiple possible 20x codes

Open randysecrist opened this issue 1 year ago • 1 comments

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?

randysecrist avatar Jun 27 '24 14:06 randysecrist

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 avatar Sep 11 '24 00:09 kdavisk6