From 5100bb4ff1c22dc92b4a0dd9b37783f943e6eb9e Mon Sep 17 00:00:00 2001 From: Lintterer Date: Thu, 8 Feb 2024 10:55:23 +0100 Subject: [PATCH 1/2] feat: handle case insensitive mapping internal for client --- build.sbt | 2 +- .../smithy4play/client/GenericAPIClient.scala | 17 ++++++++--------- .../smithy4play/client/RequestClient.scala | 2 +- .../smithy4play/client/SmithyPlayClient.scala | 2 +- .../client/SmithyPlayClientEndpoint.scala | 14 ++++++++------ .../de/innfactory/smithy4play/package.scala | 2 +- smithy4playTest/test/models/TestBase.scala | 4 ++-- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/build.sbt b/build.sbt index 1f312631..c8473041 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ import sbt.Compile import sbt.Keys.cleanFiles -val releaseVersion = sys.env.getOrElse("TAG", "1.0.0-Gamma") +val releaseVersion = sys.env.getOrElse("TAG", "1.0.1-Gamma") addCommandAlias("publishSmithy4Play", "smithy4play/publish") addCommandAlias("publishLocalSmithy4Play", "smithy4play/publishLocal") addCommandAlias("generateCoverage", "clean; coverage; test; coverageReport") diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala index c9e98dca..c01e0830 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala @@ -1,10 +1,9 @@ package de.innfactory.smithy4play.client import cats.data.Kleisli -import de.innfactory.smithy4play.{ ClientResponse, RunnableClientRequest } +import de.innfactory.smithy4play.{ClientResponse, RunnableClientRequest} import smithy4s.Service -import smithy4s.http.CaseInsensitive -import smithy4s.kinds.{ FunctorK, FunctorK5, Kind1, PolyFunction5 } +import smithy4s.kinds.{Kind1, PolyFunction5} import scala.concurrent.ExecutionContext @@ -21,7 +20,7 @@ private class GenericAPIClient[Alg[_[_, _, _, _, _]]]( smithyPlayClient.service.fromPolyFunction(this.opToResponse()) private def transformer( - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]] + additionalHeaders: Option[Map[String, Seq[String]]] ): Alg[Kind1[ClientResponse]#toKind5] = smithyPlayClient.service.fromPolyFunction(this.opToResponse(additionalHeaders)) @@ -30,14 +29,14 @@ private class GenericAPIClient[Alg[_[_, _, _, _, _]]]( new PolyFunction5[smithyPlayClient.service.Operation, Kind1[RunnableClientRequest]#toKind5] { override def apply[I, E, O, SI, SO]( fa: smithyPlayClient.service.Operation[I, E, O, SI, SO] - ): Kleisli[ClientResponse, Option[Map[CaseInsensitive, Seq[String]]], O] = - Kleisli[ClientResponse, Option[Map[CaseInsensitive, Seq[String]]], O] { additionalHeaders => + ): Kleisli[ClientResponse, Option[Map[String, Seq[String]]], O] = + Kleisli[ClientResponse, Option[Map[String, Seq[String]]], O] { additionalHeaders => smithyPlayClient.send(fa, additionalHeaders) } } private def opToResponse( - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]] + additionalHeaders: Option[Map[String, Seq[String]]] ): PolyFunction5[smithyPlayClient.service.Operation, Kind1[ClientResponse]#toKind5] = new PolyFunction5[smithyPlayClient.service.Operation, Kind1[ClientResponse]#toKind5] { override def apply[I, E, O, SI, SO]( @@ -53,7 +52,7 @@ object GenericAPIClient { def withClientAndHeaders( client: RequestClient, - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]], + additionalHeaders: Option[Map[String, Seq[String]]], additionalSuccessCodes: List[Int] = List.empty )(implicit ec: ExecutionContext): Alg[Kind1[ClientResponse]#toKind5] = apply(service, additionalHeaders, additionalSuccessCodes, client) @@ -75,7 +74,7 @@ object GenericAPIClient { def apply[Alg[_[_, _, _, _, _]]]( serviceI: Service[Alg], - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]], + additionalHeaders: Option[Map[String, Seq[String]]], additionalSuccessCodes: List[Int], client: RequestClient )(implicit ec: ExecutionContext): Alg[Kind1[ClientResponse]#toKind5] = diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/RequestClient.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/RequestClient.scala index 45919432..0b7a7680 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/RequestClient.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/RequestClient.scala @@ -11,7 +11,7 @@ trait RequestClient { def send( method: String, path: String, - headers: Map[CaseInsensitive, Seq[String]], + headers: Map[String, Seq[String]], request: EndpointRequest ): Future[HttpResponse[Blob]] } diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClient.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClient.scala index 6311dd95..4cd2446f 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClient.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClient.scala @@ -16,7 +16,7 @@ class SmithyPlayClient[Alg[_[_, _, _, _, _]], F[_]]( def send[I, E, O, SI, SO]( op: service.Operation[I, E, O, SI, SO], - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]] + additionalHeaders: Option[Map[String, Seq[String]]] ): ClientResponse[O] = { val endpoint = service.endpoint(op) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClientEndpoint.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClientEndpoint.scala index fb632569..855c1fc4 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClientEndpoint.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/SmithyPlayClientEndpoint.scala @@ -2,10 +2,7 @@ package de.innfactory package smithy4play package client -import alloy.SimpleRestJson -import aws.protocols.RestXml import cats.implicits._ -import play.api.http.MimeTypes import smithy4s.codecs.PayloadError import smithy4s.http._ import smithy4s.{ Blob, Endpoint, Hints, Schema } @@ -16,7 +13,7 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O, endpoint: Endpoint[Op, I, E, O, SI, SO], serviceHints: Hints, baseUri: String, - additionalHeaders: Option[Map[CaseInsensitive, Seq[String]]], + additionalHeaders: Option[Map[String, Seq[String]]], additionalSuccessCodes: List[Int], httpEndpoint: HttpEndpoint[I], input: I, @@ -38,13 +35,18 @@ private[smithy4play] class SmithyPlayClientEndpoint[Op[_, _, _, _, _], I, E, O, val headers = metadata.headers val contentTypeOpt = headers.get(contentTypeKey) val contentType = contentTypeOpt.getOrElse(Seq(serviceContentType)) - val headersWithAuth = if (additionalHeaders.isDefined) headers.combine(additionalHeaders.get) else headers + val mappedHeaders = headers.map { case (insensitive, value) => + (insensitive.toString, value) + } + val headersWithAuth = + if (additionalHeaders.isDefined) mappedHeaders.combine(additionalHeaders.get) + else mappedHeaders val code = httpEndpoint.code val response = client.send( httpEndpoint.method.toString, path, - headersWithAuth.updated(contentTypeKey, contentType), + headersWithAuth.updated(contentTypeKey.toString, contentType), writeInputToBlob(input, contentType) ) decodeResponse(response, code) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala index 921c6f32..0ab98f10 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/package.scala @@ -34,7 +34,7 @@ package object smithy4play { case class ContentType(value: String) type ClientResponse[O] = Future[Either[SmithyPlayClientEndpointErrorResponse, HttpResponse[O]]] - type RunnableClientRequest[O] = Kleisli[ClientResponse, Option[Map[CaseInsensitive, Seq[String]]], O] + type RunnableClientRequest[O] = Kleisli[ClientResponse, Option[Map[String, Seq[String]]], O] type RouteResult[O] = EitherT[Future, ContextRouteError, O] type ContextRoute[O] = Kleisli[RouteResult, RoutingContext, O] diff --git a/smithy4playTest/test/models/TestBase.scala b/smithy4playTest/test/models/TestBase.scala index 949afc29..f173b0d3 100644 --- a/smithy4playTest/test/models/TestBase.scala +++ b/smithy4playTest/test/models/TestBase.scala @@ -20,11 +20,11 @@ trait TestBase extends PlaySpec with BaseOneAppPerSuite with FakeApplicationFact override def send( method: String, path: String, - headers: Map[CaseInsensitive, Seq[String]], + headers: Map[String, Seq[String]], result: EndpointRequest ): Future[HttpResponse[Blob]] = { val baseRequest: FakeRequest[AnyContentAsEmpty.type] = FakeRequest(method, path) - .withHeaders(headers.toList.flatMap(headers => headers._2.map(v => (headers._1.toString, v))): _*) + .withHeaders(headers.toList.flatMap(headers => headers._2.map(v => (headers._1, v))): _*) val res = if (!result.body.isEmpty) route(app, baseRequest.withBody(result.body.toArray)).get else From e3fdfce753b1c7212f85d8554009ebd6204fb4e0 Mon Sep 17 00:00:00 2001 From: Lintterer Date: Thu, 8 Feb 2024 11:18:39 +0100 Subject: [PATCH 2/2] chore: scalafmt --- .../de/innfactory/smithy4play/client/GenericAPIClient.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala index c01e0830..ffd12415 100644 --- a/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala +++ b/smithy4play/src/main/scala/de/innfactory/smithy4play/client/GenericAPIClient.scala @@ -1,9 +1,9 @@ package de.innfactory.smithy4play.client import cats.data.Kleisli -import de.innfactory.smithy4play.{ClientResponse, RunnableClientRequest} +import de.innfactory.smithy4play.{ ClientResponse, RunnableClientRequest } import smithy4s.Service -import smithy4s.kinds.{Kind1, PolyFunction5} +import smithy4s.kinds.{ Kind1, PolyFunction5 } import scala.concurrent.ExecutionContext