safeApiCall

suspend fun <T : Any> safeApiCall(dispatcher: CoroutineDispatcher, emitResult: Boolean = true, apiCall: suspend () -> T): ApiResult<T>

Extension function to safe wrap an api call and parse possible errors codes.

Return

Returns subclass of sealed class ApiResult
ApiResult.Success -> Returns if api call was successful and response was successfully parsed to T.
ApiResult.EmptySuccess -> Returns if api call was successful emitResult is set to false.
ApiResult.GenericError -> Returns if a generic error occurred during execution of the api call or parsing of the call's response.
ApiResult.NetworkError -> Returns if a network error occurred\

Parameters

T

Type of the object which the (successful) response should be parsed to

dispatcher

CoroutinesDispatcher to handle exceptions

emitResult

Boolean which decides whether a successful response should contain an object as ApiResult.Success or should be returned as ApiResult.EmptySuccess

apiCall

Suspend function representing an api call which should be executed in a safe wrap