This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val service: HttpRoutes[Task] = petEndpoint.toZioRoutes { petId => | |
if (petId == 35) { | |
UIO(Pet("Tapirus terrestris", "https://en.wikipedia.org/wiki/Tapir")) | |
} else { | |
IO.fail("Unknown pet id") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Pet(species: String, url: String) | |
import tapir._ | |
import io.circe.generic.auto._ | |
import tapir.json.circe._ | |
val petEndpoint: Endpoint[Int, String, Pet, Nothing] = | |
endpoint | |
.get | |
.in("pet" / path[Int]("petId")) | |
.errorOut(stringBody) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tapir.example | |
import org.http4s.{EntityBody, HttpRoutes} | |
import scalaz.zio.{IO, Task} | |
import tapir.Endpoint | |
import tapir.server.http4s.Http4sServerOptions | |
import scalaz.zio.interop.catz._ | |
import tapir.server.ServerEndpoint | |
package object zio { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val e: Endpoint[Int, String, Pet, Nothing] = endpoint | |
.get | |
.in("pet" / path[Int]("petId")) | |
.errorOut(stringBody) | |
.out(jsonBody[Pet]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run("async 2") { | |
(printThread *> a *> printThread).on(ec1) | |
} | |
/* | |
Outputs: | |
-- async 2 -- | |
ec1-1-716083600 | |
ec3-1-1627960023 (async) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val a = IO.effectAsync[Any, Nothing, Unit] { cb => | |
ec3.submit(new Runnable { | |
override def run(): Unit = { | |
println(Thread.currentThread().getName + " (async)") | |
cb(UIO.unit) | |
} | |
}) | |
} | |
run("async") { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run("Eval on and back") { | |
printThread *> printThread.on(ec1) *> printThread | |
} | |
/* | |
Outputs: | |
-- Eval on and back -- | |
main | |
ec1-1-716083600 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def run(name: String)(th: IO[_, _]): Unit = { | |
println(s"-- $name --") | |
new DefaultRuntime {}.unsafeRun(th) | |
println() | |
} | |
run("Eval on") { | |
printThread.on(ec1) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run("async") { | |
printThread *> a *> printThread | |
} | |
/* | |
Outputs: | |
-- async -- | |
main | |
ec3-1-785992331 (async) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val a = IO.async[Unit] { cb => | |
ec3.submit(new Runnable { | |
override def run(): Unit = { | |
println(Thread.currentThread().getName + " (async)") | |
cb(Right(())) | |
} | |
}) | |
} |