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:13 UTC

[incubator-pekko-samples] branch marshallRequest created (now 76c7132)

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

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


      at 76c7132  Unmarshalling the response

This branch includes the following new commits:

     new fcb8733  Use Akka HTTP marshalling to marshall the JSON request data
     new 76c7132  Unmarshalling the response

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



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


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

Posted by fa...@apache.org.
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


[incubator-pekko-samples] 02/02: Unmarshalling the response

Posted by fa...@apache.org.
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 76c71324fbca9f1e83dbb83aec2971fd305daadd
Author: Arnout Engelen <ar...@bzzt.net>
AuthorDate: Fri Nov 1 15:22:55 2019 +0100

    Unmarshalling the response
---
 .../main/scala/sample/sharding/WeatherEdges.scala   | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 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 6c46188..ddfb782 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
@@ -6,6 +6,7 @@ import scala.util.{ Failure, Random, Success }
 import akka.actor.typed.{ ActorRef, ActorSystem, Behavior }
 import akka.actor.typed.scaladsl.{ Behaviors, TimerScheduler }
 import com.typesafe.config.ConfigFactory
+import akka.stream.SystemMaterializer
 
 /** Simulate devices and stations.
  * In the wild, each station would run its own simple system,
@@ -68,16 +69,12 @@ object WeatherHttpApi {
   def apply(stationId: Int, port: Int): Behavior[Data] =
     Behaviors.setup[Data] { context =>
       import akka.http.scaladsl.Http
-      import akka.http.scaladsl.model.HttpRequest
 
-      implicit val sys = context.system
       import context.executionContext
       import akka.actor.typed.scaladsl.adapter._
+      import akka.http.scaladsl.unmarshalling.Unmarshal
       val http = Http(context.system.toClassic)
-
-      // Run and completely consume a single akka http request
-      def runRequest(req: HttpRequest) =
-        http.singleRequest(req).flatMap { _.entity.dataBytes.runReduce(_ ++ _) }
+      implicit val mat: akka.stream.Materializer = SystemMaterializer(context.system.toClassic).materializer
 
       // This import makes the 'format' above available to the Akka HTTP
       // marshalling infractructure used when constructing the Post below:
@@ -87,11 +84,13 @@ object WeatherHttpApi {
 
       Behaviors.receiveMessage[Data] {
         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)
-          }
+          http.singleRequest(Post(s"http://localhost:$port/temperatures", data))
+            .flatMap(res => Unmarshal(res).to[String])
+            .onComplete {
+              case Success(response) =>
+                context.log.info(response)
+              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