From 08e685d7a5efd1440f24483f96ba5de3381bc17e Mon Sep 17 00:00:00 2001 From: colin-lamed <9568290+colin-lamed@users.noreply.github.com> Date: Mon, 27 Sep 2021 09:44:42 +0100 Subject: [PATCH] BDOG-1421 Rename HttpClientImpl to PlayHttpClient --- CHANGELOG.md | 2 +- .../scala/uk/gov/hmrc/http/HttpClient.scala | 32 ++++--------------- ...pClientImpl.scala => PlayHttpClient.scala} | 25 +++++++++++++-- .../scala/uk/gov/hmrc/http/HeadersSpec.scala | 4 +-- ...mplSpec.scala => PlayHttpClientSpec.scala} | 10 +++--- .../hmrc/http/test/HttpClientSupport.scala | 4 +-- 6 files changed, 38 insertions(+), 39 deletions(-) rename http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/{HttpClientImpl.scala => PlayHttpClient.scala} (62%) rename http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/{HttpClientImplSpec.scala => PlayHttpClientSpec.scala} (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 86caba75..2f5751c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpClient.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpClient.scala index e30ec9fa..7c6afa20 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpClient.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpClient.scala @@ -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") } diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/HttpClientImpl.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/PlayHttpClient.scala similarity index 62% rename from http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/HttpClientImpl.scala rename to http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/PlayHttpClient.scala index d90b2c4b..397e8e58 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/HttpClientImpl.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/PlayHttpClient.scala @@ -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, @@ -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 _) + } + } } diff --git a/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HeadersSpec.scala b/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HeadersSpec.scala index 6a4b4be9..428f9025 100644 --- a/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HeadersSpec.scala +++ b/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HeadersSpec.scala @@ -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._ @@ -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], diff --git a/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala b/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/PlayHttpClientSpec.scala similarity index 96% rename from http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala rename to http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/PlayHttpClientSpec.scala index 5ec98aeb..2a47a30a 100644 --- a/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala +++ b/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/PlayHttpClientSpec.scala @@ -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 @@ -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, @@ -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) @@ -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 diff --git a/http-verbs-test-common/src/main/scala/uk/gov/hmrc/http/test/HttpClientSupport.scala b/http-verbs-test-common/src/main/scala/uk/gov/hmrc/http/test/HttpClientSupport.scala index 5f9033f3..749a1003 100644 --- a/http-verbs-test-common/src/main/scala/uk/gov/hmrc/http/test/HttpClientSupport.scala +++ b/http-verbs-test-common/src/main/scala/uk/gov/hmrc/http/test/HttpClientSupport.scala @@ -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 { @@ -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)),