Skip to content

Instantly share code, notes, and snippets.

@bijancn
Last active May 31, 2018 11:21
Show Gist options
  • Save bijancn/e93151bac653cb3b0306dd515952d699 to your computer and use it in GitHub Desktop.
Save bijancn/e93151bac653cb3b0306dd515952d699 to your computer and use it in GitHub Desktop.
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