Skip to content

Commit

Permalink
Merge pull request #28 from jose-puente-digital-hmrc-gov-uk/full-api-…
Browse files Browse the repository at this point in the history
…url-in-links

Full API urls in links
  • Loading branch information
thomas-vogel444 authored Dec 7, 2017
2 parents 7e62f0b + 0a054dc commit 5a0d79d
Show file tree
Hide file tree
Showing 22 changed files with 127 additions and 105 deletions.
7 changes: 4 additions & 3 deletions app/uk/gov/hmrc/epayeapi/config/AppContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package uk.gov.hmrc.epayeapi.config

import javax.inject.{Inject, Singleton}

import play.api.{Configuration, Environment, Logger}
import play.api.{Configuration, Logger}
import uk.gov.hmrc.play.config.inject.DefaultServicesConfig

import scala.util.Try
Expand All @@ -31,8 +31,9 @@ case class AppContext @Inject() (config: DefaultServicesConfig) {
val appName: String = current.getString("appName").getOrElse(throw new RuntimeException("appName is not configured"))
val appUrl: String = current.getString("appUrl").getOrElse(throw new RuntimeException("appUrl is not configured"))
val serviceLocatorUrl: String = config.baseUrl("service-locator")
val apiContext: String = current.getString(s"api.context").getOrElse(throw new RuntimeException(s"Missing Key $env.api.context"))
val apiStatus: String = current.getString("api.status").getOrElse(throw new RuntimeException(s"Missing Key $env.api.status"))
val apiContext: String = current.getString("api.context").getOrElse(throw new RuntimeException(s"Missing Key 'api.context' in environment $env."))
val apiStatus: String = current.getString("api.status").getOrElse(throw new RuntimeException(s"Missing Key 'api.status' in environment $env."))
val apiBaseUrl : String = current.getString("api.baseUrl").getOrElse(throw new RuntimeException(s"Missing Key 'api.baseUrl' in environment $env."))
val useSandboxConnectors: Boolean =
Try(current.getString("useSandboxConnectors").getOrElse("false").toBoolean)
.getOrElse(false)
Expand Down
11 changes: 7 additions & 4 deletions app/uk/gov/hmrc/epayeapi/connectors/EpayeConnector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import uk.gov.hmrc.play.http.ws.WSHttp

import scala.concurrent.{ExecutionContext, Future}

case class EpayeApiConfig(baseUrl: String)
case class EpayeApiConfig(
epayeBaseUrl: String,
apiBaseUrl: String
)

@Singleton
case class EpayeConnector @Inject() (
Expand All @@ -38,7 +41,7 @@ case class EpayeConnector @Inject() (

def getTotal(empRef: EmpRef, headers: HeaderCarrier): Future[EpayeResponse[EpayeTotalsResponse]] = {
val url =
s"${config.baseUrl}" +
s"${config.epayeBaseUrl}" +
s"/epaye" +
s"/${empRef.encodedValue}" +
s"/api/v1/annual-statement"
Expand All @@ -48,7 +51,7 @@ case class EpayeConnector @Inject() (

def getAnnualStatement(empRef: EmpRef, taxYear: TaxYear, headers: HeaderCarrier): Future[EpayeResponse[EpayeAnnualStatement]] = {
val url =
s"${config.baseUrl}" +
s"${config.epayeBaseUrl}" +
s"/epaye" +
s"/${empRef.encodedValue}" +
s"/api/v1/annual-statement/${taxYear.asString}"
Expand All @@ -58,7 +61,7 @@ case class EpayeConnector @Inject() (

def getMonthlyStatement(empRef: EmpRef, headers: HeaderCarrier, taxYear: TaxYear, taxMonth: TaxMonth): Future[EpayeResponse[EpayeMonthlyStatement]] = {
val url =
s"${config.baseUrl}" +
s"${config.epayeBaseUrl}" +
s"/epaye/${empRef.encodedValue}" +
s"/api/v1" +
s"/monthly-statement/${taxYear.asString}/${taxMonth.asString}"
Expand Down
11 changes: 6 additions & 5 deletions app/uk/gov/hmrc/epayeapi/controllers/GetEmpRefsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import javax.inject.{Inject, Singleton}

import akka.stream.Materializer
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, EssentialAction}
import play.api.mvc.{Action, EssentialAction}
import uk.gov.hmrc.auth.core.AuthConnector
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.connectors.EpayeApiConfig
import uk.gov.hmrc.epayeapi.models.Formats._
import uk.gov.hmrc.epayeapi.models.out.EmpRefsJson

import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.ExecutionContext

@Singleton
case class GetEmpRefsController @Inject()(
case class GetEmpRefsController @Inject() (
config: EpayeApiConfig,
authConnector: AuthConnector,
implicit val ec: ExecutionContext,
implicit val mat: Materializer
Expand All @@ -38,7 +39,7 @@ case class GetEmpRefsController @Inject()(

def getEmpRefs(): EssentialAction = EmpRefsAction { empRefs =>
Action { request =>
Ok(Json.toJson(EmpRefsJson.fromSeq(empRefs.toSeq)))
Ok(Json.toJson(EmpRefsJson.fromSeq(config.apiBaseUrl, empRefs.toSeq)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import play.api.libs.json.Json
import play.api.mvc.{Action, EssentialAction}
import uk.gov.hmrc.auth.core.AuthConnector
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.connectors.EpayeConnector
import uk.gov.hmrc.epayeapi.connectors.{EpayeApiConfig, EpayeConnector}
import uk.gov.hmrc.epayeapi.models.Formats._
import uk.gov.hmrc.epayeapi.models.in.{EpayeJsonError, EpayeNotFound, EpayeResponse, EpayeSuccess}
import uk.gov.hmrc.epayeapi.models.out.ApiErrorJson.EmpRefNotFound
Expand All @@ -35,6 +35,7 @@ import scala.concurrent.ExecutionContext

@Singleton
case class GetMonthlyStatementController @Inject() (
config: EpayeApiConfig,
authConnector: AuthConnector,
epayeConnector: EpayeConnector,
implicit val ec: ExecutionContext,
Expand All @@ -54,6 +55,7 @@ case class GetMonthlyStatementController @Inject() (
case EpayeSuccess(json) =>
Ok(Json.toJson(
MonthlyStatementJson(
config.apiBaseUrl,
empRef,
taxYear,
taxMonth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import play.api.libs.json.Json
import play.api.mvc.{Action, EssentialAction}
import uk.gov.hmrc.auth.core.AuthConnector
import uk.gov.hmrc.domain.EmpRef
import uk.gov.hmrc.epayeapi.connectors.EpayeConnector
import uk.gov.hmrc.epayeapi.connectors.{EpayeApiConfig, EpayeConnector}
import uk.gov.hmrc.epayeapi.models.Formats._
import uk.gov.hmrc.epayeapi.models.in.{EpayeJsonError, EpayeNotFound, EpayeResponse, EpayeSuccess}
import uk.gov.hmrc.epayeapi.models.out.ApiErrorJson.EmpRefNotFound
Expand All @@ -34,6 +34,7 @@ import scala.concurrent.ExecutionContext

@Singleton
case class GetSummaryController @Inject() (
config: EpayeApiConfig,
authConnector: AuthConnector,
epayeConnector: EpayeConnector,
implicit val ec: ExecutionContext,
Expand All @@ -46,7 +47,7 @@ case class GetSummaryController @Inject() (
Action.async { request =>
epayeConnector.getTotal(empRef, hc(request)).map {
case EpayeSuccess(totals) =>
Ok(Json.toJson(SummaryJson(empRef, totals)))
Ok(Json.toJson(SummaryJson(config.apiBaseUrl, empRef, totals)))
case EpayeJsonError(err) =>
Logger.error(s"Upstream returned invalid json: $err")
InternalServerError(Json.toJson(ApiErrorJson.InternalServerError))
Expand Down
27 changes: 14 additions & 13 deletions app/uk/gov/hmrc/epayeapi/models/out/EmpRefsJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,30 @@ case class EmpRefsJson(
)

object EmpRefsJson {
def fromSeq(seq: Seq[EmpRef]): EmpRefsJson =
EmpRefsJson(seq.map(EmpRefItem(_)), EmpRefsLinks())
def apply(empRef: EmpRef): EmpRefsJson =
EmpRefsJson(Seq(EmpRefItem(empRef)), EmpRefsLinks())
def fromSeq(apiBaseUrl: String, seq: Seq[EmpRef]): EmpRefsJson =
EmpRefsJson(seq.map(EmpRefItem(apiBaseUrl, _)), EmpRefsLinks(apiBaseUrl))
def apply(apiBaseUrl: String, empRef: EmpRef): EmpRefsJson =
EmpRefsJson(Seq(EmpRefItem(apiBaseUrl, empRef)), EmpRefsLinks(apiBaseUrl))
}

case class EmpRefItem(empRef: EmpRef, _links: EmpRefLinks)

object EmpRefItem {
def apply(empRef: EmpRef): EmpRefItem =
EmpRefItem(empRef, EmpRefLinks(empRef))
def apply(apiBaseUrl: String, empRef: EmpRef): EmpRefItem =
EmpRefItem(empRef, EmpRefLinks(apiBaseUrl, empRef))
}

case class EmpRefsLinks(self: Link = Link.empRefsLink())
case class EmpRefsLinks(self: Link)

object EmpRefsLinks {
def apply(apiBaseUrl: String): EmpRefsLinks =
new EmpRefsLinks(self = Link.empRefsLink(apiBaseUrl))
}

case class EmpRefLinks(summary: Link)

object EmpRefLinks {
def apply(empRef: EmpRef): EmpRefLinks =
EmpRefLinks(summary = Link.summaryLink(empRef))
def apply(apiBaseUrl: String, empRef: EmpRef): EmpRefLinks =
EmpRefLinks(summary = Link.summaryLink(apiBaseUrl, empRef))
}





20 changes: 10 additions & 10 deletions app/uk/gov/hmrc/epayeapi/models/out/Link.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ case class Link(href: String)
object Link {
val prefix = "/organisations/paye"

def empRefsLink(): Link =
Link(s"$prefix/")
def empRefsLink(apiBaseUrl: String): Link =
Link(s"$apiBaseUrl$prefix/")

def summaryLink(empRef: EmpRef): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}")
def summaryLink(apiBaseUrl: String, empRef: EmpRef): Link =
Link(s"$apiBaseUrl$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}")

def statementsLink(empRef: EmpRef): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements")
def statementsLink(apiBaseUrl: String, empRef: EmpRef): Link =
Link(s"$apiBaseUrl$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements")

def anualStatementLink(empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}")
def anualStatementLink(apiBaseUrl: String, empRef: EmpRef, taxYear: TaxYear): Link =
Link(s"$apiBaseUrl$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}")

def monthlyStatementLink(empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth): Link =
Link(s"$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}/${taxMonth.asString}")
def monthlyStatementLink(apiBaseUrl: String, empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth): Link =
Link(s"$apiBaseUrl$prefix/${empRef.taxOfficeNumber}/${empRef.taxOfficeReference}/statements/${taxYear.asString}/${taxMonth.asString}")
}
18 changes: 9 additions & 9 deletions app/uk/gov/hmrc/epayeapi/models/out/MonthlyStatementJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ case class MonthlyStatementLinksJson(
)

object MonthlyStatementJson {
def apply(empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth, json: EpayeMonthlyStatement): MonthlyStatementJson =
def apply(apiBaseUrl: String, empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth, json: EpayeMonthlyStatement): MonthlyStatementJson =
MonthlyStatementJson(
taxOfficeNumber = empRef.taxOfficeNumber,
taxOfficeReference = empRef.taxOfficeReference,
Expand All @@ -75,7 +75,7 @@ object MonthlyStatementJson {
allocatedPayments = Payments(json.payments),
dueDate = json.balance.dueDate,
summary = MonthlySummaryJson(json),
_links = MonthlyStatementLinksJson(empRef, taxYear, taxMonth)
_links = MonthlyStatementLinksJson(apiBaseUrl, empRef, taxYear, taxMonth)
)
}

Expand Down Expand Up @@ -111,19 +111,19 @@ object MonthlySummaryJson {
}

object MonthlyStatementLinksJson {
def apply(empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth): MonthlyStatementLinksJson =
def apply(apiBaseUrl: String, empRef: EmpRef, taxYear: TaxYear, taxMonth: TaxMonth): MonthlyStatementLinksJson =
MonthlyStatementLinksJson(
empRefs =
Link.empRefsLink(),
Link.empRefsLink(apiBaseUrl),
statements =
Link.summaryLink(empRef),
Link.summaryLink(apiBaseUrl, empRef),
annualStatement =
Link.anualStatementLink(empRef, taxYear),
Link.anualStatementLink(apiBaseUrl, empRef, taxYear),
self =
Link.monthlyStatementLink(empRef, taxYear, taxMonth),
Link.monthlyStatementLink(apiBaseUrl, empRef, taxYear, taxMonth),
next =
Link.monthlyStatementLink(empRef, if (taxMonth.isLast) taxYear.next else taxYear, taxMonth.next),
Link.monthlyStatementLink(apiBaseUrl, empRef, if (taxMonth.isLast) taxYear.next else taxYear, taxMonth.next),
previous =
Link.monthlyStatementLink(empRef, if (taxMonth.isFirst) taxYear.previous else taxYear, taxMonth.previous)
Link.monthlyStatementLink(apiBaseUrl, empRef, if (taxMonth.isFirst) taxYear.previous else taxYear, taxMonth.previous)
)
}
10 changes: 5 additions & 5 deletions app/uk/gov/hmrc/epayeapi/models/out/SummaryJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ case class SummaryJson(
)

object SummaryJson {
def apply(empRef: EmpRef, total: EpayeTotalsResponse): SummaryJson =
def apply(apiBaseUrl: String, empRef: EmpRef, total: EpayeTotalsResponse): SummaryJson =
SummaryJson(
OutstandingCharges(
total.overall,
Expand All @@ -44,7 +44,7 @@ object SummaryJson {
nonRti = total.nonRti.totals.balance.debit
)
),
SummaryLinks(empRef)
SummaryLinks(apiBaseUrl, empRef)
)
}

Expand All @@ -54,9 +54,9 @@ case class SummaryLinks(
)

object SummaryLinks {
def apply(empRef: EmpRef): SummaryLinks = SummaryLinks(
Link.empRefsLink(),
Link.summaryLink(empRef)
def apply(apiBaseUrl: String, empRef: EmpRef): SummaryLinks = SummaryLinks(
Link.empRefsLink(apiBaseUrl),
Link.summaryLink(apiBaseUrl, empRef)
)
}

5 changes: 4 additions & 1 deletion app/uk/gov/hmrc/epayeapi/modules/AppModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ class AppModule() extends AbstractModule {
@Provides
@Singleton
def provideEpayeApiConfig(context: AppContext): EpayeApiConfig = {
EpayeApiConfig(context.config.baseUrl("epaye"))
EpayeApiConfig(
context.config.baseUrl("epaye"),
context.apiBaseUrl
)
}

@Provides
Expand Down
1 change: 1 addition & 0 deletions conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Prod {
}

api {
baseUrl = "https://api.service.hmrc.gov.uk"
context = "organisations/paye"
status = "PROTOTYPED"
}
12 changes: 6 additions & 6 deletions resources/public/api/conf/1.0/examples/AnnualStatement.get.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@
"isSpecified": false,
"_links": {
"self": {
"href": "/organisations/paye/840/GZ00064/statements/2016-17/4"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2016-17/4"
}
}
}
]
},
"_links": {
"empRefs": {
"href": "/organisations/paye/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/"
},
"statements": {
"href": "/organisations/paye/840/GZ00064/statements"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements"
},
"self": {
"href": "/organisations/paye/840/GZ00064/statements/2016-17"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2016-17"
},
"next": {
"href": "/organisations/paye/840/GZ00064/statements/2017-18"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2017-18"
},
"previous": {
"href": "/organisations/paye/840/GZ00064/statements/2015-16"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/840/GZ00064/statements/2015-16"
}
}
}
14 changes: 7 additions & 7 deletions resources/public/api/conf/1.0/examples/Epaye.get.array.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"taxOfficeReference": "AB00001",
"_links": {
"self": {
"href": "/organisations/paye/001/AB00001/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001"
},
"statements": {
"href": "/organisations/paye/001/AB00001/statements/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements"
},
"currentStatement": {
"href": "/organisations/paye/001/AB00001/statements/2017-18"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/AB00001/statements/2017-18"
}
}
},
Expand All @@ -21,21 +21,21 @@
"taxOfficeReference": "CD00002",
"_links": {
"self": {
"href": "/organisations/paye/001/CD00002/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/CD00002"
},
"statements": {
"href": "/organisations/paye/001/CD00002/statements/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/CD00002/statements"
},
"currentStatement": {
"href": "/organisations/paye/001/CD00002/statements/2017-18"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/001/CD00002/statements/2017-18"
}
}
}
]
},
"_links": {
"self": {
"href": "/organisations/paye/"
"href": "https://api.service.hmrc.gov.uk/organisations/paye/"
}
}
}
Loading

0 comments on commit 5a0d79d

Please sign in to comment.