Passing variables in a Spray.io application

I am creating a web service using Spray.io, which sits on top of the main application created with Akka.

When I receive a request, it is processed by a spray route, which in turn sends (using the request) the request to the subject who processes the request and returns a response using the request context.

I authenticate and authorize the user on the initial route, and this authentication / authorization returns a user object containing user data.

I need to have access to this user object in the main Akka application at different points. I don’t want to pass it as a parameter for every message (case class) sent to the actor, because it just seems messy, because sometimes I passed it to the actor so that it could be passed to another, Is there a better / recommended way to make this object available to other participants in the system? Can it be tied to the request context itself or is it bad practice?

thanks

+5
source share
1 answer

If what you are trying to do is to avoid the pattern of passing authentication information when instantiating the case class, you can add an implicit argument list to them:

scala> implicit val i = 1 i: Int = 1 scala> case class X(s: String)(implicit val y: Int) defined class X scala> val x = X("foo") x: X = X(foo) scala> xy res4: Int = 1 

you still pass authentication information with each message, and you cannot use pattern matching in the second argument list, but depending on what you are trying to do, this may work.

+2
source

All Articles