You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marvin.apache.org by lu...@apache.org on 2020/10/28 18:01:08 UTC
[incubator-marvin] branch develop updated: Allowing the predictor
endpoint to return JSON within result, in addition to strings
This is an automated email from the ASF dual-hosted git repository.
lucasbm88 pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/incubator-marvin.git
The following commit(s) were added to refs/heads/develop by this push:
new 2ac8a7f Allowing the predictor endpoint to return JSON within result, in addition to strings
2ac8a7f is described below
commit 2ac8a7f37af8626d47a6896b1d3e6646793f41e9
Author: Lucas Bonatto Miguel <lu...@olxbr.com>
AuthorDate: Sun Oct 25 17:30:40 2020 -0300
Allowing the predictor endpoint to return JSON within result, in addition to strings
---
.../scala/org/marvin/executor/api/GenericAPI.scala | 11 ++++++++++-
.../org/marvin/executor/api/GenericAPITest.scala | 23 ++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/engine-executor/src/main/scala/org/marvin/executor/api/GenericAPI.scala b/engine-executor/src/main/scala/org/marvin/executor/api/GenericAPI.scala
index 9406f86..5d18a6d 100644
--- a/engine-executor/src/main/scala/org/marvin/executor/api/GenericAPI.scala
+++ b/engine-executor/src/main/scala/org/marvin/executor/api/GenericAPI.scala
@@ -64,6 +64,7 @@ trait GenericAPIFunctions {
object GenericAPI {
case class HealthStatus(status: String, additionalMessage: String)
case class DefaultHttpResponse(result: String)
+ case class DefaultJsHttpResponse(result: JsValue)
case class DefaultOnlineRequest(params: Option[JsValue] = Option.empty, message: Option[JsValue] = Option.empty)
case class DefaultBatchRequest(params: Option[JsValue] = Option.empty)
}
@@ -85,6 +86,7 @@ class GenericAPI(system: ActorSystem,
val log: LoggingAdapter = Logging.getLogger(system, this)
implicit val defaultHttpResponseFormat: RootJsonFormat[DefaultHttpResponse] = jsonFormat1(DefaultHttpResponse)
+ implicit val defaultJsHttpResponseFormat: RootJsonFormat[DefaultJsHttpResponse] = jsonFormat1(DefaultJsHttpResponse)
implicit val defaultOnlineRequestFormat: RootJsonFormat[DefaultOnlineRequest] = jsonFormat2(DefaultOnlineRequest)
implicit val defaultBatchRequestFormat: RootJsonFormat[DefaultBatchRequest] = jsonFormat1(DefaultBatchRequest)
implicit val healthStatusFormat: RootJsonFormat[HealthStatus] = jsonFormat2(HealthStatus)
@@ -101,7 +103,14 @@ class GenericAPI(system: ActorSystem,
val responseFuture = onlineExecute("predictor", request.params.getOrElse(engineParams).toString, request.message.get.toString)
onComplete(responseFuture) {
- case Success(response) => complete(DefaultHttpResponse(response))
+ case Success(response: String) => {
+ complete {
+ Try(response.parseJson) match {
+ case Success(parsed) => DefaultJsHttpResponse(parsed)
+ case Failure(e) => DefaultHttpResponse(response)
+ }
+ }
+ }
case Failure(e) =>
log.info("RECEIVE FAILURE!!! " + e.getMessage + e.getClass)
failWith(e)
diff --git a/engine-executor/src/test/scala/org/marvin/executor/api/GenericAPITest.scala b/engine-executor/src/test/scala/org/marvin/executor/api/GenericAPITest.scala
index 8a7c7d8..67765a2 100644
--- a/engine-executor/src/test/scala/org/marvin/executor/api/GenericAPITest.scala
+++ b/engine-executor/src/test/scala/org/marvin/executor/api/GenericAPITest.scala
@@ -65,6 +65,29 @@ class GenericAPITest extends WordSpec with ScalatestRouteTest with Matchers with
}(result)
}
+ "interpret the input message and respond with a complete json body" in {
+
+ val message = "{\"msg\":\"testQuery\"}"
+ val params = "{\"p1\":\"testParams\"}"
+ val response = """
+ {"numberForTest": 1,
+ "listOfResult": ["a","b"]
+ }
+ """
+
+ val result = Post("/predictor", HttpEntity(`application/json`, s"""{"params":$params,"message":$message}""")) ~> api.routes ~> runRoute
+
+ val expectedMessage = OnlineExecute(message, params)
+ actor.expectMsg(expectedMessage)
+ actor.reply(response)
+
+ check {
+ status shouldEqual StatusCode.int2StatusCode(200)
+ contentType shouldEqual ContentTypes.`application/json`
+ responseAs[String] shouldEqual s"""{"result":{"numberForTest":1,"listOfResult":["a","b"]}}"""
+ }(result)
+ }
+
"gracefully fail when message is not informed" in {
Post("/predictor", HttpEntity(`application/json`, s"""{"params":"testParams"}""")) ~> api.routes ~> check {
status shouldEqual StatusCodes.BadRequest