Last active
October 23, 2021 16:04
-
-
Save Skyyo/f957e47327e5558874eede0904ebaf2f to your computer and use it in GitHub Desktop.
dynamically change the host url
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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