From a1871d2057cc98da30640d94ac4d3ac1315be034 Mon Sep 17 00:00:00 2001 From: colin-lamed <9568290+colin-lamed@users.noreply.github.com> Date: Tue, 26 Apr 2022 13:22:52 +0100 Subject: [PATCH 1/2] Fix case insensitive header lookup --- .../src/main/scala/uk/gov/hmrc/http/HttpResponse.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpResponse.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpResponse.scala index f8712c33..2cb99ab3 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpResponse.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/HttpResponse.scala @@ -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" From 70fd5bca634b00a706c5a612a38be5a0e3001f21 Mon Sep 17 00:00:00 2001 From: colin-lamed <9568290+colin-lamed@users.noreply.github.com> Date: Tue, 26 Apr 2022 16:29:28 +0100 Subject: [PATCH 2/2] BDOG-1512 Add Stream reads for HttpResponse --- .../scala/uk/gov/hmrc/http/client/package.scala | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/package.scala b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/package.scala index 4b12f2ff..f9361a14 100644 --- a/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/package.scala +++ b/http-verbs-common/src/main/scala/uk/gov/hmrc/http/client/package.scala @@ -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