From 474453520333eef66f6fc119d37cdbc6be8e3fe7 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 4 May 2019 11:36:40 -0600 Subject: [PATCH] server: Update playsonify to 2.0.1 This allows to reduce the custom code for setting cache headers --- server/app/controllers/BlocksController.scala | 45 ++++++------------- server/build.sbt | 2 +- .../controllers/BlocksControllerSpec.scala | 18 ++++++++ 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/server/app/controllers/BlocksController.scala b/server/app/controllers/BlocksController.scala index 43bb0bd..f2a80d1 100644 --- a/server/app/controllers/BlocksController.scala +++ b/server/app/controllers/BlocksController.scala @@ -1,18 +1,16 @@ package controllers +import com.alexitc.playsonify.core.FutureOr.Implicits.FutureOps import com.alexitc.playsonify.models.ordering.OrderingQuery import com.alexitc.playsonify.models.pagination.{Limit, Offset, PaginatedQuery} -import com.alexitc.playsonify.models.{ErrorId, WrappedExceptionError} import com.xsn.explorer.models.LightWalletTransaction import com.xsn.explorer.models.values.Height import com.xsn.explorer.services.{BlockService, TransactionService} import controllers.common.{Codecs, MyJsonController, MyJsonControllerComponents} import javax.inject.Inject -import org.scalactic.{Bad, Every, Good} import play.api.libs.json.{Json, Writes} import scala.util.Try -import scala.util.control.NonFatal class BlocksController @Inject() ( blockService: BlockService, @@ -27,36 +25,19 @@ class BlocksController @Inject() ( blockService.getLatestBlocks() } - def getBlockHeaders(lastSeenHash: Option[String], limit: Int, orderingCondition: String) = Action.async(EmptyJsonParser) { request => - implicit val lang = messagesApi.preferred(request).lang - val result = blockService.getBlockHeaders(Limit(limit), lastSeenHash, orderingCondition) - result.map { - case Good((value, cacheable)) => - val response = renderSuccessfulResult(Ok, value) - if (cacheable) { - response.withHeaders("Cache-Control" -> "public, max-age=31536000") - } else { - response.withHeaders("Cache-Control" -> "no-store") + def getBlockHeaders(lastSeenHash: Option[String], limit: Int, orderingCondition: String) = public { _ => + blockService + .getBlockHeaders(Limit(limit), lastSeenHash, orderingCondition) + .toFutureOr + .map { case (value, cacheable) => + val response = Ok(Json.toJson(value)) + if (cacheable) { + response.withHeaders("Cache-Control" -> "public, max-age=31536000") + } else { + response.withHeaders("Cache-Control" -> "no-store") + } } - - case Bad(errors) => - val errorId = ErrorId.create - val status = getResultStatus(errors) - val json = renderErrors(errors) - - logServerErrors(errorId, errors) - status(json) - }.recover { - case NonFatal(ex) => - val errorId = ErrorId.create - val error = WrappedExceptionError(errorId, ex) - val errors = Every(error) - val json = renderErrors(errors) - val status = getResultStatus(errors) - - logServerErrors(errorId, errors) - status(json) - } + .toFuture } /** diff --git a/server/build.sbt b/server/build.sbt index 35668bc..c3e4502 100644 --- a/server/build.sbt +++ b/server/build.sbt @@ -22,7 +22,7 @@ scalacOptions ++= Seq( ) -val playsonifyVersion = "2.0.1-SNAPSHOT" +val playsonifyVersion = "2.0.1" lazy val root = (project in file(".")) .enablePlugins(PlayScala) diff --git a/server/test/controllers/BlocksControllerSpec.scala b/server/test/controllers/BlocksControllerSpec.scala index 8b4be66..f5b4f3a 100644 --- a/server/test/controllers/BlocksControllerSpec.scala +++ b/server/test/controllers/BlocksControllerSpec.scala @@ -356,6 +356,24 @@ class BlocksControllerSpec extends MyAPISpec { } } + "GET /blocks/headers" should { + "return the headers" in { + pending + } + + "cache when all results from a query are delivered" in { + pending + } + + "not cache when all results from a query are delivered but one of the latest 20 blocks are included" in { + pending + } + + "not cache when retrieving headers in descending order" in { + pending + } + } + private def matchBlock(expected: Block, actual: JsValue) = { val jsonBlock = actual val block = expected