aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZach Smith <zach@driver.xyz>2018-08-22 09:59:36 -0700
committerGitHub <noreply@github.com>2018-08-22 09:59:36 -0700
commit46306f0c8f7e88e55a3b18df8ab212e9ea5e01f1 (patch)
tree6ee0d1d5d76222d8125ecbf21362edb7222ca780
parent1f320bfba719d4cc3a4d46bc90a30d8a6bbd91d4 (diff)
downloaddriver-core-46306f0c8f7e88e55a3b18df8ab212e9ea5e01f1.tar.gz
driver-core-46306f0c8f7e88e55a3b18df8ab212e9ea5e01f1.tar.bz2
driver-core-46306f0c8f7e88e55a3b18df8ab212e9ea5e01f1.zip
Add responseToListResponse to RestService (#199)v1.12.4
* Add responseToListResponse to RestService * Make pagination optional
-rw-r--r--src/main/scala/xyz/driver/core/rest/RestService.scala16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/core/rest/RestService.scala b/src/main/scala/xyz/driver/core/rest/RestService.scala
index 8d46d72..91b3550 100644
--- a/src/main/scala/xyz/driver/core/rest/RestService.scala
+++ b/src/main/scala/xyz/driver/core/rest/RestService.scala
@@ -6,6 +6,8 @@ import akka.stream.Materializer
import scala.concurrent.{ExecutionContext, Future}
import scalaz.{ListT, OptionT}
+import scalaz.syntax.equal._
+import scalaz.Scalaz.stringInstance
trait RestService extends Service {
@@ -69,4 +71,18 @@ trait RestService extends Service {
protected def endpointUri(baseUri: Uri, path: String, query: Seq[(String, String)]): Uri =
baseUri.withPath(Uri.Path(path)).withQuery(Uri.Query(query: _*))
+
+ protected def responseToListResponse[T: JsonFormat](pagination: Option[Pagination])(
+ response: HttpResponse): Future[ListResponse[T]] = {
+ import DefaultJsonProtocol._
+ val resourceCount = response.headers
+ .find(_.name() === ContextHeaders.ResourceCount)
+ .map(_.value().toInt)
+ .getOrElse(0)
+ val meta = ListResponse.Meta(resourceCount, pagination.getOrElse(Pagination(resourceCount, 1)))
+ Unmarshal(response.entity).to[List[T]].map(ListResponse(_, meta))
+ }
+
+ protected def responseToListResponse[T: JsonFormat](pagination: Pagination)(
+ response: HttpResponse): Future[ListResponse[T]] = responseToListResponse(Some(pagination))(response)
}