Browse Source

server: Update playsonify to 2.0.1

This allows to reduce the custom code for setting cache headers
develop
Alexis Hernandez 6 years ago
parent
commit
4744535203
  1. 45
      server/app/controllers/BlocksController.scala
  2. 2
      server/build.sbt
  3. 18
      server/test/controllers/BlocksControllerSpec.scala

45
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
}
/**

2
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)

18
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

Loading…
Cancel
Save