Last active
May 7, 2022 08:48
-
-
Save soulduse/b832152e42b893581f7736f4524f3dcd to your computer and use it in GitHub Desktop.
ver.2) Example of usage kotlin-coroutines-retrofit
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
interface ApiInterface { | |
@GET("User") | |
fun getUser( | |
@Query("user_id") userId: String | |
): Deferred<User> | |
} |
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
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 | |
} | |
} |
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
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" | |
// ... | |
} |
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
/** | |
* 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() | |
} | |
} | |
} | |
} |
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
// 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 | |
) |
Author
soulduse
commented
Apr 11, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment