handleSync

Signature

def handleSync(handler: HttpRequest 
def handleSync(handler: PartialFunction[HttpRequest, HttpResponse]): StandardRoute 
def handleSync( handler: PartialFunction[HttpRequest, HttpResponse], rejections: Seq[Rejection]): StandardRoute 

Description

Creates a RouteRoute that handles the request using a function or PartialFunction from HttpRequestHttpRequest to a HttpResponseHttpResponse.

This directive can be used to include components into a routing tree that have been defined only in terms of the low-level model classes.

This is a strict version of handle.

Example

Scala
sourceval handler: PartialFunction[HttpRequest, HttpResponse] = {
  case HttpRequest(HttpMethods.GET, Uri.Path("/value"), _, _, _) => HttpResponse(entity = "23")
}

val route =
  concat(
    handleSync(handler),
    complete("fallback"))

// tests:
Get("/value") ~> route ~> check {
  status shouldEqual StatusCodes.OK
  responseAs[String] shouldEqual "23"
}

// Uri doesn't match so function is never invoked and the request is rejected and the
// fallback completes the request.
Get("/other") ~> route ~> check {
  status shouldEqual StatusCodes.OK
  responseAs[String] shouldEqual "fallback"
}