In addition, this TApply keyword can be used together with other keywords.
For example, you can use com.thoughtworks.dsl.keywords.Using to automatically manage resources.
With the help of the Using keyword, the currentDirectoryRoute will open the current directory according to the glob pattern extract from the query parameter, and automatically close the directory after the HTTP request is processed completely.
Get("/current-directory/file-names?glob=*.md") ~> currentDirectoryRoute ~> check {
responseAs[String] should be("README.md")
}
Get("/current-directory/file-names?glob=*.*") ~> currentDirectoryRoute ~> check {
responseAs[String].split(',') should (contain("secret.sbt") and contain("build.sbt") and contain("README.md"))
}
Get("/current-directory/number-of-files?glob=*.sbt") ~> currentDirectoryRoute ~> check {
responseAs[String] should be("2")
}
,
Ordinary Akka HTTP DSL requires additional brackets to perform directives.
For example, the following extractParametersWithBrackets extracts query parameters p1 and p2 with the help of the directive akka.http.scaladsl.server.Directives.parameters.
When you have lots of directives, you will experience the callback hell problem,
as the directives become heavily nested scopes and the code become too complicated to read.
Those brackets can be avoided with the help of this TApply keyword:
def extractParametersWithoutBrackets = {
get {
val (p1, p2) = !TApply(parameters("p1", "p2"))
complete(s"$p1, $p2!")
}
}
Get("/?p1=Hello&p2=World") ~> extractParametersWithoutBrackets ~> check {
responseAs[String] should be("Hello, World!")
}
Note
This TApply class can be found in the following library:
This TApply keyword can be used to extract the value of akka.http.scaladsl.server.Directives, without additional indentation levels of curly brackets.
In addition, this TApply keyword can be used together with other keywords. For example, you can use com.thoughtworks.dsl.keywords.Using to automatically manage resources.
With the help of the
Using
keyword, thecurrentDirectoryRoute
will open the current directory according to the glob pattern extract from the query parameter, and automatically close the directory after the HTTP request is processed completely.Ordinary Akka HTTP DSL requires additional brackets to perform directives. For example, the following
extractParametersWithBrackets
extracts query parametersp1
andp2
with the help of the directive akka.http.scaladsl.server.Directives.parameters.When you have lots of directives, you will experience the callback hell problem, as the directives become heavily nested scopes and the code become too complicated to read. Those brackets can be avoided with the help of this TApply keyword:
This TApply class can be found in the following library:
com.thoughtworks.dsl.domains.akka.http for creating custom Akka HTTP directives with the help of Dsl.scala.