Last active
June 1, 2024 01:05
-
-
Save ElianFabian/ae6833dc6aec026db6ab1de9b2d2dff1 to your computer and use it in GitHub Desktop.
Network utilities for Android.
This file contains 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
sealed interface NetworkRequestError { | |
data class HttpError( | |
val code: Int, | |
val message: String?, | |
) : NetworkRequestError | |
data object IoError : NetworkRequestError | |
data object UnknownError : NetworkRequestError | |
} |
This file contains 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
import kotlin.contracts.ExperimentalContracts | |
import kotlin.contracts.contract | |
sealed interface NetworkResult<out T : Any> { | |
sealed interface Failure : NetworkResult<Nothing> | |
data object NoInternetConnection : Failure | |
data class RequestError(val error: NetworkRequestError) : Failure | |
data class Success<out T : Any>(val data: T) : NetworkResult<T> | |
} | |
@OptIn(ExperimentalContracts::class) | |
fun <T : Any> NetworkResult<T>.isSuccessful(): Boolean { | |
contract { | |
returns(true) implies (this@isSuccessful is NetworkResult.Success<T>) | |
returns(false) implies (this@isSuccessful is NetworkResult.Failure) | |
} | |
return this is NetworkResult.Success<T> | |
} | |
@OptIn(ExperimentalContracts::class) | |
fun <T : Any> NetworkResult<T>.isFailure(): Boolean { | |
contract { | |
returns(false) implies (this@isFailure is NetworkResult.Success<T>) | |
returns(true) implies (this@isFailure is NetworkResult.Failure) | |
} | |
return this is NetworkResult.Failure | |
} |
This file contains 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
sealed interface Resource<out D, out E> { | |
class Success<out D>(val data: D) : Resource<D, Nothing> | |
class Error<out E>(val error: E) : Resource<Nothing, E> | |
companion object | |
} |
This file contains 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
import retrofit2.HttpException | |
import java.io.IOException | |
import kotlin.coroutines.cancellation.CancellationException | |
suspend inline fun <D> Resource.Companion.safeApiCall( | |
crossinline call: suspend () -> D, | |
): Resource<D, NetworkRequestError> { | |
try { | |
val response = call() | |
return Resource.Success(response) | |
} | |
catch (e: CancellationException) { | |
throw e | |
} | |
catch (e: HttpException) { | |
e.printStackTrace() | |
return Resource.Error( | |
NetworkRequestError.HttpError( | |
code = e.code(), | |
message = e.message().takeIf { it.isNotBlank() }, | |
) | |
) | |
} | |
catch (e: IOException) { | |
e.printStackTrace() | |
return Resource.Error(NetworkRequestError.IoError) | |
} | |
catch (e: Exception) { | |
e.printStackTrace() | |
return Resource.Error(NetworkRequestError.UnknownError) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment