I have a service which returns an Option[ProductDoc] in a Future (as an akka ask)
How do I respond in spray routing so that a valid product repsonds with a product but an unknown but well formed one returns a 404?
I want the code to fill in the gap here :
get {
path("products" / PathElement) { productID:String =>
val productFuture = (productService ? ProductService.Get(productID)).mapTo[Option[ProductDoc]]
// THE CODE THAT GOES HERE SO THAT
// IF PRODUCT.ISDEFINED RETURN PRODUCT ELSE REJECT
}
}
The only way I can get to work is with this abomination :
get {
path(PathElement) { productID:String =>
val productFuture = (productService ? ProductService.Get(productID)).mapTo[Option[ProductDoc]]
provide(productFuture).unwrapFuture.hflatMap {
case x => provide(x)
} { hResponse:shapeless.::[Option[ProductDoc], HNil] =>
hResponse.head match {
case Some(product) => complete(product)
case None => reject
}
}
}
}
This can't be the correct way to achieve this, surely? This seems like a pretty simple pattern that must have been solved by someone already!