How does path method work in akka-http - scala

This is more of a Scala question but inspired by this http-scala example.
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
Based on example path seems like it could be a curried method that takes String as first argument and Route as second although I do not see such implementation in the API. Could someone enlighten me how this works?
Link to docs: http://doc.akka.io/api/akka-http/current/akka/http/javadsl/server/Directives$.html#path(segment:String,inner:java.util.function.Supplier[akka.http.javadsl.server.Route]):akka.http.javadsl.server.Route

No, path is not a curried function. The API gives the signature:
def path[L](pm: PathMatcher[L]): Directive[L]
Path is a simple unary function that takes in a PathMatcher and returns a Directive. Path only seems like it is a curried function because Directive has an apply method which makes it appear to be a function, but it's actually a class.
PathMatcher
By importing Directives._ the following implicit function becomes available:
implicit def _segmentStringToPathMatcher(segment: String): PathMatcher0
Note: PathMatcher0 is defined as PathMatcher[Unit] since it does not pass the matched value along to the inner Route.
Calling path with the argument in the example, path("hello"), is similar to following code:
val directive : Directive[Unit] = path(_segmentStringToPathMatcher("hello"))
Directive
As stated previously, the Directive has an apply method that lazily takes in a Route:
def apply(v1 : => Route) : Route
So your example code is essentially making the following call:
val route =
directive.apply(get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
})
Putting It All Together
Let's simplify the definition of Route and mock some logic to demonstrate what is happening:
type SimpleRoute : HttpRequest => RouteResult
We can now write a version of path using a higher order function. Our simpler version takes in a String and returns a function instead of a Directive. It gets a little tricky because the returned function is also higher order:
def simplePath(pathStr : String) : SimpleRoute => SimpleRoute =
(innerRoute : SimpleRoute) => {
//this is the returned SimpleRoute
(request : HttpRequest) => {
if(request.uri.path.toString equalsIgnoreCase pathStr)
RouteResult.Complete(innerRoute(request))
else
RouteResult.Rejected(Seq.empty[Rejection])
}
}

Related

Akka-HTTP compilation error using custom requireParam() directive

I developed custom generic directive, which will provide param of given type, if it exists, or else reject with my custom exception.
import akka.http.scaladsl.common.NameReceptacle
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.directives.ParameterDirectives.ParamDefAux
import akka.http.scaladsl.server.{Directive1, Route}
class MyCustomException(msg: String) extends Exception(msg)
def requireParam[T](name: NameReceptacle[T])
(implicit pdef: ParamDefAux[NameReceptacle[T], Directive1[T]]): Directive1[T] =
parameter(name).recover { _ =>
throw new MyCustomException(s"${name.name} is missed!")
}
It works ok, if I want to create route, using two parameters, for example:
val negSumParams: Route =
(requireParam("param1".as[Int]) & requireParam("param2".as[Int])) {
(param1, param2) =>
complete((-param1-param2).toString)
}
But if I try to use exactly one parameter, this doesn't compile:
val negParamCompilationFail: Route =
requireParam("param".as[Int]) {
param => // scalac complains about missing type param here
complete((-param).toString)
}
If I use it with pass directive, it works:
val negParamWithPass: Route =
(pass & requireParam("param".as[Int])) { // this pass usage looks hacky
param =>
complete((-param).toString)
}
If I write requireParam() return type explicitly, it works too:
val negParamWithExplicitType: Route =
(requireParam("param".as[Int]): Directive1[Int]) { // DRY violation
param =>
complete((-param).toString)
}
Why do I need these tricks? Why can't it work just with requireParam("param".as[Int])?
Scala version 2.12.1, Akka-HTTP 10.0.10.
This error happens due to the Directive companion object apply method. IT allows to create a Route from a function with parameter (T => Route) => Route:
object Directive {
/**
* Constructs a directive from a function literal.
*/
def apply[T: Tuple](f: (T ⇒ Route) ⇒ Route): Directive[T] =
new Directive[T] { def tapply(inner: T ⇒ Route) = f(inner) }
}
But the T parameter must be a tuple. In your case, the compiler can not build the Route. Your requireParam("param".as[Int]) returns a Directive1[Int] so the apply method doesn´t work because Int is not a Tuple.
To make this work you shoul use tapply method directly:
(requireParam("param1".as[Int])).tapply((param1) =>
complete((-param1._1).toString))
and
val negSumParams2: Route =
(requireParam("param1".as[Int]) & requireParam("param2".as[Int])).tapply {
case (param1, param2) =>
complete((-param1-param2).toString)
}
So it seems that every Directive tries to convert its param to TupleX. For example:
path("order" / IntNumber) returns a Directive[Tuple1[Int]] instead of Directive1[Int]. In your case requireParam("param1".as[Int]) returns Directive1[Int]
Maybe there is a better solution and to avoid tapply
Johannes from Lightbend answered to this question here: https://groups.google.com/forum/#!topic/akka-user/NmQvcrz5sJg Answer is:
You discovered one of the reasons for the magnet pattern. If you use
requireParam("param".as[Int]) { abc => ... } then the { abc => }
block is mistaken as the implicit argument of requireParam. So,
either you are ok with that and require users to use extra parentheses
((requireParam("param".as[Int])) { abc => ... }) or you use the
magnet pattern at this level as well. You can read about the magnet
pattern on the old spray blog
(http://spray.io/blog/2012-12-13-the-magnet-pattern/) or just look
into akka-http sources.
A better way to implement the feature would be just to use the
existing implementation ;) and install a custom rejection handler that
produces whatever output you would like. See
https://doc.akka.io/docs/akka-http/10.0.10/scala/http/routing-dsl/rejections.html#customizing-rejection-handling
for how to do that.

Spray: factor out onSuccess directive

I would like to change the following code fragment in a way that I want to factor out the onSuccess block into a new method. JSON marshalling should still work.
(patch & parameterMap & asJson) { params =>
...
val f:Future[ResposeData]=createResponse(...)
onSuccess(f){complete(_)}
}
I would like to have a method like:
def handleSuccess(f:Future/FutureMagnet)(implicit ...)
A simple refactoring doesn't work for me. I tried a lot of combinations but I can't find either the correct signature or the code working.
Example:
def handleSuccess(f: Future[ResposeData]): Unit = {
onSuccess(f) { complete(_) }
}
Error:(43, 15) type mismatch;
found : scala.concurrent.Future[ResponseData]
required: spray.routing.directives.OnSuccessFutureMagnet
onSuccess(f) {
^
If I now change signature I get another error:
def handleSuccess(f: OnSuccessFutureMagnet)
Error:(44, 18) spray.routing.Directive[f.Out] does not take parameters
onSuccess(f) {
^
Maybe this is just a simple thing to do but I'm new to spray.
So it would be nice if somebody could give a hint.
Thanks
onSuccess takes a function, basically when you use that on your future the value inside the future becomes available and you can complete your route using that value if you want:
case class ResponseData(data: String)
def handleSuccess(f: Future[ResponseData]): Unit = {
onSuccess(f) { responseData =>
complete(_)
}
}
From the comments:
"Unwraps" a Future[T] and runs its inner route after future completion with the future's value as an extraction of type T.
Note also that
If the future fails its failure throwable is bubbled up to the nearest ExceptionHandler.
Maybe you want to use onComplete which returns a Try and you can then match on Success or Failure.

What does the word "Action" do in a Scala function definition using the Play framework?

I am developing Play application and I've just started with Scala. I see that there is this word Action after the equals sign in the function below and before curly brace.
def index = Action {
Ok(views.html.index("Hi there"))
}
What does this code do? I've seen it used with def index = { but not with the word before the curly brace.
I would assume that the name of the function is index. But I do not know what the word Action does in this situation.
This word is a part of Play Framework, and it's an object, which has method apply(block: ⇒ Result), so your code is actually:
def index: Action[AnyContent] = Action.apply({
Ok.apply(views.html.index("Hi there"))
})
Your index method returns an instance of the class Action[AnyContent].
By the way, you're passing a block of code {Ok(...)} to apply method, which (block of code) is actually acts as anonymous function here, because the required type for apply's input is not just Result but ⇒ Result, which means that it takes an anonymous function with no input parameters, which returns Result. So, your Ok-block will be executed when container, received your instance of class Action (from index method), decided to execute this block. Which simply means that you're just describing an action here - not executing - it will be actually executed when Play received your request - and find binding to your action inside routing file.
Also, you don't have to use def here as you always return same action - val or lazy val is usually enough. You will need a def only if you actually want to pass some parameter from routing table (for instance):
GET /clients/:id controllers.SomeController.index(id: Long)
def index(id: Long) = Action { ... } // new action generated for every new request here
Another possible approach is to choose Action, based on parameter:
def index(id: Long) = {
if (id == 0) Action {...} else Action{...}
}
But uasually you can use routing table itself for that, which is better for decoupling. This example just shows that Action is nothing more than return value.
Update for #Kazuya
val method1 = Action{...} //could be def too, no big difference here
// this (code inside Action) gonna be called separately after "index" (if method2 is requested of course)
// notice that it needs the whole request, so it (request) should be completely parsed at the time
val method2 = Action{ req => // you can extract additional params from request
val param1 = req.headers("header1")
...
}
//This is gonna be called first, notice that Play doesn't need the whole request body here, so it might not even be parsed on this stage
def index(methodName: String) = methodName match {
case "method1" => method1
case "method2" => method2
}
GWT/Scala.js use simillar approach for client-server interaction. This is just one possible solution to explain importance of the parameter "methodName" passed from routing table. So, action could be thought as a wrapper over function that in its turn represents a reference to OOP-method, which makes it useful for both REST and RPC purposes.
The other answers deal with your specific case. You asked about the general case, however, so I'll attempt to answer from that perspective.
First off, def is used to define a method, not a function (better to learn that difference now). But, you're right, index is the name of that method.
Now, unlike other languages you might be familiar with (e.g., C, Java), Scala lets you define methods with an expression (as suggested by the use of the assignment operator syntax, =). That is, everything after the = is an expression that will be evaluated to a value each time the method is invoked.
So, whereas in Java you have to say:
public int three() { return 3; }
In Scala, you can just say:
def three = 3
Of course, the expression is usually more complicated (as in your case). It could be a block of code, like you're more used to seeing, in which case the value is that of the last expression in the block:
def three = {
val a = 1
val b = 2
a + b
}
Or it might involve a method invocation on some other object:
def three = Numbers.add(1, 2)
The latter is, in fact, exactly what's going on in your specific example, although it requires a bit more explanation to understand why. There are two bits of magic involved:
If an object has an apply method, then you can treat the object as if it were a function. You can say, for example, Add(1, 2) when you really mean Add.apply(1,2) (assuming there's an Add object with an apply method, of course). And just to be clear, it doesn't have to be an object defined with the object keyword. Any object with a suitable apply method will do.
If a method has a single by-name parameter (e.g., def ifWaterBoiling(fn: => Tea)), then you can invoke the method like ifWaterBoiling { makeTea }. The code in that block is evaluated lazily (and may not be evaluated at all). This would be equivalent to writing ifWaterBoiling({ makeTea }). The { makeTea } part just defines an expression that gets passed in, unevaluated, for the fn parameter.
Its the Action being called on with an expression block as argument. (The apply method is used under the hood).
Action.apply({
Ok("Hello world")
})
A simple example (from here) is as follows (look at comments in code):
case class Logging[A](action: Action[A]) extends Action[A] {
def apply(request: Request[A]): Result = {// apply method which is called on expression
Logger.info("Calling action")
action(request) // action being called on further with the request provided to Logging Action
}
lazy val parser = action.parser
}
Now you can use it to wrap any other action value:
def index = Logging { // Expression argument starts
Action { // Action argument (goes under request)
Ok("Hello World")
}
}
Also, the case you mentioned for def index = { is actually returning Unit like: def index: Unit = {.

Scala map cannot resolve mapper function

Scala noob here.
I'm trying to apply a map in a class method:
class Miner(args: Args) extends Job(args) {
def fetchUrl(url:String) = {
...
}
TextLine(args("input")).map(url: String => fetchUrl(url))
.write(args("output"))
}
this codes breaks complaining about not being able to resolve the symbol fetchUrl.
I've thought that, fetchUrl being a one argument function, I could just omit the argument and do something like:
TextLine(args("input")).map(fetchUrl)
.write(args("output"))
This now breaks saying that I'm missing arguments for the method fetchUrl.
What gives?
Isn't mapTo a curried function?
I imagine you use this function from this object: (google redirects me to that)
mapTo[U](out: Fields)(mf: (String) ⇒ U)(implicit flowDef: FlowDef, mode: Mode, setter: TupleSetter[U]): Pipe
Perhaps you would use it like this: Textline.mapTo(args("input"))(fetchUrl)
You have some examples of mapTo usage at this page, but based on the Pipe object:
https://github.com/twitter/scalding/wiki/Fields-based-API-Reference#map-functions
Excerpt:
val savings =
items.mapTo(('price, 'discountedPrice) -> 'savings) {
x : (Float, Float) =>
val (price, discountedPrice) = x
price - discountedPrice
}
So not based on TextLine in this example, but also curried...this might be a good hint for you.

Scala Implicit parameters by passing a function as argument To feel the adnvatage

I try to feel the advantage of implicit parameters in Scala. (EDITED: special case when anonymous function is used. Please look at the links in this question)
I try to make simple emulation based on this post. Where explained how Action works in PlayFramework. This also related to that.
The following code is for that purpose:
object ImplicitArguments extends App {
implicit val intValue = 1 // this is exiting value to be passed implicitly to everyone who might use it
def fun(block: Int=>String): String = { // we do not use _implicit_ here !
block(2) // ?? how to avoid passing '2' but make use of that would be passed implicitly ?
}
// here we use _implicit_ keyword to make it know that the value should be passed !
val result = fun{ implicit intValue => { // this is my 'block'
intValue.toString // (which is anonymous function)
}
}
println(result) // prints 2
}
I want to get "1" printed.
How to avoid passing magic "2" but use "1" that was defined implicitly?
Also see the case where we do not use implicit in definition, but it is there, because of anonymous function passing with implicit.
EDITED:
Just in case, I'm posting another example - simple emulation of how Play' Action works:
object ImplicitArguments extends App {
case class Request(msg:String)
implicit val request = Request("my request")
case class Result(msg:String)
case class Action(result:Result)
object Action {
def apply(block:Request => Result):Action = {
val result = block(...) // what should be here ??
new Action(result)
}
}
val action = Action { implicit request =>
Result("Got request [" + request + "]")
}
println(action)
}
Implicits don't work like this. There is no magic. They are just (usually) hidden parameters and are therefore resolved when invoking the function.
There are two ways to make your code work.
you can fix the implicit value for all invocations of fun
def fun(block: Int=>String): String = {
block(implicitly[Int])
}
implicitly is a function defined in Predef. Again no magic. Here's it's definition
def implicitly[A](implicit value: A) = value
But this means it will resolve the implicit value when declaring the fun and not for each invocation.
If you want to use different values for different invocations you will need to add the implicit paramter
def fun(block: Int=>String)(implicit value: Int): String = {
block(value)
}
This will now depend on the implicit scope at the call site. And you can easily override it like this
val result = fun{ _.toString }(3)
and result will be "3" because of the explicit 3 at the end. There is, however, no way to magically change the fun from your declaration to fetch values from implicit scope.
I hope you understand implicits better now, they can be a bit tricky to wrap your head around at first.
It seems that for that particular case I asked, the answer might be like this:
That this is not really a good idea to use implicit intValue or implicit request along with implicitly() using only one parameter for the function that accept (anonymous) function.
Why not, because:
Say, if in block(...) in apply() I would use implicitly[Request], then
it does not matter whether I use "implicit request" or not - it will use
request that is defined implicitly somewhere. Even if I would pass my
own request to Action { myOwnRequest =Result }.
For that particular case is better to use currying and two arguments and.. in the second argument - (first)(second) to use implicit
Like this:
def apply(block:Request => Result)(implicit request:Request):Action2
See my little effort around this example/use case here.
But, I don't see any good example so far in regards to how to use implicit by passing the (anonymous) function as argument (my initial question):
fun{ implicit intValue => {intValue.toString}
or that one (updated version):
val action = Action { implicit request =>
Result("Got request [" + request + "]")
}