From de4a9f93d43570204055ec451fccf0c1d3314c97 Mon Sep 17 00:00:00 2001 From: Suresh Mandalapu Date: Wed, 6 Jun 2018 10:56:24 +0100 Subject: [PATCH] more changes --- .../connectors/AuthConnector.scala | 1 - .../controllers/HelpToSaveApiController.scala | 50 +++++++------------ 2 files changed, 19 insertions(+), 32 deletions(-) diff --git a/app/uk/gov/hmrc/helptosavetestadminfrontend/connectors/AuthConnector.scala b/app/uk/gov/hmrc/helptosavetestadminfrontend/connectors/AuthConnector.scala index 13da5e2..1ebf3d2 100644 --- a/app/uk/gov/hmrc/helptosavetestadminfrontend/connectors/AuthConnector.scala +++ b/app/uk/gov/hmrc/helptosavetestadminfrontend/connectors/AuthConnector.scala @@ -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}") } diff --git a/app/uk/gov/hmrc/helptosavetestadminfrontend/controllers/HelpToSaveApiController.scala b/app/uk/gov/hmrc/helptosavetestadminfrontend/controllers/HelpToSaveApiController.scala index e37c4d6..897e1d9 100644 --- a/app/uk/gov/hmrc/helptosavetestadminfrontend/controllers/HelpToSaveApiController.scala +++ b/app/uk/gov/hmrc/helptosavetestadminfrontend/controllers/HelpToSaveApiController.scala @@ -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 @@ -38,34 +38,23 @@ 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())) @@ -73,11 +62,10 @@ class HelpToSaveApiController @Inject()(http: WSHttp, authConnector: AuthConnect 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)