-
Notifications
You must be signed in to change notification settings - Fork 2
Retrofit Adapter
kevadiya krunal edited this page May 1, 2019
·
1 revision
- SealedApiResults utilizes Kotlin's sealed classes to provide a safe way of handling http network calls.
//Retrofit client
return Retrofit.Builder()
.addCallAdapterFactory(RxSealedCallAdapterFactory())
.build()
//Retrofit interface
@GET("api/logout")
fun logout(@HeaderMap header: HashMap<String, Any>):
Single<SealedApiResult<YourResponseClass>>
//Api result
fun onResult(result: SealedApiResult<YourResponseClass>) =
when(result) {
is SealedApiResult.Some.Success2XX.Ok200 -> println(it.body?.data)
is SealedApiResult.Some.ClientError4XX.Unauthorized401 -> println(it.errorBody?.message)
is SealedApiResult.Some -> println(it.errorBody?.message)
is NetworkError -> println(result.e.toString())
else -> println("Error Unknown")
}- Your service methods can now use LiveData as their return type.
- SealedApiResults utilizes Kotlin's sealed classes to provide a safe way of handling http network calls.
//Retrofit client
return Retrofit.Builder()
.addCallAdapterFactory(LiveDataCallAdapterFactory())
.build()
//Retrofit interface
@GET("api/logout")
fun logout(@HeaderMap header: HashMap<String, Any>):
LiveData<SealedApiResult<YourResponseClass>>
//Api result
fun onResult(result: SealedApiResult<YourResponseClass>) =
when(result) {
is SealedApiResult.Some.Success2XX.Ok200 -> println(it.body?.data)
is SealedApiResult.Some.ClientError4XX.Unauthorized401 -> println(it.errorBody?.message)
is SealedApiResult.Some -> println(it.errorBody?.message)
is NetworkError -> println(result.e.toString())
else -> println("Error Unknown")
}- Your service methods can now use Deferred as their return type
- SealedApiResults utilizes Kotlin's sealed classes to provide a safe way of handling http network calls.
//Retrofit client
return Retrofit.Builder()
.addCallAdapterFactory(CoroutineCallAdapterFactory())
.build()
//Retrofit interface
@GET("api/logout")
fun logout(@HeaderMap header: HashMap<String, Any>):
Deferred<SealedApiResult<YourResponseClass>>
//Api result
fun onResult(result: SealedApiResult<YourResponseClass>) =
when(result) {
is SealedApiResult.Some.Success2XX.Ok200 -> println(it.body?.data)
is SealedApiResult.Some.ClientError4XX.Unauthorized401 -> println(it.errorBody?.message)
is SealedApiResult.Some -> println(it.errorBody?.message)
is NetworkError -> println(result.e.toString())
else -> println("Error Unknown")
}//Retrofit client
return Retrofit.Builder()
.addCallAdapterFactory(RxErrorHandlingCallAdapterFactory.create(context))
.build()
//Retrofit interface
@GET("api/logout")
fun logout(@HeaderMap header: HashMap<String, Any>):
Observable<YourResponseClass>The compiler will complain when you forget to handle a case, such as the NetworkError. This makes for a very safe way of dealing with network results.