Skip to content

Commit

Permalink
BDOG-1421 Move HttpClientImpl into play package
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Sep 27, 2021
1 parent 1d15d0f commit cd6fb44
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
36 changes: 9 additions & 27 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 @@ -16,17 +16,19 @@

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 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(v => k -> v) }
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 : _*)
}
Expand All @@ -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)
)
}
Original file line number Diff line number Diff line change
@@ -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)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cd6fb44

Please sign in to comment.