Skip to content

Commit

Permalink
Merge pull request #143 from hmrc/BDOG-1512
Browse files Browse the repository at this point in the history
Bdog 1512
  • Loading branch information
colin-lamed authored Apr 27, 2022
2 parents 9e55668 + 70fd5bc commit 88ff2a6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ trait HttpResponse {
Source.single(ByteString(body))

def header(key: String): Option[String] =
headers.get(key).flatMap(_.headOption)
headers.collectFirst { case (k, v :: _) if k.equalsIgnoreCase(key) => v }

override def toString: String =
s"HttpResponse status=$status"
Expand Down
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 88ff2a6

Please sign in to comment.