Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active October 23, 2021 16:04
Show Gist options
  • Save Skyyo/f957e47327e5558874eede0904ebaf2f to your computer and use it in GitHub Desktop.
Save Skyyo/f957e47327e5558874eede0904ebaf2f to your computer and use it in GitHub Desktop.
dynamically change the host url
@Singleton
class HostSelectionInterceptor : Interceptor {
var host: String? = null
set(value) {
field = value?.toHttpUrlOrNull()?.host
}
@Throws(IOException::class)
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
var request = chain.request()
val host = this.host
if (host != null) {
val newUrl = request.url.newBuilder().host(host).build()
request = request.newBuilder().url(newUrl).build()
}
return chain.proceed(request)
}
}
@Singleton
@Provides
1. fun provideHostInterceptor(): HostSelectionInterceptor = HostSelectionInterceptor()
2. addInterceptor(hostSelectionInterceptor)
3. hostSelectionInterceptor.host = primarySmsUrl // updating value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment