Skip to content

Instantly share code, notes, and snippets.

@sfosdal
Last active July 5, 2018 15:56
Show Gist options
  • Save sfosdal/87e8b95c2d06eaa1b53021c4acd69a15 to your computer and use it in GitHub Desktop.
Save sfosdal/87e8b95c2d06eaa1b53021c4acd69a15 to your computer and use it in GitHub Desktop.
package com.apptentive.profile.routes
import java.util.UUID
import java.util.UUID.fromString
import akka.http.scaladsl.server.Directive1
import akka.http.scaladsl.server.Directives._
import com.apptentive.profile.routes.Paging._
import scala.util._
object Paging {
val DefaultPageSize: Int = 100
val ValidPageSizes: Range = 1 to 4000
}
case class Paging(startsAfter: Option[UUID] = None, size: Int = DefaultPageSize)
trait Pagination {
def extractPaging: Directive1[Paging] = {
parameterMap.flatMap { params =>
val size = Try(params.get("page_size").map(_.toInt).get)
val startsAfter = Try(params.get("starts_after").map(fromString))
(size, startsAfter) match {
case (Failure(_), _) | (_, Failure(_)) | (Success(s), _) if !ValidPageSizes.contains(s) =>
reject
case (_, _) =>
provide(Paging(startsAfter.getOrElse(None), size.getOrElse(DefaultPageSize)))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment