Skip to content

Commit

Permalink
Move serialisation to .toString
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Mar 16, 2021
1 parent 3cf084d commit 0e67322
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
30 changes: 15 additions & 15 deletions core/src/main/scala/sttp/model/sse/ServerSentEvent.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}

Expand Down
10 changes: 4 additions & 6 deletions core/src/test/scala/sttp/model/sse/ServerSentEventTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,23 +40,22 @@ 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,
None,
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
Expand Down

0 comments on commit 0e67322

Please sign in to comment.