Skip to content

Commit

Permalink
Copy over the new directive to the cats file
Browse files Browse the repository at this point in the history
  • Loading branch information
olivergrabinski committed Oct 6, 2023
1 parent 7f56d21 commit 6d323cc
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.epfl.bluebrain.nexus.delta.sdk.ce
import akka.http.scaladsl.model.MediaTypes.{`application/json`, `text/html`}
import akka.http.scaladsl.model.StatusCodes.{Redirection, SeeOther}
import akka.http.scaladsl.model._
import akka.http.scaladsl.model.headers.{`Last-Event-ID`, Accept}
import akka.http.scaladsl.model.headers.{`Accept-Encoding`, `Last-Event-ID`, Accept, RawHeader}
import akka.http.scaladsl.server.ContentNegotiator.Alternative
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server._
Expand Down Expand Up @@ -164,4 +164,19 @@ trait DeltaDirectives extends UriDirectives {
}
case None => provide(Offset.Start)
}

/** Injects a `Vary: Accept,Accept-Encoding` into the response */
def varyAcceptHeaders: Directive0 =
vary(Set(Accept.name, `Accept-Encoding`.name))

private def vary(headers: Set[String]): Directive0 =
respondWithHeader(RawHeader("Vary", headers.mkString(",")))

private def respondWithHeader(responseHeader: HttpHeader): Directive0 =
mapSuccessResponse(r => r.withHeaders(r.headers :+ responseHeader))

private def mapSuccessResponse(f: HttpResponse => HttpResponse): Directive0 =
mapRouteResultPF {
case RouteResult.Complete(response) if response.status.isSuccess => RouteResult.Complete(f(response))
}
}

0 comments on commit 6d323cc

Please sign in to comment.