Created
May 19, 2019 22:29
-
-
Save fevziomurtekin/de965791d0d940fb9a74d097ccf39e53 to your computer and use it in GitHub Desktop.
RemoteModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fevziomurtekin.di | |
import com.fevziomurtekin.hackernewsapp.data.network.RetroInterface | |
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory | |
import okhttp3.OkHttpClient | |
import okhttp3.logging.HttpLoggingInterceptor | |
import org.koin.dsl.module.applicationContext | |
import retrofit2.Retrofit | |
import retrofit2.converter.gson.GsonConverterFactory | |
import java.util.concurrent.TimeUnit | |
/** | |
* Remote Web Service in Retrofit. | |
*/ | |
const val BASE_URL="https://hacker-news.firebaseio.com/v0/" | |
val remoteModule = applicationContext { | |
// provider web compenent. | |
bean { createOkHttpClient() } | |
// fill property. | |
bean { createWebService<RetroInterface>(get(), BASE_URL) } | |
} | |
fun createOkHttpClient(): OkHttpClient { | |
val httpLoggingInterceptor = HttpLoggingInterceptor() | |
httpLoggingInterceptor.level = HttpLoggingInterceptor.Level.BASIC | |
return OkHttpClient.Builder() | |
.connectTimeout(60L, TimeUnit.SECONDS) | |
.readTimeout(60L, TimeUnit.SECONDS) | |
.addInterceptor(httpLoggingInterceptor).build() | |
} | |
inline fun <reified T> createWebService(okHttpClient: OkHttpClient, url: String): T { | |
val retrofit = Retrofit.Builder() | |
.baseUrl(url) | |
.client(okHttpClient) | |
.addConverterFactory(GsonConverterFactory.create()) | |
.addCallAdapterFactory(CoroutineCallAdapterFactory()) | |
.build() | |
return retrofit.create(T::class.java) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment