aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlad <vlad@driver.xyz>2017-02-28 19:03:38 -0800
committervlad <vlad@driver.xyz>2017-02-28 19:03:38 -0800
commit6be457e455be14657fcc6619e4a1556bd9fe0eec (patch)
tree757e327fd44e4d341af5131983ae2a0be60352da
parent82fa9d82e6b57a1e437e4da4fa99192ae18b254b (diff)
downloaddriver-core-6be457e455be14657fcc6619e4a1556bd9fe0eec.tar.gz
driver-core-6be457e455be14657fcc6619e4a1556bd9fe0eec.tar.bz2
driver-core-6be457e455be14657fcc6619e4a1556bd9fe0eec.zip
Nice utils to implement rest services 2v0.10.18
-rw-r--r--src/main/scala/xyz/driver/core/rest.scala27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/main/scala/xyz/driver/core/rest.scala b/src/main/scala/xyz/driver/core/rest.scala
index 5eef946..c559199 100644
--- a/src/main/scala/xyz/driver/core/rest.scala
+++ b/src/main/scala/xyz/driver/core/rest.scala
@@ -150,21 +150,36 @@ object rest {
}
}
- protected def unitResponse(request: Unmarshal[ResponseEntity])(
+ protected def unitResponse(request: Future[Unmarshal[ResponseEntity]])(
implicit reader: RootJsonReader[Option[Unit]]): OptionT[Future, Unit] =
- OptionT[Future, Unit](request.fold(Option.empty[Unit]))
+ OptionT[Future, Unit](request.flatMap(_.fold(Option.empty[Unit])))
- protected def optionalResponse[T](request: Unmarshal[ResponseEntity])(
+ protected def optionalResponse[T](request: Future[Unmarshal[ResponseEntity]])(
implicit reader: RootJsonReader[Option[T]]): OptionT[Future, T] =
- OptionT[Future, T](request.fold(Option.empty[T]))
+ OptionT[Future, T](request.flatMap(_.fold(Option.empty[T])))
- protected def listResponse[T](request: Unmarshal[ResponseEntity])(
+ protected def listResponse[T](request: Future[Unmarshal[ResponseEntity]])(
implicit reader: RootJsonReader[List[T]]): ListT[Future, T] =
- ListT[Future, T](request.fold(List.empty[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) =
+ HttpRequest(HttpMethods.GET, endpointUri(baseUri, path))
+
+ protected def get(baseUri: Uri, path: String, query: Map[String, String]) =
+ HttpRequest(HttpMethods.GET, endpointUri(baseUri, path, query))
+
+ protected def post(baseUri: Uri, path: String, httpEntity: RequestEntity) =
+ HttpRequest(HttpMethods.GET, endpointUri(baseUri, path), entity = httpEntity)
+
+ protected def postJson(baseUri: Uri, path: String, json: JsValue) =
+ HttpRequest(HttpMethods.GET, 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) =
baseUri.withPath(Uri.Path(path))