Skip to content

Commit

Permalink
Derive clients
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 9, 2024
1 parent fca28f3 commit aaeddfc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 43 deletions.
50 changes: 30 additions & 20 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import org.typelevel.scalacoptions.ScalacOption
import org.typelevel.scalacoptions.ScalacOptions
import Dependencies.*

lazy val scala213 = "2.13.14"
lazy val scala3 = "3.4.1"
lazy val scala213 = "2.13.14"
lazy val scala3 = "3.4.1"
lazy val supportedScalaVersions = List(scala213, scala3)

inThisBuild(
Seq(
scalaVersion := scala213,
scalaVersion := scala213,
versionScheme := Some("early-semver"),
version := "3.0.0-SNAPSHOT",
run / fork := true,
version := "3.0.0-SNAPSHOT",
run / fork := true,
run / javaOptions += "-Dconfig.override_with_env_vars=true",
Compile / doc / sources := Seq.empty,
Compile / doc / sources := Seq.empty,
Compile / packageDoc / publishArtifact := false,
Compile / packageSrc / publishArtifact := false,
resolvers += "lila-maven" at "https://raw.githubusercontent.com/ornicar/lila-maven/master"
Expand All @@ -32,8 +32,8 @@ lazy val core = project
name := "lila-search-core",
libraryDependencies ++= Seq(
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % "8.11.5",
"org.typelevel" %% "cats-core" % "2.10.0",
"joda-time" % "joda-time" % "2.12.7",
"org.typelevel" %% "cats-core" % "2.10.0",
"joda-time" % "joda-time" % "2.12.7"
)
)

Expand All @@ -46,9 +46,9 @@ lazy val play = project
tpolecatExcludeOptions += ScalacOptions.fatalWarnings,
name := "lila-search",
libraryDependencies ++= Seq(
"com.github.ornicar" %% "scalalib" % "7.1.0",
"com.typesafe.play" %% "play-json" % "2.9.4",
"com.typesafe.play" %% "play-json-joda" % "2.9.4"
"com.github.ornicar" %% "scalalib" % "7.1.0",
"com.typesafe.play" %% "play-json" % "2.9.4",
"com.typesafe.play" %% "play-json-joda" % "2.9.4"
),
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
Expand All @@ -59,8 +59,8 @@ lazy val play = project
lazy val api = (project in file("modules/api"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
scalaVersion := scala3,
name := "lila-search-api",
scalaVersion := scala3,
name := "lila-search-api",
smithy4sWildcardArgument := "?",
libraryDependencies ++= Seq(
"com.disneystreaming.smithy4s" %% "smithy4s-core" % smithy4sVersion.value
Expand All @@ -71,11 +71,14 @@ lazy val client = (project in file("modules/client"))
.enablePlugins(Smithy4sCodegenPlugin)
.settings(
scalaVersion := scala3,
name := "lila-search-client",
name := "lila-search-client",
libraryDependencies ++= Seq(
"com.disneystreaming.smithy4s" %% "smithy4s-http4s" % smithy4sVersion.value
"com.disneystreaming.smithy4s" %% "smithy4s-http4s" % smithy4sVersion.value,
"com.typesafe.play" %% "play-ahc-ws-standalone" % "2.2.7",
http4sEmberClient
)
).dependsOn(api)
)
.dependsOn(api)

lazy val app = (project in file("modules/app"))
.enablePlugins(Smithy4sCodegenPlugin)
Expand All @@ -86,7 +89,7 @@ lazy val app = (project in file("modules/app"))
libraryDependencies ++= Seq(
"com.disneystreaming.smithy4s" %% "smithy4s-http4s" % smithy4sVersion.value,
"com.disneystreaming.smithy4s" %% "smithy4s-http4s-swagger" % smithy4sVersion.value,
"com.sksamuel.elastic4s" %% "elastic4s-effect-cats" % "8.11.5",
"com.sksamuel.elastic4s" %% "elastic4s-effect-cats" % "8.11.5",
catsCore,
catsEffect,
ducktape,
Expand All @@ -96,13 +99,20 @@ lazy val app = (project in file("modules/app"))
cirisHtt4s,
logbackX
),
excludeDependencies ++= Seq("org.typelevel" % "cats-core_2.13", "org.typelevel" % "cats-kernel_2.13", "com.sksamuel.elastic4s" % "elastic4s-core_2.13", "com.sksamuel.elastic4s" % "elastic4s-domain_2.13", "com.sksamuel.elastic4s" % "elastic4s-http_2.13", "com.fasterxml.jackson.module" % "jackson-module-scala_2.13"),
Compile / run / fork := true,
excludeDependencies ++= Seq(
"org.typelevel" % "cats-core_2.13",
"org.typelevel" % "cats-kernel_2.13",
"com.sksamuel.elastic4s" % "elastic4s-core_2.13",
"com.sksamuel.elastic4s" % "elastic4s-domain_2.13",
"com.sksamuel.elastic4s" % "elastic4s-http_2.13",
"com.fasterxml.jackson.module" % "jackson-module-scala_2.13"
),
Compile / run / fork := true
)
.enablePlugins(JavaAppPackaging)
.dependsOn(api, core)

lazy val root = project
.in(file("."))
.settings(publish := {}, publish / skip := true)
.aggregate(core, play, api, app)
.aggregate(core, play, api, app, client)
37 changes: 37 additions & 0 deletions modules/client/src/main/scala/Client.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package lila.search
package client

import smithy4s.http4s.*
import org.http4s.Uri
import org.http4s.client.Client
import cats.effect.{ IO, Resource }
import org.http4s.ember.client.EmberClientBuilder
import lila.search.spec.*
import scala.concurrent.Future
import cats.effect.unsafe.implicits.global

object Client:

def apply(uri: Uri): Resource[IO, FutureClient] =
instance(uri).map(FutureClient(_))

def instance(uri: Uri): Resource[IO, SearchService[IO]] =
makeClient.flatMap(makeIO(uri))

private def makeClient: Resource[IO, Client[IO]] =
EmberClientBuilder.default[IO].build

def makeIO(uri: Uri)(client: Client[IO]): Resource[IO, SearchService[IO]] =
SimpleRestJsonBuilder
.apply(SearchService)
.client(client)
.uri(uri)
.resource

class FutureClient(io: SearchService[IO]) extends SearchService[Future]:

override def countForum(text: String, troll: Boolean): Future[CountResponse] =
io.countForum(text, troll).unsafeToFuture()

override def searchForum(body: ForumInputBody, from: Int, size: Int): Future[SearchResponse] =
io.searchForum(body, from, size).unsafeToFuture()
19 changes: 0 additions & 19 deletions modules/client/src/main/scala/IOClient.scala

This file was deleted.

42 changes: 42 additions & 0 deletions modules/client/src/main/scala/PlayClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lila.search
package client

import akka.util.ByteString
import com.github.plokhotnyuk.jsoniter_scala.core._
import lila.search.spec.*
import play.api.libs.ws.BodyWritable
import play.api.libs.ws.InMemoryBody
import play.api.libs.ws.StandaloneWSClient
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import smithy4s.json.Json.given
import smithy4s.schema.Schema
import play.api.libs.ws.BodyReadable

object implicits:

given [A](using JsonCodec[A]): BodyWritable[A] =
BodyWritable(a => InMemoryBody(ByteString.fromArrayUnsafe(writeToArray(a))), "application/json")

given [A](using JsonCodec[A]): BodyReadable[A] =
BodyReadable(res => readFromArray(res.bodyAsBytes.toArray))

def apply(client: StandaloneWSClient, url: String)(using ExecutionContext): SearchService[Future] =
PlayClient(client, url)

class PlayClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionContext)
extends SearchService[Future]:

import implicits.given

override def countForum(text: String, troll: Boolean): Future[CountResponse] =
request(s"$baseUrl/count/forum", ForumInputBody(text, troll))

override def searchForum(body: ForumInputBody, from: Int, size: Int): Future[SearchResponse] =
request(s"$baseUrl/search/forum/{from}/{int}", body)

private def request[D: Schema, R: Schema](url: String, data: D): Future[R] =
client.url(url).post(data).flatMap {
case res if res.status == 200 => Future(res.body[R])
case res => Future.failed(Exception(s"$url ${res.status}"))
}
7 changes: 3 additions & 4 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ object Dependencies {
val catsCore = "org.typelevel" %% "cats-core" % "2.10.0"
val catsEffect = "org.typelevel" %% "cats-effect" % V.catsEffect

val fs2 = "co.fs2" %% "fs2-core" % V.fs2
val fs2IO = "co.fs2" %% "fs2-io" % V.fs2
val fs2 = "co.fs2" %% "fs2-core" % V.fs2
val fs2IO = "co.fs2" %% "fs2-io" % V.fs2

val cirisCore = "is.cir" %% "ciris" % V.ciris
val cirisHtt4s = "is.cir" %% "ciris-http4s" % V.ciris
Expand All @@ -28,11 +28,10 @@ object Dependencies {
val http4sEmberClient = http4s("ember-client")

val log4Cats = "org.typelevel" %% "log4cats-slf4j" % "2.7.0"
val logbackX = "ch.qos.logback" % "logback-classic" % "1.5.6"
val logbackX = "ch.qos.logback" % "logback-classic" % "1.5.6"

val ducktape = "io.github.arainko" %% "ducktape" % "0.2.0"


val testContainers = "com.dimafeng" %% "testcontainers-scala-postgresql" % "0.41.3" % Test
val weaver = "com.disneystreaming" %% "weaver-cats" % "0.8.4" % Test
val weaverScalaCheck = "com.disneystreaming" %% "weaver-scalacheck" % "0.8.4" % Test
Expand Down

0 comments on commit aaeddfc

Please sign in to comment.