Last active
May 13, 2021 12:43
-
-
Save projectdelta6/f751214d19b31ef999afb091344f829a to your computer and use it in GitHub Desktop.
API Response sealed class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//package ... | |
import java.net.UnknownHostException | |
sealed class APIResponse<T>(val success: Boolean) { | |
class APISuccess<T>(val data: T) : APIResponse<T>(true) | |
sealed class APIFail<T>(val message: String) : APIResponse<T>(false) { | |
class APIUnsuccessful<T>(val code: Int, message: String) : APIFail<T>(message) { | |
constructor(other: APIUnsuccessful<out Any>) : this(other.code, other.message) | |
} | |
class APINetworkError<T>(message: String, val exception: UnknownHostException) : | |
APIFail<T>(message) { | |
constructor(exception: UnknownHostException) : this("Network Error", exception) | |
constructor(other: APINetworkError<out Any>) : this(other.message, other.exception) | |
} | |
class APIError<T>(message: String, val exception: Throwable) : APIFail<T>(message) { | |
constructor(exception: Throwable) : this("Something went wrong", exception) | |
constructor(other: APIError<out Any>) : this(other.message, other.exception) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleRepo { | |
suspend fun getSomeAPIData(): APIResponse<SomeObjectModel> { | |
return try { | |
val response: Response<SomeObjectModel> = apiService.someAPICall() | |
val body = response.body() | |
if(response.isSuccessful && body != null) { | |
APIResponse.APISuccess(body) | |
} else { | |
Log.e(this, "Error in someAPIcall, code: ${response.code()}, " + | |
"massage: " + "${response.message() ?: response.errorBody()}") | |
APIResponse.APIFail.APIUnsuccessful(response.code(), response.message() ?: response.errorBody().toString()) | |
} | |
} catch(e: UnknownHostException) { | |
Log.w(this, "Network error in someAPIcall companies", e) | |
APIResponse.APIFail.APINetworkError(e) | |
} catch(e: Exception) { | |
Log.e(this, "Error in someAPIcall companies", e) | |
APIResponse.APIFail.APIError(e) | |
} | |
} | |
} | |
class ExampleClass { | |
suspend fun foo() { | |
when(val response = ExampleRepo.getSomeAPIData()) { | |
is APIResponse.APISuccess -> TODO("Do something with response.data") | |
is APIResponse.APIFail.APIUnsuccessful -> TODO("API call was not successfull, check response.code & response.message") | |
is APIResponse.APIFail.APINetworkError -> TODO("Probably no internet connection...") | |
is APIResponse.APIFail.APIError -> TODO("Something else went wrong...") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment