Skip to content

Commit 0a83092

Browse files
DLS-10979: HttpClientV2 upgrade for help-to-save-api.
1 parent d1b8223 commit 0a83092

File tree

8 files changed

+124
-153
lines changed

8 files changed

+124
-153
lines changed

app/uk/gov/hmrc/helptosaveapi/connectors/HelpToSaveConnector.scala

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ import com.google.inject.{ImplementedBy, Inject, Singleton}
2020
import play.api.Configuration
2121
import play.api.libs.json.{Json, Writes}
2222
import uk.gov.hmrc.helptosaveapi.connectors.HelpToSaveConnectorImpl.CreateAccountInfo
23-
import uk.gov.hmrc.helptosaveapi.http.HttpClient.HttpClientOps
2423
import uk.gov.hmrc.helptosaveapi.models.ValidateBankDetailsRequest
2524
import uk.gov.hmrc.helptosaveapi.models.createaccount.CreateAccountBody
2625
import uk.gov.hmrc.helptosaveapi.util.Logging
27-
import uk.gov.hmrc.http._
26+
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse, StringContextOps}
27+
import uk.gov.hmrc.http.HttpReads.Implicits._
28+
import uk.gov.hmrc.http.client.HttpClientV2
2829

2930
import java.util.UUID
3031
import scala.concurrent.{ExecutionContext, Future}
@@ -63,7 +64,7 @@ trait HelpToSaveConnector {
6364
}
6465

6566
@Singleton
66-
class HelpToSaveConnectorImpl @Inject() (config: Configuration, http: HttpClient)()
67+
class HelpToSaveConnectorImpl @Inject() (config: Configuration, http: HttpClientV2)()
6768
extends HelpToSaveConnector with Logging {
6869

6970
private val htsBaseUrl = {
@@ -73,13 +74,13 @@ class HelpToSaveConnectorImpl @Inject() (config: Configuration, http: HttpClient
7374
s"$protocol://$host:$port/help-to-save"
7475
}
7576

76-
val createAccountUrl: String = s"$htsBaseUrl/create-account"
77+
private val createAccountUrl: String = s"$htsBaseUrl/create-account"
7778

7879
private val storeEmailURL = s"$htsBaseUrl/store-email"
7980

8081
private val getEnrolmentStatusURL = s"$htsBaseUrl/enrolment-status"
8182

82-
val eligibilityCheckUrl: String = s"$htsBaseUrl/eligibility-check"
83+
private val eligibilityCheckUrl: String = s"$htsBaseUrl/eligibility-check"
8384

8485
def getAccountUrl(nino: String): String = s"$htsBaseUrl/$nino/account"
8586

@@ -88,49 +89,66 @@ class HelpToSaveConnectorImpl @Inject() (config: Configuration, http: HttpClient
8889
override def createAccount(body: CreateAccountBody, correlationId: UUID, clientCode: String, eligibilityReason: Int)(
8990
implicit hc: HeaderCarrier,
9091
ec: ExecutionContext
91-
): Future[HttpResponse] =
92-
http.post(
93-
createAccountUrl,
94-
CreateAccountInfo(body, eligibilityReason, clientCode),
95-
Map(correlationIdHeaderName -> correlationId.toString)
96-
)
92+
): Future[HttpResponse]= {
93+
val reqBody = CreateAccountInfo(body, eligibilityReason, clientCode)
94+
val headers: (String, String) = s"$correlationIdHeaderName" -> s"$correlationId"
95+
http
96+
.post(url"$createAccountUrl")
97+
.transform(_.addHttpHeaders(headers))
98+
.withBody(Json.toJson(reqBody))
99+
.execute[HttpResponse]
100+
}
97101

98102
override def checkEligibility(
99103
nino: String,
100104
correlationId: UUID
101-
)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
102-
http.get(eligibilityCheckUrl, Map("nino" -> nino), Map(correlationIdHeaderName -> correlationId.toString))
105+
)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] = {
106+
val headers: (String, String) = s"$correlationIdHeaderName" -> s"$correlationId"
107+
http.get(url"$eligibilityCheckUrl?nino=$nino").transform(_.addHttpHeaders(headers)).execute[HttpResponse]
108+
}
103109

104110
override def getAccount(nino: String, systemId: String, correlationId: UUID)(
105111
implicit hc: HeaderCarrier,
106112
ec: ExecutionContext
107-
): Future[HttpResponse] =
108-
http.get(
109-
getAccountUrl(nino),
110-
Map("systemId" -> systemId, "correlationId" -> correlationId.toString),
111-
Map(correlationIdHeaderName -> correlationId.toString)
112-
)
113+
): Future[HttpResponse] = {
114+
val url = getAccountUrl(nino)
115+
val headers: (String, String) = s"$correlationIdHeaderName" -> s"$correlationId"
116+
val result = http
117+
.get(url"$url")
118+
.transform(_.addHttpHeaders(headers))
119+
.execute[HttpResponse]
120+
result
121+
}
113122

114123
override def storeEmail(encodedEmail: String, nino: String, correlationId: UUID)(
115124
implicit hc: HeaderCarrier,
116125
ec: ExecutionContext
117-
): Future[HttpResponse] =
118-
http.get(
119-
storeEmailURL,
120-
Map("email" -> encodedEmail, "nino" -> nino),
121-
Map(correlationIdHeaderName -> correlationId.toString)
122-
)
126+
): Future[HttpResponse] = {
127+
val headers: (String, String) = s"$correlationIdHeaderName" -> s"$correlationId"
128+
http
129+
.get(url"$storeEmailURL?email=$encodedEmail&nino=$nino")
130+
.transform(_.addHttpHeaders(headers))
131+
.execute[HttpResponse]
132+
}
123133

124134
override def validateBankDetails(
125135
request: ValidateBankDetailsRequest
126136
)(implicit hc: HeaderCarrier, ex: ExecutionContext): Future[HttpResponse] =
127-
http.post(s"$htsBaseUrl/validate-bank-details", request)
137+
http
138+
.post(url"$htsBaseUrl/validate-bank-details")
139+
.withBody(Json.toJson(request))
140+
.execute[HttpResponse]
128141

129142
override def getUserEnrolmentStatus(
130143
nino: String,
131144
correlationId: UUID
132-
)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
133-
http.get(getEnrolmentStatusURL, Map("nino" -> nino), Map(correlationIdHeaderName -> correlationId.toString))
145+
)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] = {
146+
val headers: (String, String) = s"$correlationIdHeaderName" -> s"$correlationId"
147+
http
148+
.get(url"$getEnrolmentStatusURL?nino=$nino")
149+
.transform(_.addHttpHeaders(headers))
150+
.execute[HttpResponse]
151+
}
134152
}
135153

136154
object HelpToSaveConnectorImpl {

app/uk/gov/hmrc/helptosaveapi/http/HttpClient.scala

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,53 @@
1414
* limitations under the License.
1515
*/
1616

17-
package uk.gov.hmrc.helptosaveapi.http
18-
19-
import play.api.libs.json.Writes
20-
import uk.gov.hmrc.http._
21-
22-
import scala.concurrent.{ExecutionContext, Future}
23-
24-
object HttpClient {
25-
26-
// this HttpReads instance for HttpResponse is preferred over the default
27-
// uk.gov.hmrc.http.RawReads.readRaw as this custom one doesn't throw exceptions
28-
private class RawHttpReads extends HttpReads[HttpResponse] {
29-
override def read(method: String, url: String, response: HttpResponse): HttpResponse = response
30-
}
31-
32-
private val rawHttpReads = new RawHttpReads
33-
34-
implicit class HttpClientOps(val http: HttpClient) extends AnyVal {
35-
def get(
36-
url: String,
37-
queryParams: Map[String, String] = Map.empty[String, String],
38-
headers: Map[String, String] = Map.empty[String, String]
39-
)(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
40-
http.GET(url, queryParams.toSeq)(rawHttpReads, hc.withExtraHeaders(headers.toSeq: _*), ec)
41-
42-
def post[A](
43-
url: String,
44-
body: A,
45-
headers: Map[String, String] = Map.empty[String, String]
46-
)(implicit w: Writes[A], hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
47-
http.POST(url, body, headers.toSeq)(w, rawHttpReads, hc, ec)
48-
}
49-
50-
}
17+
///*
18+
// * Copyright 2024 HM Revenue & Customs
19+
// *
20+
// * Licensed under the Apache License, Version 2.0 (the "License");
21+
// * you may not use this file except in compliance with the License.
22+
// * You may obtain a copy of the License at
23+
// *
24+
// * http://www.apache.org/licenses/LICENSE-2.0
25+
// *
26+
// * Unless required by applicable law or agreed to in writing, software
27+
// * distributed under the License is distributed on an "AS IS" BASIS,
28+
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
// * See the License for the specific language governing permissions and
30+
// * limitations under the License.
31+
// */
32+
//
33+
//package uk.gov.hmrc.helptosaveapi.http
34+
//
35+
//import play.api.libs.json.Writes
36+
//import uk.gov.hmrc.http._
37+
//
38+
//import scala.concurrent.{ExecutionContext, Future}
39+
//
40+
//object HttpClient {
41+
//
42+
// // this HttpReads instance for HttpResponse is preferred over the default
43+
// // uk.gov.hmrc.http.RawReads.readRaw as this custom one doesn't throw exceptions
44+
// private class RawHttpReads extends HttpReads[HttpResponse] {
45+
// override def read(method: String, url: String, response: HttpResponse): HttpResponse = response
46+
// }
47+
//
48+
// private val rawHttpReads = new RawHttpReads
49+
//
50+
// implicit class HttpClientOps(val http: HttpClient) extends AnyVal {
51+
// def get(
52+
// url: String,
53+
// queryParams: Map[String, String] = Map.empty[String, String],
54+
// headers: Map[String, String] = Map.empty[String, String]
55+
// )(implicit hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
56+
// http.GET(url, queryParams.toSeq)(rawHttpReads, hc.withExtraHeaders(headers.toSeq: _*), ec)
57+
//
58+
// def post[A](
59+
// url: String,
60+
// body: A,
61+
// headers: Map[String, String] = Map.empty[String, String]
62+
// )(implicit w: Writes[A], hc: HeaderCarrier, ec: ExecutionContext): Future[HttpResponse] =
63+
// http.POST(url, body, headers.toSeq)(w, rawHttpReads, hc, ec)
64+
// }
65+
//
66+
//}

conf/application-json-logger.xml

Lines changed: 0 additions & 11 deletions
This file was deleted.

conf/application.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ play.filters.enabled += "play.filters.headers.SecurityHeadersFilter"
2323

2424
play.modules.enabled += "uk.gov.hmrc.play.bootstrap.AuthModule"
2525

26-
play.modules.enabled += "uk.gov.hmrc.play.bootstrap.HttpClientModule"
26+
#play.modules.enabled += "uk.gov.hmrc.play.audit.AuditModule"
27+
28+
play.modules.enabled += "uk.gov.hmrc.play.bootstrap.HttpClientV2Module"
2729

2830
play.modules.enabled += "uk.gov.hmrc.mongo.play.PlayMongoModule"
2931

project/AppDependencies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sbt._
44
object AppDependencies {
55
val hmrc = "uk.gov.hmrc"
66
val playVersion = "play-30"
7-
val hmrcBootstrapVersion = "8.4.0"
7+
val hmrcBootstrapVersion = "9.0.0"
88
val hmrcMongoVersion = "1.7.0"
99

1010
val compile: Seq[ModuleID] = Seq(

project/plugins.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ resolvers += Resolver.typesafeRepo("releases")
66

77
addSbtPlugin("uk.gov.hmrc" %% "sbt-auto-build" % "3.22.0")
88
addSbtPlugin("uk.gov.hmrc" %% "sbt-distributables" % "2.5.0")
9-
addSbtPlugin("org.playframework" %% "sbt-plugin" % "3.0.1")
9+
addSbtPlugin("org.playframework" %% "sbt-plugin" % "3.0.3")
1010
addSbtPlugin("org.scoverage" %% "sbt-scoverage" % "2.0.10")
1111
addSbtPlugin("org.scalameta" %% "sbt-scalafmt" % "2.5.2")
1212

test/uk/gov/hmrc/helptosaveapi/connectors/HelpToSaveConnectorImplSpec.scala

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class HelpToSaveConnectorImplSpec
8181
HttpResponse(400, emptyJsonBody),
8282
HttpResponse(401, emptyJsonBody),
8383
HttpResponse(403, emptyJsonBody),
84+
HttpResponse(404, emptyJsonBody),
8485
HttpResponse(500, emptyJsonBody),
8586
HttpResponse(502, emptyJsonBody),
8687
HttpResponse(503, emptyJsonBody)
@@ -92,7 +93,7 @@ class HelpToSaveConnectorImplSpec
9293
Map.empty,
9394
Map("X-Correlation-ID" -> correlationId.toString),
9495
Some(Json.toJson(CreateAccountInfo(createAccountBody, 8, "1234")).toString())
95-
) thenReturn (httpResponse.status, httpResponse.body)
96+
).thenReturn(httpResponse.status, httpResponse.body)
9697
val result = await(connector.createAccount(createAccountBody, correlationId, "1234", 8))
9798
result.status shouldBe httpResponse.status
9899
result.body shouldBe httpResponse.body
@@ -113,7 +114,7 @@ class HelpToSaveConnectorImplSpec
113114
eligibilityUrl,
114115
Map("nino" -> nino),
115116
headers
116-
) thenReturn (httpResponse.status, httpResponse.body)
117+
).thenReturn(httpResponse.status, httpResponse.body)
117118
val result = await(connector.checkEligibility(nino, correlationId))
118119
result.status shouldBe 200
119120
result.json shouldBe json
@@ -151,11 +152,10 @@ class HelpToSaveConnectorImplSpec
151152
"call the correct url and return the response as is" in {
152153
val httpResponse = HttpResponse(200, json, Map.empty[String, Seq[String]])
153154
when(
154-
GET,
155-
getAccountUrl,
156-
Map("systemId" -> systemId, "correlationId" -> correlationId.toString),
157-
headers
158-
) thenReturn (httpResponse.status, httpResponse.body)
155+
method = GET,
156+
uri = getAccountUrl,
157+
headers= headers
158+
).thenReturn(httpResponse.status, httpResponse.body)
159159
val result = await(connector.getAccount(nino, systemId, correlationId))
160160
result.status shouldBe 200
161161
result.json shouldBe json
@@ -172,8 +172,7 @@ class HelpToSaveConnectorImplSpec
172172
storeEmailUrl,
173173
Map("email" -> email, "nino" -> nino),
174174
headers
175-
) thenReturn (httpResponse.status, httpResponse.body)
176-
175+
).thenReturn(httpResponse.status, httpResponse.body)
177176
val result = await(connector.storeEmail(email, nino, correlationId))
178177
result.status shouldBe 200
179178
}
@@ -188,11 +187,25 @@ class HelpToSaveConnectorImplSpec
188187
"/help-to-save/validate-bank-details",
189188
headers = Map.empty,
190189
body = Some(Json.toJson(request).toString())
191-
) thenReturn (httpResponse.status, httpResponse.body)
190+
).thenReturn(httpResponse.status, httpResponse.body)
192191
val result = await(connector.validateBankDetails(request))
193192
result.status shouldBe httpResponse.status
194193
result.body shouldBe httpResponse.body
195194
}
196195
}
196+
197+
"handling user enrolment status" must {
198+
"return http response when it calling getUserEnrolmentStatus" in {
199+
val enrollmentStatusUrl = "/help-to-save/enrolment-status"
200+
val httpResponse = HttpResponse(200, "")
201+
when(GET,
202+
enrollmentStatusUrl,
203+
Map("nino" -> nino),
204+
headers
205+
).thenReturn(httpResponse.status, httpResponse.body)
206+
val result = await(connector.getUserEnrolmentStatus(nino,correlationId))
207+
result.status shouldBe 200
208+
}
209+
}
197210
}
198211
}

0 commit comments

Comments
 (0)