Package

com.thoughtworks.dsl.keywords.akka

http

Permalink

package http

Visibility
  1. Public
  2. All

Type Members

  1. sealed trait TApply[L] extends Any

    Permalink

    This TApply keyword can be used to extract the value of akka.http.scaladsl.server.Directives, without additional indentation levels of curly brackets.

    This TApply keyword can be used to extract the value of akka.http.scaladsl.server.Directives, without additional indentation levels of curly brackets.

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

      The 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"
    

Value Members

  1. object TApply extends LowPriorityTApply

    Permalink

Ungrouped