Skip to content

Instantly share code, notes, and snippets.

@peterstorm
Last active January 3, 2021 22:20
Show Gist options
  • Save peterstorm/bea250fcf0d8e6b96c2a57fae7f68d1a to your computer and use it in GitHub Desktop.
Save peterstorm/bea250fcf0d8e6b96c2a57fae7f68d1a to your computer and use it in GitHub Desktop.
package finagleprime.endpoints
import org.http4s._
import org.http4s.dsl._
import cats.implicits._
import cats.effect.Sync
import cats.data.EitherT
final class Test[F[_]: Sync] extends Http4sDsl[F] {
val endpoint: HttpRoutes[F] = HttpRoutes.of[F] {
case GET -> Root / "test" / test =>
encodeEither[String](testRun(test))
}
def parse(s: String): Either[Throwable, String] =
if (s.equals("test")) Either.right(s)
else Either.left(new NumberFormatException(s"${s} is not a valid integer."))
def testRun(a: String): EitherT[F, Throwable, String] =
for {
a <- EitherT(Sync[F].pure(parse(a)))
} yield a
def encodeEither[A](either: EitherT[F, Throwable, A])(implicit encoder: EntityEncoder[F, A]): F[Response[F]] = {
either.foldF(
err => BadRequest(err.toString()),
value => Ok(value)
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment