Custom Directive that returns case class?

Forgive the noob question. I currently have a Directive set up like:

def combinedDirective: Directive[(String, RemoteAddress)] =
    headerValueByName("blah") & extractClientIP

Is it possible to create a case class that takes a RemoteAddress (for clientIp) and String (for headerValue) and populate that from other directives to be used within a route?

Something like:

def combinedDirective:
Directive[DirectiveCaseClass] = 
DirectiveCaseClass(headerValueByName("blah",  extractClientIP)

I’ve played around with a few of the built in api for Directive but couldn’t quite get anything working. Is what I’m trying to do possible?

Many Thanks!

Yes, see Case Class Extraction • Akka HTTP.

You can try something similar to this:

def combinedDirective: Directive[DirectiveCaseClass] =
    (headerValueByName("blah") & extractClientIP).as(DirectiveCaseClass.apply _)
1 Like