From bf0120356de5e50235e8ef7a04e6cfeb0b46eb13 Mon Sep 17 00:00:00 2001 From: adamw Date: Wed, 8 Jan 2025 12:01:39 +0100 Subject: [PATCH] add header example --- .../examples/wrapper/addHeaderBackend.scala | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 examples/src/main/scala/sttp/client4/examples/wrapper/addHeaderBackend.scala diff --git a/examples/src/main/scala/sttp/client4/examples/wrapper/addHeaderBackend.scala b/examples/src/main/scala/sttp/client4/examples/wrapper/addHeaderBackend.scala new file mode 100644 index 000000000..f5a738d30 --- /dev/null +++ b/examples/src/main/scala/sttp/client4/examples/wrapper/addHeaderBackend.scala @@ -0,0 +1,36 @@ +// {cat=Backend wrapper; effects=Synchronous; backend=HttpClient}: A backend which adds a header to all outgoing requests + +//> using dep com.softwaremill.sttp.client4::core:4.0.0-M22 + +package sttp.client4.examples.wrapper + +import sttp.capabilities.Effect +import sttp.client4.* +import sttp.client4.wrappers.DelegateBackend +import sttp.model.Header + +// If some headers should be added to each request, the best approach is to create a request description which includes +// these headers. Then, specific requests can use such a request template, just as `basicRequest` is used to create +// requests normally (it already includes some a default `Accept-Encoding` header). However, if the request description +// is for any reason outside the control of the user, a backend wrapper can be used to add headers to each request. + +class AddHeaderBackendWrapper[F[_], P](delegate: GenericBackend[F, P], headers: List[Header]) + extends DelegateBackend(delegate): + override def send[T](request: GenericRequest[T, P with Effect[F]]): F[Response[T]] = + delegate.send(headers.foldLeft(request) { case (r, h) => r.header(h) }) + +object AddHeaderBackendWrapper: + def apply(backend: WebSocketSyncBackend, headers: List[Header]): WebSocketSyncBackend = + new AddHeaderBackendWrapper(backend, headers) with WebSocketSyncBackend {} + + // depending on the backend & effect type used, other "apply" variants might be needed here + +@main def addHeaderBackend(): Unit = + val backend: WebSocketSyncBackend = AddHeaderBackendWrapper( + DefaultSyncBackend(), + List(Header("X-My-Header1", "value1"), Header("X-My-Header2", "value2")) + ) + val response: Response[Either[String, String]] = basicRequest.get(uri"https://httpbin.org/get").send(backend) + + println("Response:") + println(response.body)