2
votes

I'm attempting to build a custom Akka HTTP directive that asynchronously maps the response to another. Given the following definition:

trait ResponseFilter {
  def apply(request: HttpRequest, response: HttpResponse): Future[HttpResponse]
}

I would like to build a directive similar to this (which doesn't compile):

def filterResponse(responseFilter: ResponseFilter): Directive0 = {
  extractRequest { request =>
    mapResponse { response =>
      responseFilter(request, response)
    }
  }
}

The compiler error is:

...type mismatch;
[error]  found   : scala.concurrent.Future[akka.http.scaladsl.model.HttpResponse]
[error]  required: akka.http.scaladsl.model.HttpResponse
[error]         responseFilter(request, response)
[error]               ^
[error] one error found

which makes sense, since mapResponse is synchronous.

I'm aware of onSuccess but it doesn't seem to help matters.

1
What's the compiler error message? - lutzh
Updated the question with the compiler error. - David van Geest

1 Answers

0
votes

I spent a bit of quality time with the Akka HTTP source, and came up with the following solution which appears to work as expected:

  def filterResponse(responseFilter: ResponseFilter): Directive0 = {
    Directive { inner => ctx =>
      implicit val ec = ctx.executionContext

      inner(())(ctx).flatMap {
        case result: Complete =>
          responseFilter(ctx.request, result.response).map { filteredResponse =>
            RouteResult.Complete(filteredResponse)
          }
        case incomplete => Future.successful(incomplete)
      }
    }
  }

If there's a better way to do it, please let me know!