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
| import sttp.tapir.json.circe._ | |
| import io.circe.generic.auto._ | |
| import sttp.tapir.server.PartialServerEndpoint | |
| val error: EndpointOutput[Error] = stringBody.and(statusCode).mapTo(Error) | |
| val secureEndpoint: PartialServerEndpoint[User, Unit, Error, Unit, Nothing, IO] = | |
| endpoint | |
| .in(auth.bearer[String].mapTo(AuthToken)) | |
| .in("api" / "1.0") | |
| .errorOut(error) |
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
| import java.util.UUID | |
| import cats.effect.{ContextShift, IO, Timer} | |
| import sttp.tapir._ | |
| import sttp.model.StatusCode | |
| case class AuthToken(token: String) | |
| case class Error(msg: String, statusCode: StatusCode) | |
| case class User(id: UUID, name: String) | |
| case class Pet(id: UUID, kind: String, name: String) |
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
| // the endpoints are now interpreted as an akka.http.scaladsl.Route | |
| import akka.http.scaladsl.server.Route | |
| import sttp.tapir.server.akkahttp._ | |
| val routes: Route = List(getPetWithLogic, addPetWithLogic).toRoute | |
| // expose routes using akka-http |
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
| import scala.concurrent.Future | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| // should fail with Error if user not found | |
| def authorize(authToken: AuthToken): Future[User] = ??? | |
| def findPetForUser(user: User, id: UUID): Future[Option[Pet]] = ??? | |
| def addPetToUser(user: User, pet: Pet): Future[Unit] = ??? | |
| val getPetWithLogic = getPet.serverLogicRecoverErrors { | |
| case (authToken, 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 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") |
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
| 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 |
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
| 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, |
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
| 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 { |
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
| 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()) { |
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
| import java.util.concurrent.CompletableFuture; | |
| class Example1 { | |
| private final Database database; | |
| Example1(Database database) { | |
| this.database = database; | |
| } | |
| CompletableFuture<Boolean> activateUser(Long userId) { |