Created
May 23, 2021 16:49
-
-
Save blaZ3/0eb8227f84f49e53a750772dce8eccee to your computer and use it in GitHub Desktop.
BaseService for network calls
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
abstract class BaseService { | |
suspend fun <T : Any> apiCall(call: suspend () -> Response<T>): Result<T> { | |
val response: Response<T>? | |
try { | |
response = call.invoke() | |
} catch (ex: Exception) { | |
return NetworkFailure(ex) | |
} | |
if (response.isSuccessful) { | |
response.body()?.let { return Success(it) } ?: return NetworkFailure( | |
IllegalStateException("Response body is null") | |
) | |
} else { | |
return NetworkError(response.code()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment