Skip to content

Commit

Permalink
server: Update playsonify to 2.0.1
Browse files Browse the repository at this point in the history
This allows to reduce the custom code for setting cache headers
  • Loading branch information
AlexITC committed May 4, 2019
1 parent 097bc75 commit 4744535
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
45 changes: 13 additions & 32 deletions server/app/controllers/BlocksController.scala
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
}

/**
Expand Down
2 changes: 1 addition & 1 deletion server/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions server/test/controllers/BlocksControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4744535

Please sign in to comment.