Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAU Introduce setHeader and deprecate replaceHeader and addHeaders #147

Merged
merged 2 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,15 @@ trait RequestBuilder {

// support functions

/** Adds the header. If the header has already been defined (e.g. from HeaderCarrier), it will be replaced.
* It does not affect headers not mentioned.
*/
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 @@ -104,15 +104,18 @@ final class RequestBuilderImpl(

// -- Transform helpers --

private def replaceHeaderOnRequest(request: WSRequest, header: (String, String)): WSRequest = {
private def replaceHeaderOnRequest(request: WSRequest, headers: (String, String)*): WSRequest = {
def denormalise(hdrs: Map[String, Seq[String]]): Seq[(String, String)] =
hdrs.toList.flatMap { case (k, vs) => vs.map(k -> _) }
val hdrsWithoutKey = request.headers.filterKeys(!_.equalsIgnoreCase(header._1)).toMap // replace existing header
request.withHttpHeaders(denormalise(hdrsWithoutKey) :+ header : _*)
val hdrsWithoutKey = request.headers.filterKeys(k => !headers.map(_._1.toLowerCase).contains(k.toLowerCase)).toMap // replace existing header
request.withHttpHeaders(denormalise(hdrsWithoutKey) ++ headers : _*)
}

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

override def replaceHeader(header: (String, String)): RequestBuilderImpl =
transform(replaceHeaderOnRequest(_, header))
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