Skip to content

Instantly share code, notes, and snippets.

@soulduse
Last active May 7, 2022 08:48
Show Gist options
  • Save soulduse/b832152e42b893581f7736f4524f3dcd to your computer and use it in GitHub Desktop.
Save soulduse/b832152e42b893581f7736f4524f3dcd to your computer and use it in GitHub Desktop.
ver.2) Example of usage kotlin-coroutines-retrofit
interface ApiInterface {
@GET("User")
fun getUser(
@Query("user_id") userId: String
): Deferred<User>
}
object ApiProvider{
private const val BASE_URL = "http://domain"
private val okHttpClient: OkHttpClient
init {
val httpLogging = provideLoggingInterceptor()
okHttpClient = provideOkHttpClient(httpLogging)
}
fun provideApi(): ApiInterface
= Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
.create(ApiInterface::class.java)
private fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient
= OkHttpClient().newBuilder()
.run {
addInterceptor(interceptor)
}.build()
private fun provideLoggingInterceptor(): HttpLoggingInterceptor
= HttpLoggingInterceptor().apply {
level = if(BuildConfig.DEBUG)
HttpLoggingInterceptor.Level.BODY
else
HttpLoggingInterceptor.Level.NONE
}
}
apply plugin: 'kotlin-android'
// ...
android {
// ...
}
dependencies {
// Retrofit Lib
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:$retrofitConverterVersion"
// https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter
// OKHttp Lib
implementation "com.squareup.okhttp3:okhttp:$okHttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
implementation "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"
// Kotlin & Coroutines
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutineVersion"
// ...
}
/**
* Usage
Network.request(
call = call,
success = { },
fail = { }
)
*/
/**
* Usage
* Network.request(
* doOnSubscribe = { loading },
* doOnTerminate = { init },
* call = call,
* success = { },
* fail = { }
* )
*/
object Network {
fun <T> request(
call: Deferred<T>,
success: ((response: T)-> Unit)?,
error: ((throwable: Throwable)-> Unit)?= null,
doOnSubscribe: (()-> Unit)?= null,
doOnTerminate: (()-> Unit)?= null) {
launch(UI) {
doOnSubscribe?.invoke()
try {
success?.invoke(call.await())
} catch (t: Throwable) {
error?.invoke(t)
} finally {
doOnTerminate?.invoke()
}
}
}
}
// Use sample
// call
val call = ApiProvider.provideApi().getUser("userId")
Network.request(
call = call,
success = {
// it. (User)
}
)
// error handling
Network.request(
call = call,
success = {
// it. (User)
},
fail = {
// it. (Throwable)
}
)
Network.request(
doOnSubscribe = { loading }, // this is option
doOnTerminate = { init }, // this is option
call = call,
success = { },
fail = { } // this is option
)
@soulduse
Copy link
Author

soulduse commented Apr 11, 2019

// TODO 
// It will be like below when I use 
HttpRequest.get("http://google.com").receive(System.out);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment