Skip to content

Instantly share code, notes, and snippets.

@sberoch
Created July 13, 2020 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sberoch/2877bd07d2b3b54308c021dbc33fc4df to your computer and use it in GitHub Desktop.
Save sberoch/2877bd07d2b3b54308c021dbc33fc4df to your computer and use it in GitHub Desktop.
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