From 7d3c9b45f056b6b3d170ad4aa4f63060cb63d754 Mon Sep 17 00:00:00 2001 From: colin-lamed <9568290+colin-lamed@users.noreply.github.com> Date: Fri, 24 Sep 2021 14:34:46 +0100 Subject: [PATCH] BDOG-1421 Move HttpClientImpl into play package --- .../scala/uk/gov/hmrc/http/HttpClient.scala | 34 ++++---------- .../gov/hmrc/play/http/HttpClientImpl.scala | 44 +++++++++++++++++++ .../scala/uk/gov/hmrc/http/HeadersSpec.scala | 1 + .../{ => play}/http/HttpClientImplSpec.scala | 3 +- .../hmrc/http/test/HttpClientSupport.scala | 3 +- 5 files changed, 57 insertions(+), 28 deletions(-) create mode 100644 http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/HttpClientImpl.scala rename http-verbs-common/src/test/scala/uk/gov/hmrc/{ => play}/http/HttpClientImplSpec.scala (98%) 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 2c1fc71e..eb5e34e6 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 @@ -16,13 +16,15 @@ package uk.gov.hmrc.http -import akka.actor.ActorSystem -import com.typesafe.config.Config -import play.api.libs.ws.{WSClient, WSRequest => PlayWSRequest} -import uk.gov.hmrc.http.hooks.HttpHook -import uk.gov.hmrc.play.http.ws._ +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 with WSRequestBuilder { +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 Self`, which HttpClientImpl can fix to PlayWsRequest, + // however it would require all implementations to fix it (breaking clients) + // the default implementations here all depend on play... private def replaceHeader(req: PlayWSRequest, header: (String, String)): PlayWSRequest = { def denormalise(hdrs: Map[String, Seq[String]]): Seq[(String, String)] = @@ -48,23 +50,3 @@ trait HttpClient extends HttpGet with HttpPut with HttpPost with HttpDelete with def withTransformRequest(transform: PlayWSRequest => PlayWSRequest): HttpClient = sys.error("Your implementation of HttpClient does not implement `withTransformRequest`. You can use uk.gov.hmrc.http.HttpClientImpl") } - -// class is final, since any overrides would be lost in the result of `withPlayWSRequest` -final class HttpClientImpl ( - override val configuration : Config, - override val hooks : Seq[HttpHook], - override val wsClient : WSClient, - override val actorSystem : ActorSystem, - override val transformRequest: PlayWSRequest => PlayWSRequest -) extends HttpClient - with WSHttp { - - override def withTransformRequest(transformRequest: PlayWSRequest => PlayWSRequest): HttpClientImpl = - new HttpClientImpl( - this.configuration, - this.hooks, - this.wsClient, - this.actorSystem, - this.transformRequest.andThen(transformRequest) - ) -} 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/HttpClientImpl.scala new file mode 100644 index 00000000..d90b2c4b --- /dev/null +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/play/http/HttpClientImpl.scala @@ -0,0 +1,44 @@ +/* + * Copyright 2021 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package uk.gov.hmrc.play.http + +import akka.actor.ActorSystem +import com.typesafe.config.Config +import play.api.libs.ws.{WSClient, WSRequest => PlayWSRequest} +import uk.gov.hmrc.http.HttpClient +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 ( + override val configuration : Config, + override val hooks : Seq[HttpHook], + override val wsClient : WSClient, + override val actorSystem : ActorSystem, + override val transformRequest: PlayWSRequest => PlayWSRequest +) extends HttpClient + with WSHttp { + + override def withTransformRequest(transformRequest: PlayWSRequest => PlayWSRequest): HttpClientImpl = + new HttpClientImpl( + this.configuration, + this.hooks, + this.wsClient, + this.actorSystem, + this.transformRequest.andThen(transformRequest) + ) +} 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 51228a76..6a4b4be9 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,6 +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 scala.concurrent.ExecutionContext.Implicits.global import uk.gov.hmrc.http.HttpReads.Implicits._ diff --git a/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HttpClientImplSpec.scala b/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala similarity index 98% rename from http-verbs-common/src/test/scala/uk/gov/hmrc/http/HttpClientImplSpec.scala rename to http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala index 47e58589..5ec98aeb 100644 --- a/http-verbs-common/src/test/scala/uk/gov/hmrc/http/HttpClientImplSpec.scala +++ b/http-verbs-common/src/test/scala/uk/gov/hmrc/play/http/HttpClientImplSpec.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package uk.gov.hmrc.http +package uk.gov.hmrc.play.http import akka.actor.ActorSystem import com.github.tomakehurst.wiremock._ @@ -26,6 +26,7 @@ import org.scalatest.matchers.should.Matchers import org.scalatest.wordspec.AnyWordSpecLike import play.api.libs.ws.WSProxyServer import play.api.test.WsTestClient +import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse} import uk.gov.hmrc.http.test.{PortFinder, WireMockSupport} import java.util.concurrent.atomic.AtomicReference 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 3968925b..5f9033f3 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 @@ -20,7 +20,8 @@ import akka.actor.ActorSystem import akka.stream.{ActorMaterializer, Materializer} import com.github.ghik.silencer.silent import com.typesafe.config.{Config, ConfigFactory} -import uk.gov.hmrc.http.{HttpClient, HttpClientImpl} +import uk.gov.hmrc.http.HttpClient +import uk.gov.hmrc.play.http.HttpClientImpl import play.api.libs.ws.ahc.{AhcWSClient, AhcWSClientConfigFactory} trait HttpClientSupport {