All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.tinkoff.testops.droidherd.RetrofitProvider.kt Maven / Gradle / Ivy

Go to download

A library that parallelizes Android Test execution to all connected devices and emulators

There is a newer version: 1.0.0
Show newest version
package ru.tinkoff.testops.droidherd

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitProvider(
        private val url: String
) {

    companion object {
        val LOG: Logger = LoggerFactory.getLogger(RetrofitProvider::class.java)
    }

    fun provide(): Retrofit {
        val gson = createGson()
        val okHttpClient = createOkHttpClient()
        return Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create(gson)).client(okHttpClient).build()
    }

    private fun createOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder().apply {
            if ("true" == System.getenv("FORK_ENABLE_HTTP_LOGGER")) {
                val logger = HttpLoggingInterceptor.Logger { message -> LOG.info(message) }
                val interceptor = HttpLoggingInterceptor(logger)
                interceptor.level = HttpLoggingInterceptor.Level.BODY
                addInterceptor(interceptor)
            }
        }.build()
    }

    private fun createGson(): Gson {
        return GsonBuilder().setLenient().create()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy