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

com.deque.networking.models.DequeRetrofitBuilder.kt Maven / Gradle / Ivy

The newest version!
package com.deque.networking.models

import com.deque.axe.android.moshi.MoshiConfig
import com.deque.networking.utils.GzipRequestInterceptor
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.util.concurrent.TimeUnit

internal const val DEFAULT_CONTENT_TYPE = "Content-Type: application/json"

open class DequeRetrofitBuilder constructor(url: String) {
    private val retrofitBuilder: Retrofit.Builder
    fun debug(): DequeRetrofitBuilder {
        return debug(HttpLoggingInterceptor.Level.BODY)
    }

    fun debug(level: HttpLoggingInterceptor.Level): DequeRetrofitBuilder {
        val logging = HttpLoggingInterceptor()
        logging.level = level
        val httpClient = OkHttpClient.Builder()
        httpClient.addInterceptor(logging)
        httpClient.readTimeout(2, TimeUnit.MINUTES)
        httpClient.callTimeout(2, TimeUnit.MINUTES)
        retrofitBuilder.client(httpClient.build())
        return this
    }

    open fun url(url: String): DequeRetrofitBuilder {
        retrofitBuilder.baseUrl(url)
        return this
    }

    fun  client(clazz: Class): T {
        return retrofitBuilder.build().create(clazz)
    }

    fun gzip(): DequeRetrofitBuilder {
        val httpClient = OkHttpClient.Builder()
        httpClient.addInterceptor(GzipRequestInterceptor())
        retrofitBuilder.client(httpClient.build())
        return this
    }

    init {
        retrofitBuilder = Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(
                ScalarsConverterFactory.create()
            )
            .addConverterFactory(
                MoshiConverterFactory.create(MoshiConfig.axeMoshiConfig.moshi)
            )
            .addCallAdapterFactory(ResultCallAdapterFactory())
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy