Skip to content

Instantly share code, notes, and snippets.

@sammyrulez
Created April 18, 2019 20:52
Show Gist options
  • Save sammyrulez/2f7c3df4eafe7694fa7c5ba7198d1717 to your computer and use it in GitHub Desktop.
Save sammyrulez/2f7c3df4eafe7694fa7c5ba7198d1717 to your computer and use it in GitHub Desktop.
package com.github.sammyrulez
import tapir._
import tapir.json.circe._
import io.circe.generic.auto._
import scala.concurrent.{ ExecutionContext, Future }
import akka.http.scaladsl.server.Route
object CharRoutes {
import tapir.server.akkahttp._
val countCharactersServerEndpoint: Endpoint[String, Unit, Int, Nothing] =
endpoint.in(stringBody).out(plainBody[Int])
val countCharactersRoute: Route =
countCharactersServerEndpoint.toRoute(s => Future.successful(Right[Unit, Int](s.length)))
val baseEndpoint = endpoint.errorOut(jsonBody[ErrorInfo]).in("books")
case class User(name: String)
case class ErrorInfo(message: String)
type AuthToken = String
type Result = Int
def authFn(token: AuthToken): Future[Either[ErrorInfo, User]] = Future.successful(Right(new User(token)))
def logicFn(user: User, data: String, limit: Int): Future[Either[ErrorInfo, Result]] = Future.successful(Right(limit))
val myEndpoint: Endpoint[(AuthToken, String, Int), ErrorInfo, Result, Nothing] =
baseEndpoint.in(auth.bearer).in(stringBody).in(query[Int]("limit")).out(plainBody[Int])
val r: Route = myEndpoint.toRoute((authFn _).andThenFirstE(logicFn _))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment