Last active
May 28, 2020 12:29
-
-
Save soulduse/442c83b3f8cdd541f93baf2f4f38978f to your computer and use it in GitHub Desktop.
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" | |
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 | |
} | |
} |
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 | |
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" | |
// ... | |
} |
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 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) | |
} | |
} | |
} | |
} | |
} |
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
class NetworkCallback<T> { | |
var success: ((T) -> Unit) ?= null | |
var error: ((Throwable)-> Unit) ?= null | |
} |
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 | |
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) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think I made better code
check this code