Created
July 13, 2020 13:51
-
-
Save sberoch/2877bd07d2b3b54308c021dbc33fc4df to your computer and use it in GitHub Desktop.
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
abstract class BaseDataSource { | |
protected suspend fun <T> getResult(call: suspend () -> Response<T>): Resource<T> { | |
try { | |
val response = call() | |
if (response.isSuccessful) { | |
val body = response.body() | |
if (body != null) return Resource.success(body) | |
} | |
return error(" ${response.code()} ${response.message()}") | |
} catch (e: Exception) { | |
return error(e.message ?: e.toString()) | |
} | |
} | |
private fun <T> error(message: String): Resource<T> { | |
Log.e("remoteDataSource", message) | |
return Resource.error("Network call has failed for a following reason: $message") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment