OpenFeign / feign

Feign makes writing java http clients easier
Apache License 2.0
9.37k stars 1.91k forks source link

Using feign with multiple possible 20x codes #2461

Open randysecrist opened 3 weeks ago

randysecrist commented 3 weeks ago

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?