aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/core/rest/RestService.scala
diff options
context:
space:
mode:
authorZach Smith <zach@driver.xyz>2017-09-27 22:20:53 -0700
committerZach Smith <zach@driver.xyz>2017-10-12 16:48:08 -0700
commit2c524be93052fc6d57359a00fd60d957099885c6 (patch)
treeb68467ca2fd4f4f101da1ce315c022ab12220145 /src/main/scala/xyz/driver/core/rest/RestService.scala
parentb00892d723f6dedf50dc1c1fde7d443e9c3f9497 (diff)
downloaddriver-core-2c524be93052fc6d57359a00fd60d957099885c6.tar.gz
driver-core-2c524be93052fc6d57359a00fd60d957099885c6.tar.bz2
driver-core-2c524be93052fc6d57359a00fd60d957099885c6.zip
Refactor rest package into separate files
Diffstat (limited to 'src/main/scala/xyz/driver/core/rest/RestService.scala')
-rw-r--r--src/main/scala/xyz/driver/core/rest/RestService.scala54
1 files changed, 54 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
new file mode 100644
index 0000000..aed8d28
--- /dev/null
+++ b/src/main/scala/xyz/driver/core/rest/RestService.scala
@@ -0,0 +1,54 @@
+package xyz.driver.core.rest
+
+import akka.http.scaladsl.model._
+import akka.http.scaladsl.unmarshalling.{Unmarshal, Unmarshaller}
+import akka.stream.Materializer
+
+import scala.concurrent.{ExecutionContext, Future}
+import scalaz.{ListT, OptionT}
+
+trait RestService extends Service {
+
+ import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+ import spray.json._
+
+ protected implicit val exec: ExecutionContext
+ protected implicit val materializer: Materializer
+
+ implicit class ResponseEntityFoldable(entity: Unmarshal[ResponseEntity]) {
+ def fold[T](default: => T)(implicit um: Unmarshaller[ResponseEntity, T]): Future[T] =
+ if (entity.value.isKnownEmpty()) Future.successful[T](default) else entity.to[T]
+ }
+
+ protected def unitResponse(request: Future[Unmarshal[ResponseEntity]]): OptionT[Future, Unit] =
+ OptionT[Future, Unit](request.flatMap(_.to[String]).map(_ => Option(())))
+
+ protected def optionalResponse[T](request: Future[Unmarshal[ResponseEntity]])(
+ implicit um: Unmarshaller[ResponseEntity, Option[T]]): OptionT[Future, T] =
+ OptionT[Future, T](request.flatMap(_.fold(Option.empty[T])))
+
+ protected def listResponse[T](request: Future[Unmarshal[ResponseEntity]])(
+ implicit um: Unmarshaller[ResponseEntity, List[T]]): ListT[Future, T] =
+ ListT[Future, T](request.flatMap(_.fold(List.empty[T])))
+
+ protected def jsonEntity(json: JsValue): RequestEntity =
+ HttpEntity(ContentTypes.`application/json`, json.compactPrint)
+
+ protected def get(baseUri: Uri, path: String, query: Seq[(String, String)] = Seq.empty) =
+ HttpRequest(HttpMethods.GET, endpointUri(baseUri, path, query))
+
+ protected def post(baseUri: Uri, path: String, httpEntity: RequestEntity) =
+ HttpRequest(HttpMethods.POST, endpointUri(baseUri, path), entity = httpEntity)
+
+ protected def postJson(baseUri: Uri, path: String, json: JsValue) =
+ HttpRequest(HttpMethods.POST, endpointUri(baseUri, path), entity = jsonEntity(json))
+
+ protected def delete(baseUri: Uri, path: String) =
+ HttpRequest(HttpMethods.DELETE, endpointUri(baseUri, path))
+
+ protected def endpointUri(baseUri: Uri, path: String): Uri =
+ baseUri.withPath(Uri.Path(path))
+
+ protected def endpointUri(baseUri: Uri, path: String, query: Seq[(String, String)]): Uri =
+ baseUri.withPath(Uri.Path(path)).withQuery(Uri.Query(query: _*))
+}