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

info.metadude.kotlin.library.engelsystem.ApiModule.kt Maven / Gradle / Ivy

There is a newer version: 9.2.0
Show newest version
package info.metadude.kotlin.library.engelsystem

import com.squareup.moshi.Moshi
import info.metadude.kotlin.library.engelsystem.adapters.InstantJsonAdapter
import info.metadude.kotlin.library.engelsystem.adapters.ZoneOffsetJsonAdapter
import info.metadude.kotlin.library.engelsystem.adapters.ZonedDateTimeJsonAdapter
import okhttp3.OkHttpClient
import org.threeten.bp.Instant
import org.threeten.bp.ZoneOffset
import org.threeten.bp.ZonedDateTime
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory

object ApiModule {

    @JvmStatic
    fun provideEngelsystemService(
        baseUrl: String,
        okHttpClient: OkHttpClient
    ): EngelsystemService {
        return createRetrofit(baseUrl, okHttpClient).create(EngelsystemService::class.java)
    }

    private fun provideMoshiBuilder(): Moshi {
        return Moshi.Builder()
            .add(Instant::class.java, InstantJsonAdapter())
            .add(ZonedDateTime::class.java, ZonedDateTimeJsonAdapter())
            .add(ZoneOffset::class.java, ZoneOffsetJsonAdapter())
            .build()
    }

    private fun createRetrofit(
        baseUrl: String,
        okHttpClient: OkHttpClient
    ) = Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(MoshiConverterFactory.create(provideMoshiBuilder()))
        .client(okHttpClient)
        .build()

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy