Rafa
Rafa

Reputation: 3349

Get context from Main Actor in Akka

I'm attempting to shoot fire off a POST request like the one in the Akka docs showed here. http://doc.akka.io/docs/akka-http/current/scala/http/client-side/request-level.html#request-level-api

However, I'm trying to do it as inside of another defined class. If I try to add in anything that requires the Actor context like val http = HTTP(context.system) I get an error. How can I pass in the context into the class I'm trying to make the POST request from?

trait CustomerProfilesComponent {
  def customerProfileManager: CustomerService.Async
}

object DefaultCustomerProfiles {
  private case class ProfileUpdateData(path: Seq[String], data: JsObject, metadata: JsObject)
}

trait DefaultCustomerProfiles extends CustomerProfilesComponent {
  self: DatabaseComponent with SourceVersionComponent with ExecutionContextComponent =>

  import DefaultCustomerProfiles._

  lazy val customerProfileManager = new CustomerService.Async {

    import db.api._
    import AvroConverter._

    override def getVersion : Future[AvroVersion] = {
      Future.successful(toAvro(sourceVersion))
    }
}

Upvotes: 0

Views: 2050

Answers (1)

Bianca Tesila
Bianca Tesila

Reputation: 807

What you need is actually an actor system. Posting from the akka-http docs:

The request-level API is implemented on top of a connection pool that is shared inside the ActorSystem.

There are two usage scenarios for the request API:

  • use it within an actor - when you can get the actor system via the actor context (like you have tried, but since you are not inside an actor, you don't have an actor context available)
  • use it outside an actor (your case) - when you can get the actor system by providing it as an dependency (either via class/method params or implicit params)

I hope this is helpful.

Upvotes: 1

Related Questions