Last active
May 31, 2018 11:21
-
-
Save bijancn/e93151bac653cb3b0306dd515952d699 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
def loadNumberOfItems(url: String): IO[Either[Exception, Int]] = { | |
val maybeUrl: Option[Url] = | |
Url(url) | |
val eitherUrl: Either[Exception, Url] = | |
maybeUrl.toRight(InvalidUrl) | |
val eitherJson: Either[Exception, IO[Json]] = | |
eitherUrl.map(u => request(u)) | |
val eitherResult: Either[Exception, IO[Either[Exception, Int]]] = | |
eitherJson.map(futureJson => | |
futureJson.map(json => json.getFieldOption("number").flatMap(x => x.as[Int]).toRight(InvalidJson))) | |
val eitherResult: IO[Either[Exception, Either[Exception, Int]]] = | |
eitherResult.sequence | |
eitherResult.map(_.flatten) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment