Try to search your question here, if you can't find : Ask Any Question Now ?

How to use Either with IO properly?

HomeCategory: stackoverflowHow to use Either with IO properly?
Avatarwillson asked 5 months ago

I have AkkaHttp client and cats library. I’d like to escape throwing exception, so I wrote this code:

class AkkaHttpJokeClient(url: String)(implicit system: ActorSystem) extends JokeClient[IO] {
  override def getJoke(): IO[Either[Throwable, Joke]] = {
    implicit val materializer: ActorMaterializer = ActorMaterializer()
    implicit val ec: ExecutionContext = system.dispatcher

    IO.fromFuture(IO {
      Http()
        .singleRequest(HttpRequest(uri = url))
        .flatMap(Unmarshal(_).to[String])
        .map(x => Try{x.parseJson.convertTo[Joke]}.toEither)
      })
  }
}

It works, but I have a few problems with it. The first one is connection problem isn’t solved. The second: I think it could be done easier, isn’t it? I can’t find right methods to do it better.

1 Answers
Best Answer
Avatarnaveen answered 5 months ago
Your Answer

12 + 7 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com