scalaakkaakka-typed

How do I make a client http request inside of a akka typed actor?


It seems that the http client request API is for classic actors only:

val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))
    
   responseFuture
     .onComplete {
       case Success(res) => println(res)
       case Failure(_)   => sys.error("something wrong")
     }

I have a akka typed actor and not sure how to make external http requests to my API.

When I added the above inside of my typed actor, I got this error:

could not find implicit value for parameter system: akka.actor.ClassicActorSystemProvider

What options do I have?


Solution

  • Typed actor system can be easily converted to a classical one and used to send your http request via akka-http.

    For example, if there is a val of type akka.actor.typed.ActorSystem, you can convert it to a classical one in following way

    val system: akka.actor.typed.ActorSystem[_] = ???
    implicit val classic = system.classicSystem
    

    So now, any method requiring implicit actorSystem: akka.actor.ActorSystem method parameter will work.

    And from within a typed actor, you can get reference to a context and then to typed actor system, which then converted to classical one

    val behavior = Behaviors.setup[Int] { ctx =>
      implicit val classic = ctx.system.classicSystem
      ???
    }