And here the simple ProxyCallAdapter
that doesn't do anything special, but records the request and it's annotation
content into our map/registry. Everything else is Proxy functionality.
class ProxyCallAdapter<RETURN_TYPE : Any>(
private val adapter: CallAdapter<Any, RETURN_TYPE>,
private val registration: MutableMap<Int, String>,
private val info: String
) : CallAdapter<Any, RETURN_TYPE> {
override fun responseType(): Type = adapter.responseType()
override fun adapt(call: Call<Any>): RETURN_TYPE {
registration[RequestIdentifier.identify(call.request())] = info
return adapter.adapt(call)
}
}
We still need to implement the Interceptor
, which takes the information and manipulate the request. This now turns
out to be easy.
class SomeCustomInterceptor(private val registration: MutableMap<Int, String>) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
var request = chain.request()
val annotationContent = registration[RequestIdentifier.identify(request)]
if(annotationContent != null) {
request = chain.request().newBuilder()
.addHeader("SomeCustomHeader", annotationContent)
.build()
}
return chain.proceed(request)
}
}