khalid64927 / SealedApiResults

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SealedApiResults

Download

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

Example

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.

Install

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

About

License:Apache License 2.0


Languages

Language:Kotlin 100.0%