From a7e3d39e1fb080b1dbb144f07fc089b65044d99d Mon Sep 17 00:00:00 2001 From: Sergiusz Kierat Date: Tue, 24 Dec 2024 09:52:54 +0100 Subject: [PATCH] Add server options to AWS Lambda handler *Why I did it?* LambdaHandler had the hardcoded AwsServerOptions, so delivering the custom server options was impossible. *How I did it:* LambdaHandler started to accept options by analogous to ZioLambdaHandler Closes #4064 --- .../aws/cdk/tests/CdkTestLambdaHandler.scala | 2 +- .../aws/cdk/IOLambdaHandlerV1Test.scala | 23 ++++++++++++++++--- .../aws/cdk/test/IOLambdaHandlerV1.scala | 4 ++-- .../serverless/aws/lambda/LambdaHandler.scala | 7 +++--- .../aws/ziolambda/ZioLambdaHandler.scala | 2 +- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/serverless/aws/cdk-tests/src/main/scala/sttp/tapir/serverless/aws/cdk/tests/CdkTestLambdaHandler.scala b/serverless/aws/cdk-tests/src/main/scala/sttp/tapir/serverless/aws/cdk/tests/CdkTestLambdaHandler.scala index ac79696e53..184b734af5 100644 --- a/serverless/aws/cdk-tests/src/main/scala/sttp/tapir/serverless/aws/cdk/tests/CdkTestLambdaHandler.scala +++ b/serverless/aws/cdk-tests/src/main/scala/sttp/tapir/serverless/aws/cdk/tests/CdkTestLambdaHandler.scala @@ -11,7 +11,7 @@ import java.io.{InputStream, OutputStream} /** Used by [[AwsCdkAppTemplate]] for integration tests */ -object CdkTestLambdaHandler extends LambdaHandler[IO, AwsRequestV1] { +object CdkTestLambdaHandler extends LambdaHandler[IO, AwsRequestV1](AwsCatsEffectServerOptions.default[IO]) { override protected def getAllEndpoints: List[ServerEndpoint[Any, IO]] = allEndpoints.toList override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = { diff --git a/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/IOLambdaHandlerV1Test.scala b/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/IOLambdaHandlerV1Test.scala index 1e47875e3c..d09484b94b 100644 --- a/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/IOLambdaHandlerV1Test.scala +++ b/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/IOLambdaHandlerV1Test.scala @@ -1,12 +1,13 @@ package sttp.tapir.serverless.aws.cdk +import cats.effect.IO import com.amazonaws.services.lambda.runtime.api.client.api._ import io.circe.generic.auto._ import io.circe.parser.decode import org.scalatest.funsuite.AnyFunSuite import org.scalatest.matchers.should.Matchers import sttp.tapir.serverless.aws.cdk.test.IOLambdaHandlerV1 -import sttp.tapir.serverless.aws.lambda.AwsResponse +import sttp.tapir.serverless.aws.lambda.{AwsCatsEffectServerOptions, AwsResponse} import java.io.{ByteArrayInputStream, ByteArrayOutputStream} @@ -81,10 +82,10 @@ class IOLambdaHandlerV1Test extends AnyFunSuite with Matchers { new LambdaClientContext() ) - test("lambda handler") { + test("lambda handler without encoding") { val output = new ByteArrayOutputStream() - val handler = new IOLambdaHandlerV1 + val handler = new IOLambdaHandlerV1(AwsCatsEffectServerOptions.noEncoding[IO]) handler.handleRequest(new ByteArrayInputStream(input.getBytes), output, context) val expected = AwsResponse( @@ -96,4 +97,20 @@ class IOLambdaHandlerV1Test extends AnyFunSuite with Matchers { decode[AwsResponse](output.toString()) shouldBe Right(expected) } + + test("lambda handler with default encoding") { + val output = new ByteArrayOutputStream() + + val handler = new IOLambdaHandlerV1(AwsCatsEffectServerOptions.default[IO]) + handler.handleRequest(new ByteArrayInputStream(input.getBytes), output, context) + + val expected = AwsResponse( + isBase64Encoded = true, + 200, + Map("Content-Length" -> "9", "Content-Type" -> "text/plain; charset=UTF-8"), + "SGkhIEp1bGll" + ) + + decode[AwsResponse](output.toString()) shouldBe Right(expected) + } } diff --git a/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/test/IOLambdaHandlerV1.scala b/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/test/IOLambdaHandlerV1.scala index d89f255d58..6d60178b07 100644 --- a/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/test/IOLambdaHandlerV1.scala +++ b/serverless/aws/cdk/src/test/scala/sttp/tapir/serverless/aws/cdk/test/IOLambdaHandlerV1.scala @@ -5,11 +5,11 @@ import cats.effect.unsafe.implicits.global import com.amazonaws.services.lambda.runtime.Context import io.circe.generic.auto._ import sttp.tapir.server.ServerEndpoint -import sttp.tapir.serverless.aws.lambda.{AwsRequestV1, LambdaHandler} +import sttp.tapir.serverless.aws.lambda.{AwsRequestV1, AwsServerOptions, LambdaHandler} import java.io.{InputStream, OutputStream} -class IOLambdaHandlerV1 extends LambdaHandler[IO, AwsRequestV1] { +class IOLambdaHandlerV1(options: AwsServerOptions[IO]) extends LambdaHandler[IO, AwsRequestV1](options) { override protected def getAllEndpoints: List[ServerEndpoint[Any, IO]] = TestEndpoints.all[IO].toList diff --git a/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/LambdaHandler.scala b/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/LambdaHandler.scala index 1c4150834a..b7e792514c 100644 --- a/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/LambdaHandler.scala +++ b/serverless/aws/lambda-cats-effect/src/main/scala/sttp/tapir/serverless/aws/lambda/LambdaHandler.scala @@ -18,14 +18,15 @@ import java.nio.charset.StandardCharsets * @tparam R * AWS API Gateway request type [[AwsRequestV1]] or [[AwsRequest]]. At the moment mapping is required as there is no support for * generating API Gateway V2 definitions with AWS CDK v2. + * @param options + * Server options of type AwsServerOptions. */ -abstract class LambdaHandler[F[_]: Sync, R: Decoder] extends RequestStreamHandler { +abstract class LambdaHandler[F[_]: Sync, R: Decoder](options: AwsServerOptions[F]) extends RequestStreamHandler { protected def getAllEndpoints: List[ServerEndpoint[Any, F]] protected def process(input: InputStream, output: OutputStream): F[Unit] = { - val server: AwsCatsEffectServerInterpreter[F] = - AwsCatsEffectServerInterpreter(AwsCatsEffectServerOptions.noEncoding[F]) + val server: AwsCatsEffectServerInterpreter[F] = AwsCatsEffectServerInterpreter(options) for { allBytes <- Sync[F].blocking(input.readAllBytes()) diff --git a/serverless/aws/lambda-zio/src/main/scala/sttp/tapir/serverless/aws/ziolambda/ZioLambdaHandler.scala b/serverless/aws/lambda-zio/src/main/scala/sttp/tapir/serverless/aws/ziolambda/ZioLambdaHandler.scala index 37d5bc7efb..ea5b33bc1f 100644 --- a/serverless/aws/lambda-zio/src/main/scala/sttp/tapir/serverless/aws/ziolambda/ZioLambdaHandler.scala +++ b/serverless/aws/lambda-zio/src/main/scala/sttp/tapir/serverless/aws/ziolambda/ZioLambdaHandler.scala @@ -16,7 +16,7 @@ import java.nio.charset.StandardCharsets * * @tparam Env * The Environment type of the handler . - * @tparam options + * @param options * Server options of type AwsServerOptions. */ abstract class ZioLambdaHandler[Env: RIOMonadError](options: AwsServerOptions[RIO[Env, *]]) {