Last active
March 25, 2020 07:28
-
-
Save navi25/67176730f5595b3f1fb5095062a92f15 to your computer and use it in GitHub Desktop.
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
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