Skip to content

Commit

Permalink
BDOG-1421 Rename HttpClientImpl to PlayHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Sep 29, 2021
1 parent cd6fb44 commit 08e685d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
| `withUserAgent` added | Minor | Optional change |
| `withTransformRequest` added | Minor | Optional change |

For the following changes, it is expected that you will be using the `uk.gov.hmrc.HttpClientImpl` implementation of `HttpClient` (which should be provided by bootstrap-play). They may not be supported on custom implementations of `HttpClient`.
For the following changes, it is expected that you will be using the `uk.gov.hmrc.PlayHttpClient` implementation of `HttpClient` (which should be provided by bootstrap-play). They may not be supported on custom implementations of `HttpClient`.

### WSProxy

Expand Down
32 changes: 6 additions & 26 deletions http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,16 @@
package uk.gov.hmrc.http

import play.api.libs.ws.{WSRequest => PlayWSRequest}
import uk.gov.hmrc.play.http.ws.WSProxyConfiguration

trait HttpClient extends HttpGet with HttpPut with HttpPost with HttpDelete with HttpPatch {

// we could remove the dependency on PlayWsRequest (which should be in the play package only)
// by using `type Request`, which HttpClientImpl can fix to PlayWsRequest,
// however it would require all implementations to fix it (breaking clients)
// the default implementations here all depend on concrete PlayWSRequest...

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

// implementations provided to not break clients (which won't be using the new functions)
// e.g. implementations of ProxyHttpClient, and the deprecated DefaultHttpClient in bootstrap.
def withUserAgent(userAgent: String): HttpClient =
withTransformRequest { req =>
replaceHeader(req, "User-Agent" -> userAgent)
}
sys.error("Not implemented by your implementation of HttpClient. You can use uk.gov.hmrc.http.PlayHttpClient")

def withProxy: HttpClient = {
val optProxyServer = WSProxyConfiguration.buildWsProxyServer(configuration)
withTransformRequest { req =>
optProxyServer.foldLeft(req)(_ withProxyServer _)
}
}
def withProxy: HttpClient =
sys.error("Not implemented by your implementation of HttpClient. You can use uk.gov.hmrc.http.PlayHttpClient")

// implementation required to not break clients (which won't be using the new functions)
// e.g. implementations of ProxyHttpClient, and the deprecated DefaultHttpClient in bootstrap.
def withTransformRequest(transform: PlayWSRequest => PlayWSRequest): HttpClient =
sys.error("Your implementation of HttpClient does not implement `withTransformRequest`. You can use uk.gov.hmrc.http.HttpClientImpl")
sys.error("Your implementation of HttpClient does not implement `withTransformRequest`. You can use uk.gov.hmrc.http.PlayHttpClient")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import uk.gov.hmrc.http.hooks.HttpHook
import uk.gov.hmrc.play.http.ws._

// class is final, since any overrides would be lost in the result of `withPlayWSRequest`
final class HttpClientImpl (
final class PlayHttpClient (
override val configuration : Config,
override val hooks : Seq[HttpHook],
override val wsClient : WSClient,
Expand All @@ -33,12 +33,31 @@ final class HttpClientImpl (
) extends HttpClient
with WSHttp {

override def withTransformRequest(transformRequest: PlayWSRequest => PlayWSRequest): HttpClientImpl =
new HttpClientImpl(
override def withTransformRequest(transformRequest: PlayWSRequest => PlayWSRequest): PlayHttpClient =
new PlayHttpClient(
this.configuration,
this.hooks,
this.wsClient,
this.actorSystem,
this.transformRequest.andThen(transformRequest)
)

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

override def withUserAgent(userAgent: String): HttpClient =
withTransformRequest { req =>
replaceHeader(req, "User-Agent" -> userAgent)
}

override def withProxy: HttpClient = {
val optProxyServer = WSProxyConfiguration.buildWsProxyServer(configuration)
withTransformRequest { req =>
optProxyServer.foldLeft(req)(_ withProxyServer _)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.json.{JsValue, Json}
import play.api.libs.ws.WSClient
import uk.gov.hmrc.http.test.WireMockSupport
import uk.gov.hmrc.play.http.HttpClientImpl
import uk.gov.hmrc.play.http.PlayHttpClient

import scala.concurrent.ExecutionContext.Implicits.global
import uk.gov.hmrc.http.HttpReads.Implicits._
Expand All @@ -51,7 +51,7 @@ class HeadersSpec
).withExtraHeaders("extra-header" -> "my-extra-header")

private lazy val httpClient =
new HttpClientImpl(
new PlayHttpClient(
configuration = app.configuration.underlying,
hooks = Seq.empty,
wsClient = app.injector.instanceOf[WSClient],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import java.util.concurrent.atomic.AtomicReference
import scala.concurrent.ExecutionContext.Implicits.global
import uk.gov.hmrc.http.HttpReads.Implicits._

class HttpClientImplSpec
class PlayHttpClientSpec
extends AnyWordSpecLike
with Matchers
with OptionValues
Expand All @@ -46,9 +46,9 @@ class HttpClientImplSpec
HeaderCarrier(extraHeaders = Seq("x-test" -> "test-val"))

WsTestClient.withClient { wsClient =>
"HttpClientImpl.withUserAgent" should {
"PlayHttpClient.withUserAgent" should {
val httpClient =
new HttpClientImpl(
new PlayHttpClient(
configuration = ConfigFactory.parseString("appName=myApp")
.withFallback(ConfigFactory.load()),
hooks = Seq.empty,
Expand Down Expand Up @@ -78,7 +78,7 @@ class HttpClientImplSpec
}
}

"HttpClientImpl.withProxy" should {
"PlayHttpClient.withProxy" should {
val proxyProtocol = "http"
val proxyHost = "proxy.com"
val proxyPort = PortFinder.findFreePort(portRange = 6001 to 7000, excluded = wireMockPort)
Expand All @@ -88,7 +88,7 @@ class HttpClientImplSpec
val proxyRef = new AtomicReference[Option[WSProxyServer]]

val httpClient =
new HttpClientImpl(
new PlayHttpClient(
configuration = ConfigFactory
.parseString(
s"""|proxy.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import akka.stream.{ActorMaterializer, Materializer}
import com.github.ghik.silencer.silent
import com.typesafe.config.{Config, ConfigFactory}
import uk.gov.hmrc.http.HttpClient
import uk.gov.hmrc.play.http.HttpClientImpl
import uk.gov.hmrc.play.http.PlayHttpClient
import play.api.libs.ws.ahc.{AhcWSClient, AhcWSClientConfigFactory}

trait HttpClientSupport {
Expand All @@ -33,7 +33,7 @@ trait HttpClientSupport {
@silent("deprecated")
implicit val mat: Materializer = ActorMaterializer() // explicitly required for play-26

new HttpClientImpl(
new PlayHttpClient(
configuration = config,
hooks = Seq.empty,
wsClient = AhcWSClient(AhcWSClientConfigFactory.forConfig(config)),
Expand Down

0 comments on commit 08e685d

Please sign in to comment.