From 33fd63d6b7b8d83009e018c381ba687888f83141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 3 Nov 2022 18:36:25 +0100 Subject: [PATCH] Build http4s routers lazily --- .../http4s/SimpleProtocolBuilder.scala | 25 ++++++++------ .../smithy4s/http4s/ProtocolBuilderSpec.scala | 34 +++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 modules/http4s/test/src/smithy4s/http4s/ProtocolBuilderSpec.scala diff --git a/modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala b/modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala index 13a65b95c..38f5a1e7f 100644 --- a/modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala +++ b/modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala @@ -103,7 +103,9 @@ abstract class SimpleProtocolBuilder[P](val codecs: CodecAPI)(implicit def use: Either[UnsupportedProtocolError, Monadic[Alg, F]] = { checkProtocol(service, protocolTag) - .as( + // Making sure the router is evaluated lazily, so that all the compilation inside it + // doesn't happen in case of a missing protocol + .map { _ => new SmithyHttp4sReverseRouter[Alg, Op, F]( uri, service, @@ -111,7 +113,7 @@ abstract class SimpleProtocolBuilder[P](val codecs: CodecAPI)(implicit EntityCompiler .fromCodecAPI[F](codecs) ) - ) + } .map(service.transform[GenLift[F]#λ](_)) } } @@ -140,14 +142,17 @@ abstract class SimpleProtocolBuilder[P](val codecs: CodecAPI)(implicit new RouterBuilder(service, impl, fe) def make: Either[UnsupportedProtocolError, HttpRoutes[F]] = - checkProtocol(service, protocolTag).as { - new SmithyHttp4sRouter[Alg, Op, F]( - service, - impl, - errorTransformation, - entityCompiler - ).routes - } + checkProtocol(service, protocolTag) + // Making sure the router is evaluated lazily, so that all the compilation inside it + // doesn't happen in case of a missing protocol + .map { _ => + new SmithyHttp4sRouter[Alg, Op, F]( + service, + impl, + errorTransformation, + entityCompiler + ).routes + } def resource: Resource[F, HttpRoutes[F]] = make.leftWiden[Throwable].liftTo[Resource[F, *]] diff --git a/modules/http4s/test/src/smithy4s/http4s/ProtocolBuilderSpec.scala b/modules/http4s/test/src/smithy4s/http4s/ProtocolBuilderSpec.scala new file mode 100644 index 000000000..ae30ef8d2 --- /dev/null +++ b/modules/http4s/test/src/smithy4s/http4s/ProtocolBuilderSpec.scala @@ -0,0 +1,34 @@ +package smithy4s.http4s + +import cats.effect.IO +import org.http4s.HttpApp +import org.http4s.client.Client +import smithy4s.example.PizzaAdminServiceGen +import smithy4s.example.WeatherGen +import weaver._ + +object ProtocolBuilderSpec extends FunSuite { + + private val fakeClient = Client.fromHttpApp(HttpApp.notFound[IO]) + + test( + "SimpleProtocolBuilder (client) fails when the protocol is not present" + ) { + val result = SimpleRestJsonBuilder(WeatherGen) + .client(fakeClient) + .use + + assert(result.isLeft) + } + + test( + "SimpleProtocolBuilder (client) succeeds when the protocol is present" + ) { + val result = SimpleRestJsonBuilder(PizzaAdminServiceGen) + .client(fakeClient) + .use + + assert(result.isRight) + } + +}