Skip to content

Instantly share code, notes, and snippets.

@andretietz
Created December 14, 2020 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andretietz/2d54504084afb2a9b81ddc09cd24b3cc to your computer and use it in GitHub Desktop.
Save andretietz/2d54504084afb2a9b81ddc09cd24b3cc to your computer and use it in GitHub Desktop.
Error with markdown interpretor

ProxyCallAdapter

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)
  }
}

Manipulating the request

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)
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment