SealedApiResults utilizes Kotlin's sealed classes to provide a safe way of handling http network calls.
fun onResult(result: SealedApiResult<String>) =
when(result) {
is Ok200 -> println(result.body)
is Unauthorized401 -> println("Unauthorized")
is Some -> println("Unhandled response: ${result.code}")
is NetworkError -> result.e.printStackTrace()
}
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.
SealedApiResults is available on JCenter.
For Gradle users, add the one of the following 'compile' sections to your build.gradle
:
repositories {
jcenter()
}
dependencies {
compile "com.nhaarman:sealedapiresults:x.x.x"
compile "com.nhaarman:sealedapiresults-retrofit:x.x.x"
compile "com.nhaarman:sealedapiresults-retrofit-rx:x.x.x"
}