How can one collapse these directives as much as possible?
I was able to collapse three directives into mainHeader but how can include authenticate and responseWithMediaType as well?
val mainHeaders = cors & headerValueByName("Access_Token") & headerValueByName("Tenant_Name")
override def getLayer = path("api" / Segment / "model" / "layers") { modeledSystemName =>
mainHeaders { (accessToken, tenantName) =>
authenticate(validate(accessToken)) { authInfo =>
respondWithMediaType(`application/json`) {
get {
onComplete(service.getlayers(modeledSystemName, tenantName)) {
case Success(layers) => complete(layers)
//case Success(None) => complete(NotFound, "Release version not found")
case Failure(ex) => complete(InternalServerError, s"An error occurred: ${ex.getMessage}")
}
}
}
}
}
}
Spray routes are super compossible so you can do whatever you want. For example I would write this route like this:
Let me explain this code. First, I created the directive
authWithHeader
that reads header value and then do authentication with this value. It is super simple and all magic done byflatMap
.Then, I created
authenticatedJsonRoute
which is just a composition of other simple directives. Composition magic is done with&
operator (I pronounce it as "and then" in this context) and those directives will be executed in order.Lastly, I used my composed directive,
authenticatedJsonRoute
, in a complete routegetLayer
.