Skip to content

Instantly share code, notes, and snippets.

@raamcosta
Last active November 28, 2021 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raamcosta/ee6647a3ed0a1f1b4b35b93e1c029127 to your computer and use it in GitHub Desktop.
Save raamcosta/ee6647a3ed0a1f1b4b35b93e1c029127 to your computer and use it in GitHub Desktop.
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