-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
examples/src/main/scala/sttp/client4/examples/wrapper/addHeaderBackend.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |