Skip to content

Commit

Permalink
BDOG-1512 Add Stream reads for HttpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-lamed committed Apr 26, 2022
1 parent a1871d2 commit 70fd5bc
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,27 @@ trait StreamHttpReadsInstances {
def tag[A](instance: A): A with client.Streaming =
instance.asInstanceOf[A with client.Streaming]

implicit def readEitherSource(implicit mat: Materializer, errorTimeout: ErrorTimeout): client.StreamHttpReads[Either[UpstreamErrorResponse, Source[ByteString, _]]] =
tag[HttpReads[Either[UpstreamErrorResponse, Source[ByteString, _]]]](
implicit val readStreamHttpResponse: client.StreamHttpReads[HttpResponse] =
tag[HttpReads[HttpResponse]](
HttpReads.ask.map { case (_, _, response) => response }
)

implicit def readStreamEitherHttpResponse(implicit mat: Materializer, errorTimeout: ErrorTimeout): client.StreamHttpReads[Either[UpstreamErrorResponse, HttpResponse]] =
tag[HttpReads[Either[UpstreamErrorResponse, HttpResponse]]](
HttpReads.ask.flatMap { case (method, url, response) =>
HttpErrorFunctions.handleResponseEitherStream(method, url)(response) match {
case Left(err) => HttpReads.pure(Left(err))
case Right(response) => HttpReads.pure(Right(response.bodyAsSource))
case Right(response) => HttpReads.pure(Right(response))
}
}
)

implicit def readEitherSource(implicit mat: Materializer, errorTimeout: ErrorTimeout): client.StreamHttpReads[Either[UpstreamErrorResponse, Source[ByteString, _]]] =
tag[HttpReads[Either[UpstreamErrorResponse, Source[ByteString, _]]]](
readStreamEitherHttpResponse
.map(_.map(_.bodyAsSource))
)

implicit def readSource(implicit mat: Materializer, errorTimeout: ErrorTimeout): client.StreamHttpReads[Source[ByteString, _]] =
tag[HttpReads[Source[ByteString, _]]](
readEitherSource
Expand Down

0 comments on commit 70fd5bc

Please sign in to comment.