You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pekko.apache.org by fa...@apache.org on 2022/10/31 09:36:14 UTC

[incubator-pekko-samples] 01/02: Use Akka HTTP marshalling to marshall the JSON request data

This is an automated email from the ASF dual-hosted git repository.

fanningpj pushed a commit to branch marshallRequest
in repository https://gitbox.apache.org/repos/asf/incubator-pekko-samples.git

commit fcb8733bb806d7eddbc9f65fbf1333d8c8673735
Author: Arnout Engelen <ar...@bzzt.net>
AuthorDate: Fri Nov 1 15:04:00 2019 +0100

    Use Akka HTTP marshalling to marshall the JSON request data
---
 .../main/scala/sample/sharding/WeatherEdges.scala  | 35 +++++++++++-----------
 1 file changed, 18 insertions(+), 17 deletions(-)

diff --git a/akka-sample-sharding-scala/src/main/scala/sample/sharding/WeatherEdges.scala b/akka-sample-sharding-scala/src/main/scala/sample/sharding/WeatherEdges.scala
index 0bbc99a..6c46188 100644
--- a/akka-sample-sharding-scala/src/main/scala/sample/sharding/WeatherEdges.scala
+++ b/akka-sample-sharding-scala/src/main/scala/sample/sharding/WeatherEdges.scala
@@ -60,10 +60,15 @@ object WeatherHttpApi {
 
   final case class Data(stationId: Int, deviceId: Int, data: List[Double])
 
+  // This formatter determines how to convert to and from Data objects:
+  import spray.json._
+  import spray.json.DefaultJsonProtocol._
+  implicit val dataFormat: RootJsonFormat[Data] = jsonFormat3(Data)
+
   def apply(stationId: Int, port: Int): Behavior[Data] =
     Behaviors.setup[Data] { context =>
       import akka.http.scaladsl.Http
-      import akka.http.scaladsl.model.{ ContentTypes, HttpEntity, HttpMethods, HttpRequest }
+      import akka.http.scaladsl.model.HttpRequest
 
       implicit val sys = context.system
       import context.executionContext
@@ -74,23 +79,19 @@ object WeatherHttpApi {
       def runRequest(req: HttpRequest) =
         http.singleRequest(req).flatMap { _.entity.dataBytes.runReduce(_ ++ _) }
 
+      // This import makes the 'format' above available to the Akka HTTP
+      // marshalling infractructure used when constructing the Post below:
+      import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._
+
+      import akka.http.scaladsl.client.RequestBuilding.Post
+
       Behaviors.receiveMessage[Data] {
-        case Data(sid, did, data) =>
-          // TODO marshalling vs scary hand-coded!
-          val values = data.mkString(",")
-          val json = s"""{"stationId":$sid,"deviceId":$did,"data":[$values]}"""
-          context.log.info("Read: {}", json)
-
-          // See https://doc.akka.io/docs/akka-http/current/common/index.html
-          // for marshalling, unmarshalling, json support in the wild.
-          val httpEntity = HttpEntity(ContentTypes.`application/json`, json)
-          runRequest(
-            HttpRequest(method = HttpMethods.POST, uri = s"http://localhost:$port/temperatures", entity = httpEntity))
-            .onComplete {
-              case Success(response) =>
-                context.log.info(response.utf8String)
-              case Failure(e) => context.log.error("Something wrong.", e)
-            }
+        case data: Data =>
+          runRequest(Post(s"http://localhost:$port/temperatures", data)).onComplete {
+            case Success(response) =>
+              context.log.info(response.utf8String)
+            case Failure(e) => context.log.error("Something wrong.", e)
+          }
 
           Behaviors.same
       }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@pekko.apache.org
For additional commands, e-mail: commits-help@pekko.apache.org