From 0e67322ed60eb4b624636847c59178edf03a6dfb Mon Sep 17 00:00:00 2001 From: adamw Date: Tue, 16 Mar 2021 14:56:42 +0100 Subject: [PATCH] Move serialisation to .toString --- .../sttp/model/sse/ServerSentEvent.scala | 30 +++++++++---------- .../sttp/model/sse/ServerSentEventTest.scala | 10 +++---- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/core/src/main/scala/sttp/model/sse/ServerSentEvent.scala b/core/src/main/scala/sttp/model/sse/ServerSentEvent.scala index 34020d44..98ecd7cf 100644 --- a/core/src/main/scala/sttp/model/sse/ServerSentEvent.scala +++ b/core/src/main/scala/sttp/model/sse/ServerSentEvent.scala @@ -3,11 +3,19 @@ package sttp.model.sse import scala.util.Try case class ServerSentEvent( - data: Option[String] = None, - eventType: Option[String] = None, - id: Option[String] = None, - retry: Option[Int] = None - ) + data: Option[String] = None, + eventType: Option[String] = None, + id: Option[String] = None, + retry: Option[Int] = None +) { + override def toString: String = { + val _data = data.map(_.split("\n")).map(_.map(line => Some(s"data: $line"))).getOrElse(Array.empty[Option[String]]) + val _event = eventType.map(event => s"event: $event") + val _id = id.map(id => s"id: $id") + val _retry = retry.map(retryCount => s"retry: $retryCount") + (_data :+ _event :+ _id :+ _retry).flatten.mkString("\n") + } +} object ServerSentEvent { // https://html.spec.whatwg.org/multipage/server-sent-events.html @@ -24,18 +32,10 @@ object ServerSentEvent { } } - def composeSSE(sse: ServerSentEvent): String = { - val data = sse.data.map(_.split("\n")).map(_.map(line => Some(s"data: $line"))).getOrElse(Array.empty[Option[String]]) - val event = sse.eventType.map(event => s"event: $event") - val id = sse.id.map(id => s"id: $id") - val retry = sse.retry.map(retryCount => s"retry: $retryCount") - (data :+ event :+ id :+ retry).flatten.mkString("\n") - } - private def combineData(event: ServerSentEvent, newData: String): ServerSentEvent = { event match { - case e@ServerSentEvent(Some(oldData), _, _, _) => e.copy(data = Some(s"$oldData\n$newData")) - case e@ServerSentEvent(None, _, _, _) => e.copy(data = Some(newData)) + case e @ ServerSentEvent(Some(oldData), _, _, _) => e.copy(data = Some(s"$oldData\n$newData")) + case e @ ServerSentEvent(None, _, _, _) => e.copy(data = Some(newData)) } } diff --git a/core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala b/core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala index 255fcba4..0143a40e 100644 --- a/core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala +++ b/core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala @@ -29,10 +29,9 @@ class ServerSentEventTest extends AnyFlatSpec with Matchers { } } - "composeSSE" should "successfully serialise Server Sent Event with all fields set" in { val sse = ServerSentEvent(Some("data"), Some("event"), Some("id1"), Some(10)) - ServerSentEvent.composeSSE(sse) shouldBe + sse.toString shouldBe s"""data: data |event: event |id: id1 @@ -41,15 +40,14 @@ class ServerSentEventTest extends AnyFlatSpec with Matchers { "composeSSE" should "omit fields that are not set" in { val sse = ServerSentEvent(Some("data"), None, Some("id1"), None) - ServerSentEvent.composeSSE(sse) shouldBe + sse.toString shouldBe s"""data: data |id: id1""".stripMargin } "composeSSE" should "successfully serialise multiline data event" in { val sse = ServerSentEvent( - Some( - """some data info 1 + Some("""some data info 1 |some data info 2 |some data info 3""".stripMargin), None, @@ -57,7 +55,7 @@ class ServerSentEventTest extends AnyFlatSpec with Matchers { None ) - ServerSentEvent.composeSSE(sse) shouldBe + sse.toString shouldBe s"""data: some data info 1 |data: some data info 2 |data: some data info 3""".stripMargin