sealed trait TApply[L] extends Any
This TApply keyword can be used to extract the value of akka.http.scaladsl.server.Directives, without additional indentation levels of curly brackets.
- Source
- TApply.scala
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.import akka.http.scaladsl.server.Directives._ def extractParametersWithBrackets = { get { parameters("p1", "p2") { (p1, p2) => complete(s"$p1, $p2!") } } } Get("/?p1=Hello&p2=World") ~> extractParametersWithBrackets ~> check { responseAs[String] should be("Hello, World!") }
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!") }
, 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.
import akka.http.scaladsl.server.Directives._ import com.thoughtworks.dsl.keywords.Using import com.thoughtworks.dsl.keywords.akka.http.TApply import java.nio.file.Files.newDirectoryStream import java.nio.file.Paths import scala.collection.JavaConverters._ def currentDirectoryRoute = pathPrefix("current-directory") { get { val glob = !TApply(parameters("glob")) val currentDirectory = !Using(newDirectoryStream(Paths.get(""), glob)) path("file-names") { complete(currentDirectory.iterator.asScala.map(_.toString).mkString(",")) } ~ path("number-of-files") { complete(currentDirectory.iterator.asScala.size.toString) } } }
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.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") }
- Note
This TApply class can be found in the following library:
libraryDependencies += "com.thoughtworks.dsl" %% "keywords-akka-http-tapply" % "latest.release"
- See also
com.thoughtworks.dsl.domains.akka.http for creating custom Akka HTTP directives with the help of Dsl.scala.
- Alphabetic
- By Inheritance
- TApply
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Abstract Value Members
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (TApply[L], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
- def ensuring(cond: (TApply[L]) ⇒ Boolean, msg: ⇒ Any): TApply[L]
- def ensuring(cond: (TApply[L]) ⇒ Boolean): TApply[L]
- def ensuring(cond: Boolean, msg: ⇒ Any): TApply[L]
- def ensuring(cond: Boolean): TApply[L]
-
def
equals(arg0: Any): Boolean
- Definition Classes
- Any
- def formatted(fmtstr: String): String
-
def
hashCode(): Int
- Definition Classes
- Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
toString(): String
- Definition Classes
- Any
- def →[B](y: B): (TApply[L], B)