Dynamic urls with Koin with Retrofit also call the old url

Viewed 334

I'm working on a solution that needs to make recurring calls to an api every 10 seconds. However, I need to dynamically change the URL pointing to another service. That is, the new loop that will start will make the call to this new url base. I am using Koin as a DI. Here is an example of my code:

This is my dataModule koin

single<Retrofit>() {

        Retrofit.Builder()
            .client(httpClient)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(OLD_BASE)
            .build()
    }

    single<ApiService>() {
        get<Retrofit>().create(ApiService::class.java)
    }

    {   single<OkHttpClient>(named(WITH_AUTH)) {
        OkHttpClient.Builder()
            .callTimeout(30, TimeUnit.SECONDS)
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(30, TimeUnit.SECONDS)
            .addInterceptor(get<HttpLoggingInterceptor>(named(DATA_INTERCEPTOR)))
            .addInterceptor(get<AuthInterceptor>(named(AUTH_INTERCEPTOR)))
            .authenticator(get<AccessTokenAuthenticator>(named(AUTH_AUTHENTICATOR)))
            .build()
    }

  single(named(DATA_INTERCEPTOR)) {
        HttpLoggingInterceptor().apply {
            level =
                if (BuildConfig.DEBUG) {
                    HttpLoggingInterceptor.Level.BODY
                } else {
                    HttpLoggingInterceptor.Level.BASIC
                }
        }
    }

    single<AuthInterceptor>(named(AUTH_INTERCEPTOR)) {
        AuthInterceptor(
            get(), get()
        )
    }

And this is my interceptor :

class AuthInterceptor(
    private val tokenRepository: TokenRepository,
    private val envRepository: EnvRepository
) : Interceptor {

    @Volatile
    private var host: HttpUrl? = null

    override fun intercept(chain: Interceptor.Chain): Response {


            var request = chain.request()
            val token = tokenRepository.getToken(TokenRepository.AUTH_TOKEN).blockingGet()
            //val authenticationRequest = request(originalRequest, token)

            host = envRepository.getEnvBaseUrl().toHttpUrlOrNull()
            host?.let {
                val newUrl = chain.request().url.newBuilder()
                    .scheme(it.scheme)
                    .host(it.toUrl().toURI().host)
                    .port(it.port)
                    .build()

              request =  chain.request().newBuilder()
                    .url(newUrl)
                    .build()
            }
            val authRequest = request(request, token) ?: request

            return chain.proceed(authRequest)
    }

    private fun request(originalRequest: Request?, token: String?): Request? {


        return if (!token.isNullOrEmpty()) {
            originalRequest?.newBuilder()?.addHeader("Authorization", "Bearer $token")?.build()
        } else {
            originalRequest
        }

    }
}

The problem is that my interceptor works well, but each time before calling the new URL it also calls the old one. And I have no idea how to prevent it from calling the old URL in the loop. SO I have something like this in my debuger htts:

call old url call olrd url call new url call new url call old url call old url call new url call new url

I hope I have been clear Thanks,

0 Answers
Related