Skip to content

Commit

Permalink
Merge pull request #244 from innFactory/feat/internalCaseInsensitive
Browse files Browse the repository at this point in the history
feat: handle case insensitive mapping internal for client
  • Loading branch information
MoeQuadrat authored Feb 8, 2024
2 parents 1180afb + e3fdfce commit 5ee0f4d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package de.innfactory.smithy4play.client
import cats.data.Kleisli
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

Expand All @@ -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))

Expand All @@ -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](
Expand All @@ -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)
Expand All @@ -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] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 2 additions & 2 deletions smithy4playTest/test/models/TestBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5ee0f4d

Please sign in to comment.