Retrofit Adapter - Krunal-Kevadiya/kotlin-common GitHub Wiki

Using Sealed Factory

  • SealedApiResults utilizes Kotlin's sealed classes to provide a safe way of handling http network calls.

How to Used

//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")
}

Using LiveData Factory

  • 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.

How to Used

//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")
}

Used Coroutines

  • 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.

How to Used

//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")
}

Used RxErrorHandling

How to Used

//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.