2

I have a retrofit module in my project, Before login i want to use retrofit without headers, But after login i want to use retrofit with headers using Hilt Dagger. How can i do this?

@Module
@InstallIn(SingletonComponent::class)
object RetrofitDi {


@Provides
fun getBasePath(): String {
    return "http://abcd.com/"
}

@Provides
fun providesLoggingInterceptor(): HttpLoggingInterceptor {

    return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}

@Provides
fun providesOkHttpClients(@ApplicationContext context: Context, sharedPreference: SharedPreference, httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
    val okhttpClient = OkHttpClient.Builder()
    okhttpClient.addInterceptor(httpLoggingInterceptor)
    okhttpClient.callTimeout(60, TimeUnit.SECONDS)
    okhttpClient.connectTimeout(60, TimeUnit.SECONDS)
    okhttpClient.writeTimeout(60, TimeUnit.SECONDS)
    okhttpClient.readTimeout(60, TimeUnit.SECONDS)
    val token = sharedPreference.getStringData(SharedPreference.AUTH_KEY)
    val user_id = sharedPreference.getStringData(SharedPreference.USER_ID)
    if (BuildConfig.DEBUG) {
        val interceptor = HttpLoggingInterceptor()
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS)
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        okhttpClient.addInterceptor(interceptor)
        okhttpClient.addInterceptor(Interceptor { chain ->
            val response = chain.proceed(chain.request())
            if (!response.isSuccessful) {
                when (response.code) {
                    CommonUtils.ALREADY_LOGGED_IN -> {
                        
                        sharedPreference.setBoolean(SharedPreference.IS_LOGGED_IN, false)
                        sharedPreference.clear()
                        context.getCacheDir().delete()
                        val intent = Intent(context, LoginActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                        context.startActivity(intent)
                    }
                }
            }
            response
        })
    }

    okhttpClient.addInterceptor(Interceptor { chain: Interceptor.Chain ->
        val builder1 = chain.request().newBuilder()
        var request: Request? = null
        if (!token.isEmpty()) {
            builder1.addHeader("auth_key", "" + token)
        }
        if (!user_id.isEmpty()) {
            builder1.addHeader("user_id", "" + user_id)
        }
        request = builder1.build()
        chain.proceed(request)
    })
    return okhttpClient.build()
}

@Provides
fun providesGSONConvertorFactory(): Converter.Factory {

    return GsonConverterFactory.create()
}

@Provides
fun providesRetrofit(baseUrl: String, convertor: Converter.Factory, okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(convertor).client(okHttpClient).build()
}


@Provides
fun providesApiService(retrofit: Retrofit): ApiService {
    return retrofit.create(ApiService::class.java)
}
}

Singleton instance created once in hilt-dagger but token and user_id will be available after login. After login i will need new okhttpclient. I did it without DI. But don't know how to deal with hilt-dagger.

Nitish
  • 313
  • 4
  • 11
  • You should use OkHttp interceptors for this. Add an interceptor which check if the user is logged or not, if yes add the header you need https://square.github.io/okhttp/interceptors/#rewriting-requests – MatPag Jul 08 '21 at 12:32
  • Or check this answer: https://stackoverflow.com/a/41082979/2910520 – MatPag Jul 08 '21 at 12:47
  • Thanks @MatPag i updated my question. I know how to do it without DI, but in Hilt-dagger i am struggling. – Nitish Jul 08 '21 at 13:35
  • 1
    I think your code is fine (you could clean up it a bit), just move ```val token = sharedPreference.getStringData(SharedPreference.AUTH_KEY) val user_id = sharedPreference.getStringData(SharedPreference.USER_ID)``` inside the second interceptor block, otherwise they won't be evaluated again because those are singleton – MatPag Jul 08 '21 at 14:08

1 Answers1

3

Better way to do this is to have your API service methods annotated with @Headers("Token-required") for the APIs which requires token. Then in your interceptor method check for this header as:

if (request.header("Token-required") != null) {
        request = request.newBuilder()
            .addHeader("token", "your token value")
            .build()
}
Anand
  • 857
  • 1
  • 12
  • 18