Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
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")
}
}
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)
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 {
val e: Endpoint[Int, String, Pet, Nothing] = endpoint
.get
.in("pet" / path[Int]("petId"))
.errorOut(stringBody)
.out(jsonBody[Pet])
run("async 2") {
(printThread *> a *> printThread).on(ec1)
}
/*
Outputs:
-- async 2 --
ec1-1-716083600
ec3-1-1627960023 (async)
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") {
run("Eval on and back") {
printThread *> printThread.on(ec1) *> printThread
}
/*
Outputs:
-- Eval on and back --
main
ec1-1-716083600
def run(name: String)(th: IO[_, _]): Unit = {
println(s"-- $name --")
new DefaultRuntime {}.unsafeRun(th)
println()
}
run("Eval on") {
printThread.on(ec1)
}
run("async") {
printThread *> a *> printThread
}
/*
Outputs:
-- async --
main
ec3-1-785992331 (async)
val a = IO.async[Unit] { cb =>
ec3.submit(new Runnable {
override def run(): Unit = {
println(Thread.currentThread().getName + " (async)")
cb(Right(()))
}
})
}