From 6d323cc5093dc906569a7ca47be9e6fed9278e4d Mon Sep 17 00:00:00 2001 From: Oliver <20188437+olivergrabinski@users.noreply.github.com> Date: Fri, 6 Oct 2023 13:53:55 +0200 Subject: [PATCH] Copy over the new directive to the cats file --- .../nexus/delta/sdk/ce/DeltaDirectives.scala | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/ce/DeltaDirectives.scala b/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/ce/DeltaDirectives.scala index ad57159de3..743850583b 100644 --- a/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/ce/DeltaDirectives.scala +++ b/delta/sdk/src/main/scala/ch/epfl/bluebrain/nexus/delta/sdk/ce/DeltaDirectives.scala @@ -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._ @@ -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)) + } }