0

I have several components that proxy requests to a REST service and deserializes the result as appropriate. For example, something like:

import akka.http.scaladsl.HttpExt
trait UsersResource {
  val http: HttpExt
  def getUser(id: String): Future[User] = http.singleRequest(HttpRequest(...))
      .flatMap(r => Unmarshal(r.entity).to[User])

  def findUsers(query: Any): Future[List[User]]
}

I'd like to somehow proxy each of these requests, so that I can modify the request (eg. to add headers) or modify the response. Specifically, I'm interested in adding some code that adds:

  • logging and monitoring request/response
  • add cookies to each request
  • add auth to request
  • transform the response body

Since each specific resource typically has these three steps in common (and in some cases this logic is common across all resources), I'd like to change the http: HttpExt field somehow to apply these steps.

Is anything like this possible with Akka HTTP?

I came across this question, which seems to touch on part of this question (specifically the part about logging/monitoring) however the accepted answer appears to be using HTTP Directives on the server side, rather than at the client.

Micheal Hill
  • 1,619
  • 2
  • 17
  • 38
  • Does it have to be through `HttpExt` or was that just an example? – Ramón J Romero y Vigil Dec 06 '18 at 11:49
  • I've been using Akka's HTTP client in quite a few places; so if there's a drop in replacement for `HttpExt` that would also be fine. I would prefer to continue using the Akka client if possible. – Micheal Hill Dec 08 '18 at 07:13
  • Sorry, I wasn't clear. Can the functionality come from something other than `HttpExt` but still using `akka`? – Ramón J Romero y Vigil Dec 08 '18 at 16:23
  • I don't mind if the functionality comes from somewhere else. I guess another way to put this, is: 1) is there some way that Akka expects this behavior to be achieved or 2) if Akka doesn't supply this behavior, what is the standard way to do this? This is assuming that what I'm trying to do is not uncommon, but maybe that's wrong. – Micheal Hill Dec 10 '18 at 04:32

0 Answers0