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

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

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