Trait/Object

com.thoughtworks.dsl.keywords.akka.http

TApply

Related Docs: object TApply | package http

Permalink

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
Examples:
  1. 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, 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")
    }
  2. ,
  3. 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.

    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!")
    }
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.

Linear Supertypes
Any
Known Subclasses
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. TApply
  2. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def directive: Directive[L]

    Permalink
  2. abstract def getClass(): Class[_]

    Permalink
    Definition Classes
    Any

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. def +(other: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to any2stringadd[TApply[L]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (TApply[L], B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to ArrowAssoc[TApply[L]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  7. def ensuring(cond: (TApply[L]) ⇒ Boolean, msg: ⇒ Any): TApply[L]

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to Ensuring[TApply[L]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  8. def ensuring(cond: (TApply[L]) ⇒ Boolean): TApply[L]

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to Ensuring[TApply[L]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  9. def ensuring(cond: Boolean, msg: ⇒ Any): TApply[L]

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to Ensuring[TApply[L]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  10. def ensuring(cond: Boolean): TApply[L]

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to Ensuring[TApply[L]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  11. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  12. def formatted(fmtstr: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to StringFormat[TApply[L]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  13. def hashCode(): Int

    Permalink
    Definition Classes
    Any
  14. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  15. def toString(): String

    Permalink
    Definition Classes
    Any
  16. def [B](y: B): (TApply[L], B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from TApply[L] to ArrowAssoc[TApply[L]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from Any

Inherited by implicit conversion any2stringadd from TApply[L] to any2stringadd[TApply[L]]

Inherited by implicit conversion StringFormat from TApply[L] to StringFormat[TApply[L]]

Inherited by implicit conversion Ensuring from TApply[L] to Ensuring[TApply[L]]

Inherited by implicit conversion ArrowAssoc from TApply[L] to ArrowAssoc[TApply[L]]

Ungrouped