Grantas33
04/30/2020, 1:06 PMEamonn
04/30/2020, 1:51 PMphldavies
04/30/2020, 3:13 PMsealed class ApiResponse {
data class Device(val id: String, val status: String)
data class Error(val error: String, val message: String, val status: Int)
}
suspend fun fetchDevice(id: String) = client.get<HttpResponse> {
url {
takeFrom(baseUri)
path(id)
}
}.let {
when {
it.status.isSuccess() -> it.receive<ApiResponse.Device>()
else -> it.receive<ApiResponse.Error>()
}
}
runBlocking {
when(val result = fetchDevice("bob")) {
is ApiResponse.Error -> error("failed to receive device due to: ${result.message}")
is ApiResponse.Device -> println(result.status)
}
}
Grantas33
04/30/2020, 3:35 PMexpectSuccess = false
in the HttpClient config