From ffd04ab6d128adfab56dcc566748bf8bbcfe5a1c Mon Sep 17 00:00:00 2001 From: Peter Kotula Date: Mon, 25 Dec 2023 12:06:23 +0100 Subject: [PATCH] zio-http updates --- .../zio/logging/api/http/ApiEndpoints.scala | 18 +++---- .../zio/logging/api/http/ApiHandlers.scala | 51 ++++++++++--------- .../example/ConfigurableLoggerApp.scala | 5 +- .../logging/api/http/ApiEndpointsSpec.scala | 24 +++++---- .../logging/api/http/ApiHandlersSpec.scala | 10 ++-- project/Versions.scala | 2 +- 6 files changed, 59 insertions(+), 51 deletions(-) diff --git a/examples/core/src/main/scala/zio/logging/api/http/ApiEndpoints.scala b/examples/core/src/main/scala/zio/logging/api/http/ApiEndpoints.scala index 4c1f2aa86..feb6f12ce 100644 --- a/examples/core/src/main/scala/zio/logging/api/http/ApiEndpoints.scala +++ b/examples/core/src/main/scala/zio/logging/api/http/ApiEndpoints.scala @@ -21,30 +21,29 @@ import zio.http.codec.PathCodec.{ literal, string } import zio.http.codec.{ Doc, HttpCodec, PathCodec } import zio.http.endpoint.EndpointMiddleware.None import zio.http.endpoint._ +import zio.logging.api.http.ApiDomain.Error object ApiEndpoints { import ApiDomain.logLevelSchema def rootPathCodec(rootPath: Seq[String]): PathCodec[Unit] = if (rootPath.isEmpty) { - HttpCodec.empty + PathCodec.empty } else { rootPath.map(literal).reduce(_ / _) } def getLoggerConfigs( rootPath: Seq[String] = Seq.empty - ): Endpoint[Unit, ApiDomain.Error.Internal, List[ApiDomain.LoggerConfig], None] = - Endpoint - .get(rootPathCodec(rootPath) / literal("logger")) + ): Endpoint[Unit, Unit, Error.Internal, List[ApiDomain.LoggerConfig], None] = + Endpoint(Method.GET / rootPathCodec(rootPath) / literal("logger")) .out[List[ApiDomain.LoggerConfig]] .outError[ApiDomain.Error.Internal](Status.InternalServerError) def getLoggerConfig( rootPath: Seq[String] = Seq.empty - ): Endpoint[String, ApiDomain.Error, ApiDomain.LoggerConfig, None] = - Endpoint - .get(rootPathCodec(rootPath) / literal("logger") / string("name")) + ): Endpoint[String, String, Error, ApiDomain.LoggerConfig, None] = + Endpoint(Method.GET / rootPathCodec(rootPath) / literal("logger") / string("name")) .out[ApiDomain.LoggerConfig] .outErrors[ApiDomain.Error]( HttpCodec.error[ApiDomain.Error.Internal](Status.InternalServerError), @@ -53,9 +52,8 @@ object ApiEndpoints { def setLoggerConfig( rootPath: Seq[String] = Seq.empty - ): Endpoint[(String, LogLevel), ApiDomain.Error.Internal, ApiDomain.LoggerConfig, None] = - Endpoint - .put(rootPathCodec(rootPath) / literal("logger") / string("name")) + ): Endpoint[String, (String, LogLevel), Error.Internal, ApiDomain.LoggerConfig, None] = + Endpoint(Method.PUT / rootPathCodec(rootPath) / literal("logger") / string("name")) .in[LogLevel] .out[ApiDomain.LoggerConfig] .outError[ApiDomain.Error.Internal](Status.InternalServerError) diff --git a/examples/core/src/main/scala/zio/logging/api/http/ApiHandlers.scala b/examples/core/src/main/scala/zio/logging/api/http/ApiHandlers.scala index e33524986..4784936bf 100644 --- a/examples/core/src/main/scala/zio/logging/api/http/ApiHandlers.scala +++ b/examples/core/src/main/scala/zio/logging/api/http/ApiHandlers.scala @@ -15,44 +15,49 @@ */ package zio.logging.api.http -import zio.ZIO -import zio.http.endpoint.EndpointMiddleware.None -import zio.http.endpoint.Routes +import zio.http.{ Handler, Route, Routes } import zio.logging.LoggerConfigurer import zio.logging.api.http.ApiDomain.Error +import zio.{ LogLevel, ZIO } object ApiHandlers { - def getLoggerConfigs(rootPath: Seq[String] = Seq.empty): Routes[LoggerConfigurer, Error.Internal, None] = + def getLoggerConfigs(rootPath: Seq[String] = Seq.empty): Route[LoggerConfigurer, Nothing] = ApiEndpoints .getLoggerConfigs(rootPath) - .implement(_ => - LoggerConfigurer - .getLoggerConfigs() - .map(_.map(ApiDomain.LoggerConfig.from)) - .mapError(_ => Error.Internal()) - ) + .implement { + Handler.fromFunctionZIO[Unit] { _ => + LoggerConfigurer + .getLoggerConfigs() + .map(_.map(ApiDomain.LoggerConfig.from)) + .mapError(_ => Error.Internal()) + } + } - def getLoggerConfig(rootPath: Seq[String] = Seq.empty): Routes[LoggerConfigurer, Error, None] = + def getLoggerConfig(rootPath: Seq[String] = Seq.empty): Route[LoggerConfigurer, Nothing] = ApiEndpoints .getLoggerConfig(rootPath) - .implement { name => - LoggerConfigurer.getLoggerConfig(name).mapError(_ => Error.Internal()).flatMap { - case Some(r) => ZIO.succeed(ApiDomain.LoggerConfig.from(r)) - case _ => ZIO.fail(Error.NotFound()) + .implement { + Handler.fromFunctionZIO[String] { name => + LoggerConfigurer.getLoggerConfig(name).mapError(_ => Error.Internal()).flatMap { + case Some(r) => ZIO.succeed(ApiDomain.LoggerConfig.from(r)) + case _ => ZIO.fail(Error.NotFound()) + } } } - def setLoggerConfigs(rootPath: Seq[String] = Seq.empty): Routes[LoggerConfigurer, Error.Internal, None] = + def setLoggerConfigs(rootPath: Seq[String] = Seq.empty): Route[LoggerConfigurer, Nothing] = ApiEndpoints .setLoggerConfig(rootPath) - .implement { case (name, logLevel) => - LoggerConfigurer - .setLoggerConfig(name, logLevel) - .map(ApiDomain.LoggerConfig.from) - .mapError(_ => Error.Internal()) + .implement { + Handler.fromFunctionZIO[(String, LogLevel)] { case (name, logLevel) => + LoggerConfigurer + .setLoggerConfig(name, logLevel) + .map(ApiDomain.LoggerConfig.from) + .mapError(_ => Error.Internal()) + } } - def routes(rootPath: Seq[String] = Seq.empty): Routes[LoggerConfigurer, ApiDomain.Error, None] = - getLoggerConfigs(rootPath) ++ getLoggerConfig(rootPath) ++ setLoggerConfigs(rootPath) + def routes(rootPath: Seq[String] = Seq.empty): Routes[LoggerConfigurer, Nothing] = + Routes(getLoggerConfigs(rootPath), getLoggerConfig(rootPath), setLoggerConfigs(rootPath)) } diff --git a/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala b/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala index d7fbd2cd7..708539b85 100644 --- a/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala +++ b/examples/core/src/main/scala/zio/logging/example/ConfigurableLoggerApp.scala @@ -15,7 +15,6 @@ */ package zio.logging.example -import zio.http.HttpAppMiddleware.basicAuth import zio.http._ import zio.logging.api.http.ApiHandlers import zio.logging.{ @@ -86,8 +85,8 @@ object ConfigurableLoggerApp extends ZIOAppDefault { _ <- ZIO.logDebug("Done") @@ LogAnnotation.TraceId(traceId) } yield () - val httpApp: Http[LoggerConfigurer, Response, Request, Response] = - ApiHandlers.routes("example" :: Nil).toApp[LoggerConfigurer] @@ basicAuth("admin", "admin") + val httpApp: HttpApp[LoggerConfigurer] = + ApiHandlers.routes("example" :: Nil).toHttpApp @@ Middleware.basicAuth("admin", "admin") override def run: ZIO[Scope, Any, ExitCode] = (for { diff --git a/examples/core/src/test/scala/zio/logging/api/http/ApiEndpointsSpec.scala b/examples/core/src/test/scala/zio/logging/api/http/ApiEndpointsSpec.scala index b4656c5db..8aa5c949a 100644 --- a/examples/core/src/test/scala/zio/logging/api/http/ApiEndpointsSpec.scala +++ b/examples/core/src/test/scala/zio/logging/api/http/ApiEndpointsSpec.scala @@ -10,9 +10,13 @@ object ApiEndpointsSpec extends ZIOSpecDefault { def spec: Spec[Environment with TestEnvironment with Scope, Any] = suite("ApiEndpointsSpec")( test("rootPathCodec") { def testRootPathCodec(rootPath: Seq[String], expected: PathCodec[Unit]) = - assertTrue(ApiEndpoints.rootPathCodec(rootPath).encodeRequest(()).url == expected.encodeRequest(()).url) + assertTrue( + ApiEndpoints.rootPathCodec(rootPath).encode(()).getOrElse(zio.http.Path.empty) == expected + .encode(()) + .getOrElse(zio.http.Path.empty) + ) - testRootPathCodec(Nil, HttpCodec.empty) && testRootPathCodec( + testRootPathCodec(Nil, PathCodec.empty) && testRootPathCodec( "example" :: Nil, literal("example") ) && testRootPathCodec("v1" :: "example" :: Nil, literal("v1") / literal("example")) @@ -21,7 +25,9 @@ object ApiEndpointsSpec extends ZIOSpecDefault { def testPath(rootPath: Seq[String], expected: PathCodec[Unit]) = assertTrue( - ApiEndpoints.getLoggerConfigs(rootPath).input.encodeRequest(()).url == expected.encodeRequest(()).url + ApiEndpoints.getLoggerConfigs(rootPath).input.encodeRequest(()).path == expected + .encode(()) + .getOrElse(zio.http.Path.empty) ) testPath(Nil, literal("logger")) && testPath( @@ -33,9 +39,9 @@ object ApiEndpointsSpec extends ZIOSpecDefault { def testPath(rootPath: Seq[String], expected: PathCodec[Unit]) = assertTrue( - ApiEndpoints.getLoggerConfig(rootPath).input.encodeRequest("my-logger").url == expected - .encodeRequest(()) - .url + ApiEndpoints.getLoggerConfig(rootPath).input.encodeRequest("my-logger").path == expected + .encode(()) + .getOrElse(zio.http.Path.empty) ) testPath(Nil, literal("logger") / literal("my-logger")) && testPath( @@ -51,9 +57,9 @@ object ApiEndpointsSpec extends ZIOSpecDefault { .setLoggerConfig(rootPath) .input .encodeRequest(("my-logger", LogLevel.Info)) - .url == expected - .encodeRequest(()) - .url + .path == expected + .encode(()) + .getOrElse(zio.http.Path.empty) ) testPath(Nil, literal("logger") / literal("my-logger")) && testPath( diff --git a/examples/core/src/test/scala/zio/logging/api/http/ApiHandlersSpec.scala b/examples/core/src/test/scala/zio/logging/api/http/ApiHandlersSpec.scala index 6a1d861be..275ed35bf 100644 --- a/examples/core/src/test/scala/zio/logging/api/http/ApiHandlersSpec.scala +++ b/examples/core/src/test/scala/zio/logging/api/http/ApiHandlersSpec.scala @@ -32,7 +32,7 @@ object ApiHandlersSpec extends ZIOSpecDefault { for { request <- ZIO.attempt(Request.get(URL.decode("/example/logger").toOption.get)) - response <- routes.toApp.runZIO(request) + response <- routes.toHttpApp.runZIO(request) content <- HttpCodec.content[List[ApiDomain.LoggerConfig]].decodeResponse(response) } yield assertTrue(response.status.isSuccess) && assertTrue( content == List(ApiDomain.LoggerConfig("root", LogLevel.Info)) @@ -42,7 +42,7 @@ object ApiHandlersSpec extends ZIOSpecDefault { val routes = ApiHandlers.routes("example" :: Nil) for { request <- ZIO.attempt(Request.get(URL.decode("/example/logger/example.Service").toOption.get)) - response <- routes.toApp.runZIO(request) + response <- routes.toHttpApp.runZIO(request) content <- HttpCodec.content[ApiDomain.LoggerConfig].decodeResponse(response) } yield assertTrue(response.status.isSuccess) && assertTrue( content == ApiDomain.LoggerConfig("example.Service", LogLevel.Info) @@ -55,11 +55,11 @@ object ApiHandlersSpec extends ZIOSpecDefault { request <- ZIO.attempt( Request .put( - HttpCodec.content[LogLevel].encodeRequest(LogLevel.Warning).body, - URL.decode("/example/logger/example.Service").toOption.get + URL.decode("/example/logger/example.Service").toOption.get, + HttpCodec.content[LogLevel].encodeRequest(LogLevel.Warning).body ) ) - response <- routes.toApp.runZIO(request) + response <- routes.toHttpApp.runZIO(request) content <- HttpCodec.content[ApiDomain.LoggerConfig].decodeResponse(response) } yield assertTrue(response.status.isSuccess) && assertTrue( content == ApiDomain.LoggerConfig("example.Service", LogLevel.Warning) diff --git a/project/Versions.scala b/project/Versions.scala index 22ad1df33..5e4689627 100644 --- a/project/Versions.scala +++ b/project/Versions.scala @@ -10,6 +10,6 @@ object Versions { val zioConfig = "4.0.0-RC16" val zioParser = "0.1.9" val zioPrelude = "1.0.0-RC19" - val zioHttp = "3.0.0-RC2" + val zioHttp = "3.0.0-RC4" val log4jVersion = "2.19.0" }