Added ApiResult Extensions.
This commit is contained in:
parent
e62d244645
commit
dbfa709923
|
@ -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".
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue