Hi Carlos,
It’ll seems weird to say it but, in the way, the “problem” here is that you’re using ELFileBody which takes care of resolving values from the Session on its own.
There is absolutely nothing wrong with ELFileBody, on the contrary. But in the case of ELFileBody, doing what I suggested indeed add an unwanted and unneeded indirection.
This would be completely different if you relied on StringBody, combined with Scala’s String interpolation/Fastring. In that case you could need and want to have a strongly-typed template-based system, where session lambdas could prove very useful.
To show you an example, I’ll take an example from a set of simulations I’m working on :
I’m building load tests for a set of SOAP-based web services.
For some of those web-services, there are parameters that are optional, and ELFileBody is not powerful to handle that, so we need the full power of a programming language, and went on a Fastring-based template system, to factor out and simplify the use of the request.
Every request relies on the string template of the request and class holding all the request parameters.
So we end up with something like it :
`
object ASoapCallTemplate {
case class Params(userId: Int, username: String, password: String)
def template(params: Params): String = fast"""…"""
}
`
To build request, we also followed a common pattern for every template :
def request(params: Expression[Params]) = http("...") .post("/myurl") .body(StringBody(session => template(params(session))))
And in a case like that, using a session function, through the use of an Expression[Params], make sense, as it still allows to fetch data from the session, but ensure that the types are correct (to a limited extent, as we need to cast data from the Session to the required type using as, but it’s better than nothing ;)) :
val buildSoapCallParams: Expression[Params] = (session: Session) => Params( session("userId").as[Int], session("username").as[String], session("password").as[String]).success
Of course, not everybody need that level of flexibility.
But that’s a case where passing around session functions is really helpful.
And thanks for your kind words
Cheers,
Pierre