Skip to content

Instantly share code, notes, and snippets.

@briscula
Forked from navi25/BaseRepository.kt
Created October 1, 2019 07:55
Show Gist options
  • Select an option

  • Save briscula/892bb17d0c69f1da80960623d40e9c32 to your computer and use it in GitHub Desktop.

Select an option

Save briscula/892bb17d0c69f1da80960623d40e9c32 to your computer and use it in GitHub Desktop.
open class BaseRepository{
suspend fun <T : Any> safeApiCall(call: suspend () -> Response<T>, errorMessage: String): T? {
val result : Result<T> = safeApiResult(call,errorMessage)
var data : T? = null
when(result) {
is Result.Success ->
data = result.data
is Result.Error -> {
Log.d("1.DataRepository", "$errorMessage & Exception - ${result.exception}")
}
}
return data
}
private suspend fun <T: Any> safeApiResult(call: suspend ()-> Response<T>, errorMessage: String) : Result<T>{
val response = call.invoke()
if(response.isSuccessful) return Result.Success(response.body()!!)
return Result.Error(IOException("Error Occurred during getting safe Api result, Custom ERROR - $errorMessage"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment