Skip to content

Instantly share code, notes, and snippets.

View adamw's full-sized avatar

Adam Warski adamw

View GitHub Profile
case class User(id: UUID, name: String)
case class Pet(id: UUID, kind: String, name: String)
val getPet: Endpoint[(AuthToken, UUID), Error, Pet, Nothing] =
baseEndpoint
.get
.in(query[UUID]("id").description("The id of the pet to find"))
.out(jsonBody[Pet])
.description("Finds a pet by id")
import java.util.UUID
import sttp.tapir._
import sttp.tapir.json.circe._
import io.circe.generic.auto._
import sttp.model.StatusCode
case class AuthToken(token: String)
case class Error(msg: String, statusCode: StatusCode) extends Exception
import zio._
import zio.clock.Clock
import zio.duration._
class Example4(emailService: EmailService) {
// Schedule an email to each address, but send at most 10 in parallel
def welcomeUsers(emails: List[String]): RIO[Clock, Unit] =
ZIO.foreachParN_(10)(emails)(sendWelcomeEmail)
// Try to send a welcome email at most 5 times,
import kotlinx.coroutines.future.await
import java.util.concurrent.CompletableFuture
internal class Example3(private val database: Database) {
suspend fun activateUser(userId: Long?): Boolean {
val user = database.findUser(userId).await()
if (user != null && !user.isActive) {
database.activateUser(userId).await()
return true
} else {
class Example2 {
private final Database database;
Example2(Database database) {
this.database = database;
}
boolean activateUser(Long userId) {
User user = database.findUser(userId);
if (user != null && !user.isActive()) {
import java.util.concurrent.CompletableFuture;
class Example1 {
private final Database database;
Example1(Database database) {
this.database = database;
}
CompletableFuture<Boolean> activateUser(Long userId) {
package sttp.tapir.example
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.model.StatusCode
import sttp.tapir.docs.openapi._
import sttp.tapir.openapi.circe.yaml._
import sttp.tapir.json.circe._
object Example extends App {
object CirceAnnotationCodecs {
import io.circe.{Decoder, Encoder}
import io.circe.generic.JsonCodec
import sttp.tapir._
import sttp.tapir.json.circe._
@JsonCodec
case class Foo(x: String)
it should "add 1 point" in {
// given
val updatedPoints = new AtomicInteger(0)
val userId = UUID.randomUUID()
val stubDao = newStubDaoConstantPoints(15, updatedPoints)
// when
new Points(stubDao).increase(userId).transact(transactor).unsafeRunSync()
// then
override protected def beforeAll(): Unit = {
super.beforeAll()
transactor = Transactor(
(),
(_: Unit) => Resource.pure(null),
KleisliInterpreter[IO](
Blocker.liftExecutionContext(ExecutionContext.global))
.ConnectionInterpreter,
Strategy.void
)