Skip to content

Commit

Permalink
ADR-1181: library updates (#158)
Browse files Browse the repository at this point in the history
* ADR-1181 library updates WIP

* ADR-1181 Fix connector spec issue

* ADR-1181 library updates connector fixes

* ADR-1181 Fix CacheConnector test

* ADR-1181 Fix tests in DirectDebitConnectorSpec

* ADR-1181 library updates connector tests fixes

* ADR-1181 test fixes

* ADR-1181 pr bot fixes

---------

Co-authored-by: yasaracar <[email protected]>
Co-authored-by: David Flynn <[email protected]>
  • Loading branch information
3 people authored Sep 20, 2024
1 parent 0eda5fb commit 18ec316
Show file tree
Hide file tree
Showing 28 changed files with 734 additions and 618 deletions.
11 changes: 7 additions & 4 deletions app/connectors/AlcoholDutyAccountConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ import config.FrontendAppConfig
import models.{HistoricPayments, OpenPayments}
import play.api.Logging
import play.api.http.Status.OK
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

class AlcoholDutyAccountConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances
with Logging {
def outstandingPayments(appaId: String)(implicit hc: HeaderCarrier): Future[OpenPayments] =
httpClient
.GET[Either[UpstreamErrorResponse, HttpResponse]](url = config.adrGetOutstandingPaymentsUrl(appaId))
.get(url"${config.adrGetOutstandingPaymentsUrl(appaId)}")
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.flatMap {
case Right(response) if response.status == OK =>
Try(response.json.as[OpenPayments]) match {
Expand All @@ -47,7 +49,8 @@ class AlcoholDutyAccountConnector @Inject() (

def historicPayments(appaId: String, year: Int)(implicit hc: HeaderCarrier): Future[HistoricPayments] =
httpClient
.GET[Either[UpstreamErrorResponse, HttpResponse]](url = config.adrGetHistoricPaymentsUrl(appaId, year))
.get(url"${config.adrGetHistoricPaymentsUrl(appaId, year)}")
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.flatMap {
case Right(response) if response.status == OK =>
Try(response.json.as[HistoricPayments]) match {
Expand Down
44 changes: 21 additions & 23 deletions app/connectors/AlcoholDutyCalculatorConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ import models.returns.AlcoholDuty
import models.{AlcoholRegime, RateBand, RatePeriod}
import play.api.http.Status.OK
import play.api.libs.json.Json
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import java.time.YearMonth
import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class AlcoholDutyCalculatorConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances {

Expand All @@ -45,16 +46,16 @@ class AlcoholDutyCalculatorConnector @Inject() (
)
.toString
)
httpClient.GET[Seq[RateBand]](url = config.adrCalculatorRatesUrl(), queryParams = queryParams)
httpClient.get(url"${config.adrCalculatorRatesUrl()}?$queryParams").execute[Seq[RateBand]]
}

def calculateTotalDuty(requestBody: TotalDutyCalculationRequest)(implicit
hc: HeaderCarrier
): Future[AlcoholDuty] =
httpClient.POST[TotalDutyCalculationRequest, AlcoholDuty](
url = config.adrCalculatorCalculateTotalDutyUrl(),
body = requestBody
)
httpClient
.post(url"${config.adrCalculatorCalculateTotalDutyUrl()}")
.withBody(Json.toJson(requestBody))
.execute[AlcoholDuty]

def rateBand(taxTypeCode: String, ratePeriod: YearMonth)(implicit
hc: HeaderCarrier
Expand All @@ -64,10 +65,8 @@ class AlcoholDutyCalculatorConnector @Inject() (
"taxTypeCode" -> taxTypeCode
)
httpClient
.GET[Either[UpstreamErrorResponse, HttpResponse]](
url = config.adrCalculatorRateBandUrl(),
queryParams = queryParams
)
.get(url"${config.adrCalculatorRateBandUrl()}?$queryParams")
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.map({
case Right(response) if response.status == OK => response.json.asOpt[RateBand]
case _ => None
Expand All @@ -79,28 +78,27 @@ class AlcoholDutyCalculatorConnector @Inject() (
val body: AdjustmentDutyCalculationRequest =
AdjustmentDutyCalculationRequest(adjustmentType, pureAlcoholVolume, rate)
httpClient
.POST[AdjustmentDutyCalculationRequest, AdjustmentDuty](
url = config.adrCalculatorCalculateAdjustmentDutyUrl(),
body = body
)
.post(url"${config.adrCalculatorCalculateAdjustmentDutyUrl()}")
.withBody(Json.toJson(body))
.execute[AdjustmentDuty]
}
def calculateRepackagedDutyChange(newDuty: BigDecimal, oldDuty: BigDecimal)(implicit
hc: HeaderCarrier
): Future[AdjustmentDuty] = {
val body: RepackagedDutyChangeRequest = RepackagedDutyChangeRequest(newDuty, oldDuty)
httpClient.POST[RepackagedDutyChangeRequest, AdjustmentDuty](
url = config.adrCalculatorCalculateRepackagedDutyChangeUrl(),
body = body
)
httpClient
.post(url"${config.adrCalculatorCalculateRepackagedDutyChangeUrl()}")
.withBody(Json.toJson(body))
.execute[AdjustmentDuty]
}

def calculateTotalAdjustment(
duties: Seq[BigDecimal]
)(implicit hc: HeaderCarrier): Future[AdjustmentDuty] = {
val body: AdjustmentTotalCalculationRequest = AdjustmentTotalCalculationRequest(duties)
httpClient.POST[AdjustmentTotalCalculationRequest, AdjustmentDuty](
url = config.adrCalculatorCalculateTotalAdjustmentUrl(),
body = body
)
httpClient
.post(url"${config.adrCalculatorCalculateTotalAdjustmentUrl()}")
.withBody(Json.toJson(body))
.execute[AdjustmentDuty]
}
}
19 changes: 11 additions & 8 deletions app/connectors/AlcoholDutyReturnsConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,26 @@ import config.FrontendAppConfig
import models.ObligationData
import models.returns.{AdrReturnCreatedDetails, AdrReturnSubmission, ReturnDetails}
import play.api.Logging
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
import play.api.http.Status.{CREATED, OK}
import play.api.libs.json.Json
import uk.gov.hmrc.http.client.HttpClientV2

class AlcoholDutyReturnsConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances
with Logging {

def obligationDetails(appaId: String)(implicit hc: HeaderCarrier): Future[Seq[ObligationData]] =
httpClient
.GET[Either[UpstreamErrorResponse, HttpResponse]](url = config.adrGetObligationDetailsUrl(appaId))
.get(url"${config.adrGetObligationDetailsUrl(appaId)}")
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.flatMap {
case Right(response) if response.status == OK =>
Try(response.json.as[Seq[ObligationData]]) match {
Expand All @@ -50,7 +53,8 @@ class AlcoholDutyReturnsConnector @Inject() (

def getReturn(appaId: String, periodKey: String)(implicit hc: HeaderCarrier): Future[ReturnDetails] =
httpClient
.GET[Either[UpstreamErrorResponse, HttpResponse]](url = config.adrGetReturnsUrl(appaId, periodKey))
.get(url"${config.adrGetReturnsUrl(appaId, periodKey)}")
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.flatMap {
case Right(response) if response.status == OK =>
Try(response.json.as[ReturnDetails]) match {
Expand All @@ -66,10 +70,9 @@ class AlcoholDutyReturnsConnector @Inject() (
): EitherT[Future, String, AdrReturnCreatedDetails] =
EitherT {
httpClient
.POST[AdrReturnSubmission, Either[UpstreamErrorResponse, HttpResponse]](
url = config.adrSubmitReturnUrl(appaId, periodKey),
returnSubmission
)
.post(url"${config.adrSubmitReturnUrl(appaId, periodKey)}")
.withBody(Json.toJson(returnSubmission))
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.map {
case Right(response) if response.status == CREATED =>
Try(response.json.as[AdrReturnCreatedDetails]) match {
Expand Down
49 changes: 22 additions & 27 deletions app/connectors/CacheConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,49 @@ package connectors

import config.FrontendAppConfig
import models.{ReturnId, UserAnswers}
import play.api.libs.json.{Json, Writes}
import play.api.libs.json.Json
import uk.gov.hmrc.alcoholdutyreturns.models.ReturnAndUserDetails
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReads, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class CacheConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances {

def get(appaId: String, periodKey: String)(implicit
hc: HeaderCarrier
): Future[Either[UpstreamErrorResponse, UserAnswers]] =
httpClient.GET[Either[UpstreamErrorResponse, UserAnswers]](config.adrCacheGetUrl(appaId, periodKey))
httpClient.get(url"${config.adrCacheGetUrl(appaId, periodKey)}").execute[Either[UpstreamErrorResponse, UserAnswers]]

def set(userAnswers: UserAnswers)(implicit hc: HeaderCarrier): Future[HttpResponse] =
httpClient.PUT(config.adrCacheSetUrl(), userAnswers)(
implicitly[Writes[UserAnswers]],
implicitly[HttpReads[HttpResponse]],
hc.withExtraHeaders("Csrf-Token" -> "nocheck"),
implicitly
)
httpClient
.put(url"${config.adrCacheSetUrl()}")
.setHeader("Csrf-Token" -> "nocheck")
.withBody(Json.toJson(userAnswers))
.execute[HttpResponse]

def createUserAnswers(returnAndUserDetails: ReturnAndUserDetails)(implicit hc: HeaderCarrier): Future[HttpResponse] =
httpClient.POST(config.adrCacheCreateUserAnswersUrl(), returnAndUserDetails)(
implicitly[Writes[ReturnAndUserDetails]],
implicitly[HttpReads[HttpResponse]],
hc.withExtraHeaders("Csrf-Token" -> "nocheck"),
implicitly
)
httpClient
.post(url"${config.adrCacheCreateUserAnswersUrl()}")
.withBody(Json.toJson(returnAndUserDetails))
.setHeader("Csrf-Token" -> "nocheck")
.execute[HttpResponse]

def releaseLock(returnId: ReturnId)(implicit hc: HeaderCarrier): Future[Unit] =
httpClient
.DELETE(config.adrReleaseCacheLockUrl(returnId.appaId, returnId.periodKey))(
implicitly[HttpReads[HttpResponse]],
hc.withExtraHeaders("Csrf-Token" -> "nocheck"),
implicitly
)
.delete(url"${config.adrReleaseCacheLockUrl(returnId.appaId, returnId.periodKey)}")
.setHeader("Csrf-Token" -> "nocheck")
.execute[HttpResponse]
.map(_ => ())

def keepAlive(returnId: ReturnId)(implicit hc: HeaderCarrier): Future[HttpResponse] =
httpClient.PUT(config.adrCacheKeepAliveUrl(returnId.appaId, returnId.periodKey), Json.obj())(
implicitly,
implicitly[HttpReads[HttpResponse]],
hc.withExtraHeaders("Csrf-Token" -> "nocheck"),
implicitly
)
httpClient
.put(url"${config.adrCacheKeepAliveUrl(returnId.appaId, returnId.periodKey)}")
.setHeader("Csrf-Token" -> "nocheck")
.execute[HttpResponse]
}
13 changes: 7 additions & 6 deletions app/connectors/DirectDebitConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import config.FrontendAppConfig
import models.payments.{StartDirectDebitRequest, StartDirectDebitResponse}
import play.api.Logging
import play.api.http.Status.CREATED
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import play.api.libs.json.Json
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

class DirectDebitConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances
with Logging {
Expand All @@ -39,10 +41,9 @@ class DirectDebitConnector @Inject() (
)(implicit hc: HeaderCarrier): EitherT[Future, String, StartDirectDebitResponse] =
EitherT {
httpClient
.POST[StartDirectDebitRequest, Either[UpstreamErrorResponse, HttpResponse]](
url = config.startDirectDebitUrl,
startDirectDebitRequest
)
.post(url"${config.startDirectDebitUrl}")
.withBody(Json.toJson(startDirectDebitRequest))
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.map {
case Right(response) if response.status == CREATED =>
Try(response.json.as[StartDirectDebitResponse]) match {
Expand Down
13 changes: 7 additions & 6 deletions app/connectors/PayApiConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ import config.FrontendAppConfig
import models.payments.{StartPaymentRequest, StartPaymentResponse}
import play.api.Logging
import play.api.http.Status.CREATED
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpReadsInstances, HttpResponse, UpstreamErrorResponse}
import play.api.libs.json.Json
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpReadsInstances, HttpResponse, StringContextOps, UpstreamErrorResponse}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}

class PayApiConnector @Inject() (
config: FrontendAppConfig,
implicit val httpClient: HttpClient
implicit val httpClient: HttpClientV2
)(implicit ec: ExecutionContext)
extends HttpReadsInstances
with Logging {
Expand All @@ -39,10 +41,9 @@ class PayApiConnector @Inject() (
)(implicit hc: HeaderCarrier): EitherT[Future, String, StartPaymentResponse] =
EitherT {
httpClient
.POST[StartPaymentRequest, Either[UpstreamErrorResponse, HttpResponse]](
url = config.startPaymentUrl,
startPaymentRequest
)
.post(url"${config.startPaymentUrl}")
.withBody(Json.toJson(startPaymentRequest))
.execute[Either[UpstreamErrorResponse, HttpResponse]]
.map {
case Right(response) if response.status == CREATED =>
Try(response.json.as[StartPaymentResponse]) match {
Expand Down
12 changes: 7 additions & 5 deletions app/handlers/ErrorHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,25 @@ package handlers

import play.api.Logging
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.mvc.Request
import play.api.mvc.RequestHeader
import play.twirl.api.Html
import uk.gov.hmrc.play.bootstrap.frontend.http.FrontendErrorHandler
import views.html.ErrorTemplate

import javax.inject.{Inject, Singleton}
import scala.concurrent.{ExecutionContext, Future}

@Singleton
class ErrorHandler @Inject() (
val messagesApi: MessagesApi,
view: ErrorTemplate
) extends FrontendErrorHandler
)(implicit val ec: ExecutionContext)
extends FrontendErrorHandler
with Logging
with I18nSupport {

override def standardErrorTemplate(pageTitle: String, heading: String, message: String)(implicit
rh: Request[_]
): Html =
view(pageTitle, heading, message)
request: RequestHeader
): Future[Html] =
Future.successful(view(pageTitle, heading, message))
}
11 changes: 5 additions & 6 deletions app/testonly/connectors/TestOnlyCacheConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,18 @@
package testonly.connectors

import config.FrontendAppConfig
import uk.gov.hmrc.http.HttpReads.Implicits.readRaw
import uk.gov.hmrc.http.{HeaderCarrier, HttpClient, HttpResponse}
import uk.gov.hmrc.http.HttpReads.Implicits._
import uk.gov.hmrc.http.client.HttpClientV2
import uk.gov.hmrc.http.{HeaderCarrier, HttpResponse, StringContextOps}

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

class TestOnlyCacheConnector @Inject() (
appConfig: FrontendAppConfig,
httpClient: HttpClient
httpClient: HttpClientV2
)(implicit val ec: ExecutionContext) {

def clearAllData()(implicit hc: HeaderCarrier): Future[HttpResponse] =
httpClient.DELETE(
appConfig.adrCacheClearAllUrl()
)
httpClient.delete(url"${appConfig.adrCacheClearAllUrl()}").execute[HttpResponse]
}
2 changes: 1 addition & 1 deletion app/views/ErrorTemplate.scala.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
layout: templates.Layout
)

@(pageTitle: String, heading: String, message: String)(implicit request: Request[_], messages: Messages)
@(pageTitle: String, heading: String, message: String)(implicit request: RequestHeader, messages: Messages)

@layout(pageTitle = titleNoForm(pageTitle)) {
<h1 class="govuk-heading-xl">@messages(heading)</h1>
Expand Down
Loading

0 comments on commit 18ec316

Please sign in to comment.