Skip to content

Instantly share code, notes, and snippets.

@fevziomurtekin
Created May 19, 2019 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fevziomurtekin/de965791d0d940fb9a74d097ccf39e53 to your computer and use it in GitHub Desktop.
Save fevziomurtekin/de965791d0d940fb9a74d097ccf39e53 to your computer and use it in GitHub Desktop.
RemoteModule.kt
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