Skip to content

Instantly share code, notes, and snippets.

@blaZ3
Created May 23, 2021 16:49
Show Gist options
  • Save blaZ3/0eb8227f84f49e53a750772dce8eccee to your computer and use it in GitHub Desktop.
Save blaZ3/0eb8227f84f49e53a750772dce8eccee to your computer and use it in GitHub Desktop.
BaseService for network calls
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