Added ApiResult Extensions.

This commit is contained in:
Neil Marietta 2021-03-05 15:15:58 +01:00
parent e62d244645
commit dbfa709923
2 changed files with 23 additions and 1 deletions

View File

@ -44,7 +44,7 @@ class ApiProvider(
): ApiManager<out Api> = get(sessionProvider.getSessionId(userId))
inline fun <reified Api : BaseRetrofitApi> get(
sessionId: SessionId? = null,
sessionId: SessionId? = null
): ApiManager<out Api> {
// ConcurrentHashMap does not allow null to be used as a key or value.
// If sessionId == null -> sessionName = "null".

View File

@ -164,3 +164,25 @@ open class ApiException(val error: ApiResult.Error) : Exception(
else error.cause?.message,
error.cause
)
/**
* Performs the given [action] if this instance represents an [ApiResult.Error].
* Returns the original `Result` unchanged.
*/
inline fun <T> ApiResult<T>.onError(
action: (value: ApiResult.Error) -> Unit
): ApiResult<T> {
if (this is ApiResult.Error) action(this)
return this
}
/**
* Performs the given [action] if this instance represents an [ApiResult.Success].
* Returns the original `Result` unchanged.
*/
inline fun <T> ApiResult<T>.onSuccess(
action: (value: T) -> Unit
): ApiResult<T> {
if (this is ApiResult.Success) action(value)
return this
}