Skip to content

Commit

Permalink
more changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshhmrc committed Jun 6, 2018
1 parent 36770ba commit de4a9f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class AuthConnector @Inject()(http: WSHttp, appConfig: AppConfig) extends Loggin
response
response.status match {
case Status.OK =>
logger.info(s"Got 200 from auth stub, response headers= ${response.allHeaders} and body=${response.body}")
Right(response.body)
case other: Int => Left(s"unexpected status during auth, got status=$other but 200 expected, response body=${response.body}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.google.common.cache.{CacheBuilder, CacheLoader, LoadingCache}
import com.google.inject.Inject
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs.json.Json
import play.api.mvc.{Action, AnyContent, Request}
import play.api.mvc.{Action, AnyContent}
import uk.gov.hmrc.helptosavetestadminfrontend.config.AppConfig
import uk.gov.hmrc.helptosavetestadminfrontend.connectors.AuthConnector
import uk.gov.hmrc.helptosavetestadminfrontend.forms.NinoForm
Expand All @@ -38,46 +38,34 @@ import scala.util.{Failure, Success, Try}
class HelpToSaveApiController @Inject()(http: WSHttp, authConnector: AuthConnector)(implicit override val appConfig: AppConfig, val messageApi: MessagesApi)
extends AdminFrontendController(messageApi, appConfig) with I18nSupport with Logging {

var tokenCache: LoadingCache[String, String] = _

def initializeCache(implicit hc: HeaderCarrier, request: Request[_]): LoadingCache[String, String] = {
if (tokenCache == null) {
logger.info("tokenCache is null, initializing it first time")
tokenCache =
CacheBuilder
.newBuilder
.maximumSize(1)
.expireAfterWrite(3, TimeUnit.HOURS)
.build(new CacheLoader[String, String] {
override def load(key: String): String = {
val result = Await.result(authConnector.loginAndGetToken(), Duration(1, TimeUnit.MINUTES))
result match {
case Right(token) =>
logger.info(s"Loaded access token from oauth, token=$token")
token
case Left(e) => throw new Exception(s"error during retrieving token from oauth, error=$e")
}
}
})

tokenCache
} else {
logger.info("tokenCache is not null, means it was initialized already")
tokenCache
}
}
var tokenCache: LoadingCache[String, String] =
CacheBuilder
.newBuilder
.maximumSize(1)
.expireAfterWrite(3, TimeUnit.HOURS)
.build(new CacheLoader[String, String] {
override def load(key: String): String = {
implicit val hc: HeaderCarrier = HeaderCarrier()
val result = Await.result(authConnector.loginAndGetToken(), Duration(1, TimeUnit.MINUTES))
result match {
case Right(token) =>
logger.info(s"Loaded access token from oauth, token=$token")
token
case Left(e) => throw new Exception(s"error during retrieving token from oauth, error=$e")
}
}
})

def availableEndpoints(): Action[AnyContent] = Action.async { implicit request =>
Future.successful(Ok(views.html.availableEndpoints()))
}

def getCheckEligibilityPage(): Action[AnyContent] = Action.async { implicit request =>
Try {
initializeCache
tokenCache.get("token")
} match {
case Success(token) =>
logger.info(s"token exists in cache, token: $token")
logger.info(s"loaded token from cache, token: $token")
Future.successful(Ok(views.html.get_check_eligibility_page(NinoForm.ninoForm)))
case Failure(e) =>
logger.warn(e.getMessage)
Expand Down

0 comments on commit de4a9f9

Please sign in to comment.