3

I have a query that returns a list of servers, and the user can select the server he needs.

Googling did not help, almost no results.

Tell me how to implement basic URL spoofing in real time using Koin and Retrofit?

My Modules:

fun createMainModule(context: Context) = module {

    single(named(APP_CONTEXT)) { context }

    single(named(RESOURCES)) { context.resources }

    single(named(REPOSITORY)) {
        Repository(get(named(RETROFIT)))
    }
}

fun createNetworkModule(baseUrl: String) = module(override = true) {

    single(named(TOKEN_INTERCEPTOR)) { createTokenInterceptor(get(named(DATA_PROVIDER))) }

    single(named(OK_HTTP)) { createOkHttpClient(get(named(TOKEN_INTERCEPTOR))) }

    single(named(GSON)) { createGson() }

    single(named(RETROFIT)) {
        createRetrofit(
            get(named(RESOURCES)),
            get(named(LOG_OUT_SUBJECT)),
            get(named(GSON)),
            baseUrl,
            get(named(OK_HTTP))
        )
}
michael_bitard
  • 3,613
  • 1
  • 24
  • 35

1 Answers1

3

I resolve my problem with comment @sonnet This code:

class ChangeableBaseUrlInterceptor : Interceptor {
    @Volatile
    private var host: HttpUrl? = null

    fun setHost(host: String) {
        this.host = host.toHttpUrlOrNull()
    }

    fun clear() {
        host = null
    }

    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
        var request = chain.request()
        host?.let {
            val newUrl = request.url.newBuilder()
                .scheme(it.scheme)
                .host(it.toUrl().toURI().host)
                .port(it.port)
                .build()
            request = request.newBuilder().url(newUrl).build()
        }
        return chain.proceed(request)
    }
}