From 93363846e38262376cd3045b7f6aa0961fb90fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Thu, 25 Jan 2024 01:58:47 +0100 Subject: [PATCH] Add demo --- build.sbt | 1 + core/src/test/scala/respectfully/Demo.scala | 66 +++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 core/src/test/scala/respectfully/Demo.scala diff --git a/build.sbt b/build.sbt index 805671e..e3d7225 100644 --- a/build.sbt +++ b/build.sbt @@ -32,6 +32,7 @@ val commonSettings = Seq( scalacOptions ++= Seq( "-Wunused:all" ), + Test / fork := true, ) lazy val core = project diff --git a/core/src/test/scala/respectfully/Demo.scala b/core/src/test/scala/respectfully/Demo.scala new file mode 100644 index 0000000..6643e03 --- /dev/null +++ b/core/src/test/scala/respectfully/Demo.scala @@ -0,0 +1,66 @@ +package respectfully + +import cats.effect.IO +import cats.effect.IOApp +import io.circe.Codec +import io.circe.Decoder +import io.circe.Encoder +import org.http4s.HttpApp +import org.http4s.Response +import org.http4s.Uri +import org.http4s.circe.CirceEntityCodec._ +import org.http4s.client.Client +import org.http4s.ember.client.EmberClientBuilder +import org.http4s.ember.server.EmberServerBuilder + +@annotation.experimental +object Demo extends IOApp.Simple { + + case class User(id: Int, name: String, age: Int) derives Codec.AsObject + + trait Api { + def getUsers(): IO[List[User]] + def getUser(id: Int): IO[User] + def createUser(user: User): IO[User] + def updateUser(user: User): IO[User] + def deleteUser(id: Int): IO[Unit] + } + + given API[Api] = API.derived + + val impl: Api = + new Api { + def getUsers(): IO[List[User]] = IO(List(User(1, "John", 20))) + def getUser(id: Int): IO[User] = IO(User(1, "John", 20)) + def createUser(user: User): IO[User] = IO(user) + def updateUser(user: User): IO[User] = IO(user) + def deleteUser(id: Int): IO[Unit] = IO.unit + } + + def router(impl: Api): HttpApp[IO] = API[Api].toRoutes(impl) + + def client(c: Client[IO], base: Uri): Api = API[Api].toClient(c, base) + + import com.comcast.ip4s._ + + def run: IO[Unit] = EmberServerBuilder + .default[IO] + .withHttpApp(router(impl)) + .withHost(host"0.0.0.0") + .withPort(port"8080") + .withErrorHandler { case e => IO.consoleForIO.printStackTrace(e) *> IO.raiseError(e) } + .build + .use { server => + EmberClientBuilder.default[IO].build.use { c => + val apiClient = client(c, server.baseUri) + + apiClient + .getUser(42) + .flatMap(IO.println(_)) *> + apiClient + .getUsers() + .flatMap(IO.println(_)) + } + } + +}