Skip to content

Commit

Permalink
BAU Introduce setHeader and deprecate replaceHeader and addHeaders
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Jul 5, 2022
1 parent c4f2709 commit a2eb67c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ httpClientV2.post(url"$url").withBody(Json.toJson(payload)).addHeaders(headers).

#### Header manipulation

With `HttpClient`, replacing a header can require providing a customised client implementation (e.g. to replace the user-agent header), or updating the `HeaderCarrier` (e.g. to replace the authorisation header). This can now all be done with the `replaceHeader` on `HttpClientV2` per call. e.g.
With `HttpClient`, replacing a header can require providing a customised client implementation (e.g. to replace the user-agent header), or updating the `HeaderCarrier` (e.g. to replace the authorisation header). This can now all be done with the `setHeader` on `HttpClientV2` per call. e.g.

```scala
httpClientV2.get(url"$url").replaceHeader("User-Agent" -> userAgent).replaceHeader("Authorization" -> authorization).execute[ResponseType]
httpClientV2.get(url"$url").setHeader("User-Agent" -> userAgent).setHeader("Authorization" -> authorization).execute[ResponseType]
```

As well as replacing existing header values, `setHeader` can be used to add new headers too, and in most cases should be used in preference to `addHeaders` where the values are merged with any existing ones (e.g. from HeaderCarrier).

#### Using proxy

With `HttpClient`, to use a proxy requires creating a new instance of HttpClient to mix in `WSProxy` and configure. With `HttpClientV2` this can be done with the same client, calling `withProxy` per call. e.g.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ trait RequestBuilder {

// support functions

/** Adds the header. If the header has already been defined (e.g. from HeaderCarrier), it will be replaced. */
def setHeader(header: (String, String)): RequestBuilder

@deprecated("Use setHeader", "14.5.0")
def replaceHeader(header: (String, String)): RequestBuilder

@deprecated("Use setHeader to add or replace, or use transform(_.addHttpHeaders) to append header values to existing", "14.5.0")
def addHeaders(headers: (String, String)*): RequestBuilder

def withProxy: RequestBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ final class RequestBuilderImpl(
request.withHttpHeaders(denormalise(hdrsWithoutKey) :+ header : _*)
}

override def replaceHeader(header: (String, String)): RequestBuilderImpl =
override def setHeader(header: (String, String)): RequestBuilderImpl =
transform(replaceHeaderOnRequest(_, header))

override def replaceHeader(header: (String, String)): RequestBuilderImpl =
setHeader(header)

override def addHeaders(headers: (String, String)*): RequestBuilderImpl =
transform(_.addHttpHeaders(headers: _*))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ class HttpClientV2Spec
httpClientV2
.put(url"$wireMockUrl/")
.transform(_.withBody(Json.toJson(ReqDomain("req"))))
.replaceHeader("User-Agent" -> "ua2")
.setHeader("User-Agent" -> "ua2")
.execute[ResDomain]
}

Expand All @@ -547,7 +547,7 @@ class HttpClientV2Spec
httpClientV2
.put(url"$wireMockUrl/")
.withBody(Json.toJson(ReqDomain("req")))
.replaceHeader("User-Agent" -> "ua2")
.setHeader("User-Agent" -> "ua2")
.execute[ResDomain]

res.futureValue shouldBe ResDomain("res")
Expand Down

0 comments on commit a2eb67c

Please sign in to comment.