Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
val ae = IO.async[Unit] { cb =>
ec3.submit(new Runnable {
override def run(): Unit = {
println(Thread.currentThread().getName + " (async)")
cb(Left(new IllegalStateException()))
}
})
}
run("async error") {
run("async shift") {
a *> IO.shift(ec1) *> printThread
}
/*
Outputs:
-- async shift --
ec3-1-785992331 (async)
ec1-2-274064559
import tapir.docs.openapi._
import tapir.openapi.circe.yaml._
val yaml = List(petEndpoint).toOpenAPI("Our pets", "1.0").toYaml
implicit val runtime: DefaultRuntime = new DefaultRuntime {}
val serve = BlazeServerBuilder[Task]
.bindHttp(8080, "localhost")
.withHttpApp(Router("/" -> service).orNotFound)
.serve
.compile
.drain
runtime.unsafeRun(serve)
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") {