Skip to content

Commit

Permalink
Merge pull request #568 from disneystreaming/issue-501
Browse files Browse the repository at this point in the history
Build http4s routers lazily
  • Loading branch information
daddykotex authored Nov 3, 2022
2 parents 283685e + 33fd63d commit 98e64fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
25 changes: 15 additions & 10 deletions modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ 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,
client,
EntityCompiler
.fromCodecAPI[F](codecs)
)
)
}
.map(service.transform[GenLift[F]#λ](_))
}
}
Expand Down Expand Up @@ -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, *]]
Expand Down
34 changes: 34 additions & 0 deletions modules/http4s/test/src/smithy4s/http4s/ProtocolBuilderSpec.scala
Original file line number Diff line number Diff line change
@@ -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)
}

}

0 comments on commit 98e64fa

Please sign in to comment.