Skip to content

Commit

Permalink
Replace Client.streaming with Client.stream
Browse files Browse the repository at this point in the history
  • Loading branch information
rossabaker committed Sep 30, 2018
1 parent 1de73ae commit a0a565d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 6 additions & 0 deletions client/src/main/scala/org/http4s/client/Client.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ trait Client[F[_]] {
@deprecated("Use toHttpApp. Call `.mapF(OptionT.liftF)` if OptionT is really desired.", "0.19")
def toHttpService: HttpService[F]

/** Run the request as a stream. The response lifecycle is equivalent
* to the returned Stream's. */
def stream(req: Request[F]): Stream[F, Response[F]]

@deprecated("Use `client.stream(req).flatMap(f)`", "0.19.0-RC1")
def streaming[A](req: Request[F])(f: Response[F] => Stream[F, A]): Stream[F, A]

@deprecated("Use `Stream.eval(req).flatMap(client.stream).flatMap(f)`", "0.19.0-RC1")
def streaming[A](req: F[Request[F]])(f: Response[F] => Stream[F, A]): Stream[F, A]

def expectOr[A](req: Request[F])(onError: Response[F] => F[Throwable])(
Expand Down
7 changes: 5 additions & 2 deletions client/src/main/scala/org/http4s/client/DefaultClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ private[client] abstract class DefaultClient[F[_]](implicit F: Bracket[F, Throwa
def toHttpService: HttpService[F] =
toHttpApp.mapF(OptionT.liftF(_))

def stream(req: Request[F]): Stream[F, Response[F]] =
Stream.resource(run(req))

def streaming[A](req: Request[F])(f: Response[F] => Stream[F, A]): Stream[F, A] =
Stream.resource(run(req)).flatMap(f)
stream(req).flatMap(f)

def streaming[A](req: F[Request[F]])(f: Response[F] => Stream[F, A]): Stream[F, A] =
Stream.eval(req).flatMap(streaming(_)(f))
Stream.eval(req).flatMap(stream).flatMap(f)

def expectOr[A](req: Request[F])(onError: Response[F] => F[Throwable])(
implicit d: EntityDecoder[F, A]): F[A] = {
Expand Down
17 changes: 5 additions & 12 deletions client/src/test/scala/org/http4s/client/ClientSyntaxSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,28 +237,21 @@ class ClientSyntaxSpec extends Http4sSpec with Http4sClientDsl[IO] with MustThro
EntityDecoder.text[IO].orElse(edec)) must returnValue("Accept: text/*, image/jpeg")
}

"streaming returns a stream" in {
"stream returns a stream" in {
client
.streaming(req)(_.body.through(fs2.text.utf8Decode))
.compile
.toVector
.unsafeRunSync() must_== Vector("hello")
}

"streaming returns a stream from a request task" in {
client
.streaming(req)(_.body.through(fs2.text.utf8Decode))
.stream(req)
.flatMap(_.body.through(fs2.text.utf8Decode))
.compile
.toVector
.unsafeRunSync() must_== Vector("hello")
}

"streaming disposes of the response on success" in {
assertDisposes(_.streaming(req)(_.body).compile.drain)
assertDisposes(_.stream(req).compile.drain)
}

"streaming disposes of the response on failure" in {
assertDisposes(_.streaming(req)(_ => Stream.raiseError[IO](SadTrombone)).compile.drain)
assertDisposes(_.stream(req).flatMap(_ => Stream.raiseError[IO](SadTrombone)).compile.drain)
}

"toService disposes of the response on success" in {
Expand Down

0 comments on commit a0a565d

Please sign in to comment.