-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request streaming support through circe-streaming
- Loading branch information
1 parent
27f1b6b
commit 6fe91d7
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.finch.circe | ||
|
||
import com.twitter.io.Buf | ||
import com.twitter.util.Future | ||
import io.catbird.util._ | ||
import io.circe.Decoder | ||
import io.circe.streaming.{byteParser, decoder} | ||
import io.iteratee.Enumeratee | ||
|
||
trait Streaming { | ||
implicit def decoderEnumeratee[A : Decoder]: Enumeratee[Future, Buf, A] = | ||
Enumeratee | ||
.map[Future, Buf, Array[Byte]](Buf.ByteArray.Owned.extract) | ||
.andThen(byteParser[Future]) | ||
.andThen(decoder[Future, A]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.finch | ||
|
||
import com.twitter.io.{Buf, Reader} | ||
import com.twitter.util.Future | ||
import io.catbird.util._ | ||
import io.finch.Endpoint.Result | ||
import io.finch.items.RequestItem | ||
import io.iteratee.{Enumeratee, Enumerator} | ||
|
||
/** | ||
* Streaming module | ||
*/ | ||
package object streaming { | ||
|
||
private[finch] def enumeratorFromReader(reader: Reader): Enumerator[Future, Buf] = { | ||
Enumerator.liftM(reader.read(Int.MaxValue)).flatMap({ | ||
case None => Enumerator.empty[Future, Buf] | ||
case Some(buf) => Enumerator.enumOne[Future, Buf](buf).append(enumeratorFromReader(reader)) | ||
}) | ||
} | ||
|
||
/** | ||
* An evaluating [[Endpoint]] that reads a required chunked streaming binary body, interpreted as | ||
* an `Enumerator[Future, Buf]`. The returned [[Endpoint]] only matches chunked (streamed) requests. | ||
*/ | ||
def asyncBufBody: Endpoint[Enumerator[Future, Buf]] = new Endpoint[Enumerator[Future, Buf]] { | ||
final def apply(input: Input): Endpoint.Result[Enumerator[Future, Buf]] = | ||
if (!input.request.isChunked) EndpointResult.Skipped | ||
else EndpointResult.Matched(input, | ||
Rerunnable(Output.payload(enumeratorFromReader(input.request.reader)))) | ||
|
||
final override def item: RequestItem = items.BodyItem | ||
final override def toString: String = "asyncBufBody" | ||
} | ||
|
||
/** | ||
* An evaluating [[Endpoint]] that reads a required chunked streaming binary body, interpreted as | ||
* an `Enumerator[Future, A]`. The returned [[Endpoint]] only matches chunked (streamed) requests. | ||
*/ | ||
def asyncJsonBody[A](implicit ee: Enumeratee[Future, Buf, A]): Endpoint[Enumerator[Future, A]] = { | ||
new Endpoint[Enumerator[Future, A]] { | ||
final def apply(input: Input): Result[Enumerator[Future, A]] = asyncBufBody.map(_.through(ee))(input) | ||
|
||
final override def item: RequestItem = items.BodyItem | ||
final override def toString: String = "asyncJsonBody" | ||
} | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
streaming/src/test/scala/io/finch/streaming/EndpointStreamingSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.finch.streaming | ||
|
||
import com.twitter.finagle.http.Request | ||
import com.twitter.io.Buf | ||
import com.twitter.util.Await | ||
import io.catbird.util._ | ||
import io.finch.Input | ||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class EndpointStreamingSpec extends FlatSpec with Matchers { | ||
|
||
"asyncBufBody" should "interpret chunked request as Enumerator[Future, Buf]" in { | ||
val req = Request() | ||
req.setChunked(chunked = true) | ||
val writer = req.writer | ||
|
||
val vector = Vector("foo", "bar") | ||
|
||
def write(vector: Vector[String]): Unit = { | ||
if (vector.isEmpty) { | ||
writer.close() | ||
} else { | ||
writer.write(Buf.Utf8(vector.head)).foreach(_ => write(vector.tail)) | ||
} | ||
} | ||
write(vector) | ||
|
||
val enumerator = asyncBufBody(Input(req, Seq.empty)).awaitValueUnsafe().get | ||
|
||
Await.result(enumerator.map(Buf.Utf8.unapply).map(_.get).toVector) shouldBe vector | ||
} | ||
|
||
} |