Skip to content

Instantly share code, notes, and snippets.

@jmfayard
Last active February 16, 2018 12:46
Show Gist options
  • Save jmfayard/b65b999a7729664365a11858e603c7eb to your computer and use it in GitHub Desktop.
Save jmfayard/b65b999a7729664365a11858e603c7eb to your computer and use it in GitHub Desktop.
From Zero To One: One refactoring later
fun api() : ApiComponent = ApiModule
interface ApiComponent {
val okHttpClient: OkHttpClient
val moshi: Moshi
val catApi: CatApi
}
object ApiModule : ApiComponent {
val loggingInterceptor = HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY)
override val okHttpClient = OkHttpClient.Builder()
.addNetworkInterceptor(loggingInterceptor)
.build()
override val moshi = Moshi.Builder().build()
val retrofit = Retrofit.Builder()
.client(okHttpClient)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl("http://httpbin.org")
.build()
override val catApi = retrofit.create(CatApi::class.java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment