Hector
Hector

Reputation: 5378

How to dynamically configure OkHttpClient when using Retrofit?

I employ Retrofit in my Android application for Networking

api "com.squareup.retrofit2:retrofit:$retrofit2Version"
api "com.squareup.retrofit2:adapter-rxjava2:$retrofit2Version"
api "com.squareup.retrofit2:converter-scalars:$retrofit2Version"
api "com.squareup.retrofit2:converter-jackson:$retrofit2Version"

While developing my projects its very useful to log the HTTP Request and responses using

api "com.squareup.okhttp3:logging-interceptor:$okhttp3Version"

I configure Retrofit as follows:-

abstract class BaseService {

    internal val okHttpClient = OkHttpClient.Builder()
            .connectTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .readTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .writeTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .callTimeout(OK_HTTP_CLIENT_TIMEOUT, TimeUnit.SECONDS)
            .followSslRedirects(true)
            .retryOnConnectionFailure(true)
            .followRedirects(true)
            .addInterceptor(HTTP_HEADERS)
            .addInterceptor(HTTP_LOGGING_INTERCEPTOR)
            .build()

    companion object {

        private const val OK_HTTP_CLIENT_TIMEOUT: Long = 60

        private val HTTP_LOGGING_INTERCEPTOR = HttpLoggingInterceptor()
        private val HTTP_HEADERS = constructHeaderInterceptor()

        private const val HTTP_ORIGIN_HEADER = "Origin"
        private const val HTTP_ORIGIN_VALUE = "Android"

        private fun constructHeaderInterceptor(): Interceptor {
            return Interceptor {
                val request = it.request()
                val newRequest = request.newBuilder().addHeader(HTTP_ORIGIN_HEADER, HTTP_ORIGIN_VALUE).build()
                it.proceed(newRequest)
            }
        }

        init {
            HTTP_LOGGING_INTERCEPTOR.level = HttpLoggingInterceptor.Level.NONE
        }
    }
}

Then in my implementation of Service:-

class Service private constructor() : BaseService(), Api {

    private val service: Api

    init {
        val retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(JacksonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build()

        service = retrofit.create(Api::class.java)
   }


    override fun query(authenticationToken: String, request: Request): Single<Response<QueryResponse>> {
        return service.query(authenticationToken, request)
    }

    companion object {

        private val BASE_URL = BuildConfig.SDK_BASE_URL

        private val INSTANCE = Service()

        fun instance(): Api {
            return INSTANCE
        }
    }
}

The issue is that I am facing is that when I use

HttpLoggingInterceptor.Level.BODY

The logcat output is "excessive" as some of my network calls responses are very large Json documents.

What I would like is that I can configure my Network call logging at a fine grained level e.g. on a call by call basis.

The only way I can see how to achieve this though is to create a paired OkHttpClient & retrofit for each logging level, e.g. BODY, HEADERS, BASIC, and NONE.

What would be excellent is to be able to set the logging value on a call by call basis and only use a single OkHttpClient & retrofit instance.

Is there any way I can set different logging levels at a call by call level?

Upvotes: 1

Views: 853

Answers (1)

veritas1
veritas1

Reputation: 9180

You can create your own logger and delegate to HttpLoggingInterceptor.

class MyLoggingInterceptor(private val logger: HttpLoggingInterceptor) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        if (chain.request().url().toString().contains("condition")) {
            logger.level = HttpLoggingInterceptor.Level.BODY
        } else {
            logger.level = HttpLoggingInterceptor.Level.HEADERS
        }
        return logger.intercept(chain)
    }
}

Then add to the OkHttp builder

.addInterceptor(MyLoggingInterceptor(HttpLoggingInterceptor()))

Upvotes: 2

Related Questions