Skip to content

Commit

Permalink
Update zio-http to v3.0.0-RC6 (#3661)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvitaly committed Apr 7, 2024
1 parent 3a63a23 commit e63a4c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1539,7 +1539,10 @@ lazy val zioHttpServer: ProjectMatrix = (projectMatrix in file("server/zio-http-
.settings(commonJvmSettings)
.settings(
name := "tapir-zio-http-server",
libraryDependencies ++= Seq("dev.zio" %% "zio-interop-cats" % Versions.zioInteropCats % Test, "dev.zio" %% "zio-http" % "3.0.0-RC4")
libraryDependencies ++= Seq(
"dev.zio" %% "zio-interop-cats" % Versions.zioInteropCats % Test,
"dev.zio" %% "zio-http" % Versions.zioHttp
)
)
.jvmPlatform(scalaVersions = scala2And3Versions)
.dependsOn(serverCore, zio, serverTests % Test)
Expand Down
1 change: 1 addition & 0 deletions project/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ object Versions {
val iron = "2.5.0"
val enumeratum = "1.7.3"
val zio = "2.0.21"
val zioHttp = "3.0.0-RC6"
val zioInteropCats = "23.0.0.8"
val zioInteropReactiveStreams = "2.0.2"
val zioJson = "0.6.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sttp.tapir.server.ziohttp

import sttp.tapir.server.interpreter.BodyListener
import zio.{RIO, ZIO}
import zio.{Cause, RIO, ZIO}
import zio.stream.ZStream

import scala.util.{Failure, Success, Try}
Expand All @@ -11,19 +11,18 @@ private[ziohttp] class ZioHttpBodyListener[R] extends BodyListener[RIO[R, *], Zi
ZIO
.environmentWithZIO[R]
.apply { r =>
def succeed = cb(Success(())).provideEnvironment(r)
def failed(cause: Cause[Throwable]) = cb(Failure(cause.squash)).orDie.provideEnvironment(r)

body match {
case Right(ZioStreamHttpResponseBody(stream, contentLength)) =>
ZIO.right(
ZioStreamHttpResponseBody(
stream.onError(cause => cb(Failure(cause.squash)).orDie.provideEnvironment(r)) ++ ZStream
.fromZIO(cb(Success(())))
.provideEnvironment(r)
.drain,
stream.onError(failed) ++ ZStream.fromZIO(succeed).drain,
contentLength
)
)
case raw @ Right(_: ZioRawHttpResponseBody) => cb(Success(())).provideEnvironment(r).map(_ => raw)
case ws @ Left(_) => cb(Success(())).provideEnvironment(r).map(_ => ws)
case rawOrWs => succeed.as(rawOrWs)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ trait ZioHttpInterpreter[R] {
resp: ServerResponse[ZioResponseBody],
body: Option[ZioHttpResponseBody]
): UIO[Response] = {
val baseHeaders = resp.headers.groupBy(_.name).flatMap(sttpToZioHttpHeader).toList
val baseHeaders = resp.headers.groupBy(_.name).map(sttpToZioHttpHeader).toList
val allHeaders = body.flatMap(_.contentLength) match {
case Some(contentLength) if resp.contentLength.isEmpty => ZioHttpHeader.ContentLength(contentLength) :: baseHeaders
case _ => baseHeaders
Expand All @@ -97,20 +97,21 @@ trait ZioHttpInterpreter[R] {

ZIO.succeed(
Response(
status = Status.fromInt(statusCode).getOrElse(Status.Custom(statusCode)),
status = Status.fromInt(statusCode),
headers = ZioHttpHeaders(allHeaders),
body = body
.map {
case ZioStreamHttpResponseBody(stream, _) => Body.fromStream(stream)
case ZioRawHttpResponseBody(chunk, _) => Body.fromChunk(chunk)
case ZioStreamHttpResponseBody(stream, Some(contentLength)) => Body.fromStream(stream, contentLength)
case ZioStreamHttpResponseBody(stream, None) => Body.fromStreamChunked(stream)
case ZioRawHttpResponseBody(chunk, _) => Body.fromChunk(chunk)
}
.getOrElse(Body.empty)
)
)
}

private def sttpToZioHttpHeader(hl: (String, Seq[SttpHeader])): List[ZioHttpHeader] =
List(ZioHttpHeader.Custom(hl._1, hl._2.map(_.value).mkString(", ")))
private def sttpToZioHttpHeader(hl: (String, Seq[SttpHeader])): ZioHttpHeader =
ZioHttpHeader.Custom(hl._1, hl._2.map(_.value).mkString(", "))
}

object ZioHttpInterpreter {
Expand Down

0 comments on commit e63a4c6

Please sign in to comment.