-
-
Save raamcosta/ee6647a3ed0a1f1b4b35b93e1c029127 to your computer and use it in GitHub Desktop.
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
class GetMagicCardUseCase( | |
private val mtgApi : MagicTheGatheringApi | |
) { | |
suspend operator fun invoke(cardName: String) : Result<MagicCard, GetMagicCardUseCaseError> { | |
if (!isCardNameValid(cardName)) { | |
return Error(InvalidCardNameFormat) | |
} | |
return try { | |
Success(mtgApi.getCard(cardName)) | |
} catch (t: Throwable) { | |
val error = when (t) { | |
is IOException -> RemoteServerNotReachedError | |
is HttpException -> HttpUnsuccessfulCodeError(t.code()) | |
else -> UnexpectedApiCommunicationError | |
} | |
return Error(error) | |
} | |
} | |
} | |
sealed class GetMagicCardUseCaseError | |
object InvalidCardNameFormat : GetMagicCardUseCaseError() | |
class HttpUnsuccessfulCodeError(val httpCode: Int) : GetMagicCardUseCaseError() | |
object RemoteServerNotReachedError : GetMagicCardUseCaseError() | |
object UnexpectedApiCommunicationError : GetMagicCardUseCaseError() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment