Skip to content

Instantly share code, notes, and snippets.

@soulduse
Last active May 28, 2020 12:29
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulduse/442c83b3f8cdd541f93baf2f4f38978f to your computer and use it in GitHub Desktop.
Save soulduse/442c83b3f8cdd541f93baf2f4f38978f to your computer and use it in GitHub Desktop.
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"
fun provideApi(): ApiInterface {
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(provideOkHttpClient(provideLoggingInterceptor()))
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(CoroutineCallAdapterFactory()) // https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter
.build()
.create(ApiInterface::class.java)
}
private fun provideOkHttpClient(interceptor: HttpLoggingInterceptor): OkHttpClient {
val b = OkHttpClient.Builder()
b.addInterceptor(interceptor)
return b.build()
}
private fun provideLoggingInterceptor(): HttpLoggingInterceptor {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return interceptor
}
}
apply plugin: 'kotlin-android'
// ...
android {
// ...
}
dependencies {
// Retrofit Lib
compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
compile "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:$retrofitConverterVersion"
// https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter
// OKHttp Lib
compile "com.squareup.okhttp3:okhttp:$okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$okHttpVersion"
compile "com.squareup.okhttp3:okhttp-urlconnection:$okHttpVersion"
// Kotlin & Coroutines
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutineVersion"
// ...
}
object Network {
fun defaultError(t: Throwable) {
t.printStackTrace()
}
fun <T> request(call: Deferred<T>, callback: NetworkCallback<T>) {
request(call, callback.success, callback.error)
}
private fun <T> request(call: Deferred<T>, onSuccess: ((T) -> Unit)?, onError: ((Throwable) -> Unit)?) {
launch(UI) {
try {
onSuccess?.let {
onSuccess(call.await())
}
} catch (httpException: HttpException) {
// a non-2XX response was received
defaultError(httpException)
} catch (t: Throwable) {
// a networking or data conversion error
onError?.let {
onError(t)
}
}
}
}
}
class NetworkCallback<T> {
var success: ((T) -> Unit) ?= null
var error: ((Throwable)-> Unit) ?= null
}
//use sample
Network.request(ApiProvider.provideApi().getUser("userId"),
NetworkCallback<User>().apply {
success = {
// it. (User)
}
})
// error handling
Network.request(ApiProvider.provideApi().getUser("userId"),
NetworkCallback<User>().apply {
success = {
// it. (User)
}
error = {
// it. (Throwable)
}
})
@soulduse
Copy link
Author

I think I made better code
check this code

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