-
-
Save skydoves/49009ce771e13462a3ea68cbcc1e8eb1 to your computer and use it in GitHub Desktop.
handle_api_function
This file contains 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
suspend fun <T : Any> handleApi( | |
execute: suspend () -> Response<T> | |
): NetworkResult<T> { | |
return try { | |
val response = execute() | |
val body = response.body() | |
if (response.isSuccessful && body != null) { | |
NetworkResult.Success(body) | |
} else { | |
NetworkResult.Error(code = response.code(), message = response.message()) | |
} | |
} catch (e: HttpException) { | |
NetworkResult.Error(code = e.code(), message = e.message()) | |
} catch (e: Throwable) { | |
NetworkResult.Exception(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment