Skip to content

Commit

Permalink
Better naming for client and introduce noop client
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 11, 2024
1 parent 4985a41 commit 58d53ad
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 53 deletions.
7 changes: 4 additions & 3 deletions modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,13 @@ structure RefreshInput {

structure StoreInput {

@required
source: Source

@httpLabel
@required
id: String

@required
source: Source

}

structure StoreBulkForumInput {
Expand Down
2 changes: 1 addition & 1 deletion modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends Search
error"Error in storeBulkForum: sources=$sources" *>
IO.raiseError(InternalServerError("Internal server error"))

override def store(source: Source, id: String): IO[Unit] =
override def store(id: String, source: Source): IO[Unit] =
val (index, src) = source.extract
esClient
.store(index, Id(id), src)
Expand Down
30 changes: 30 additions & 0 deletions modules/client/src/main/scala/NoopSearchClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package lila.search
package client

import lila.search.spec.*
import scala.concurrent.Future

object NoopSearchClient extends SearchClient:

override def refresh(index: Index): Future[Unit] = Future.successful(())

override def storeBulkTeam(sources: List[TeamSourceWithId]): Future[Unit] = Future.successful(())

override def deleteByIds(index: Index, ids: List[String]): Future[Unit] = Future.successful(())

override def storeBulkForum(sources: List[ForumSourceWithId]): Future[Unit] = Future.successful(())

override def deleteById(index: Index, id: String): Future[Unit] = Future.successful(())

override def search(query: Query, from: Int, size: Int): Future[SearchResponse] =
Future.successful(SearchResponse(Nil))

override def store(id: String, source: Source): Future[Unit] = Future.successful(())

override def storeBulkGame(sources: List[GameSourceWithId]): Future[Unit] = Future.successful(())

override def storeBulkStudy(sources: List[StudySourceWithId]): Future[Unit] = Future.successful(())

override def count(query: Query): Future[CountResponse] = Future.successful(CountResponse(0))

override def mapping(index: Index): Future[Unit] = Future.successful(())
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import scala.concurrent.Future
import smithy4s.json.Json.given
import smithy4s.schema.Schema
import play.api.libs.ws.BodyReadable
import scala.annotation.targetName

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

import implicits.given

Expand All @@ -25,70 +24,30 @@ class PlayClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionCon
.post(StoreBulkTeamInput(sources))
.map(_ => ())

@targetName("storeBulkTeamWithPair")
def storeBulkTeam(sources: List[(String, TeamSource)]): Future[Unit] =
storeBulkTeam(sources.map(TeamSourceWithId.apply.tupled))

override def storeBulkStudy(sources: List[StudySourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/study")
.post(StoreBulkStudyInput(sources))
.map(_ => ())

@targetName("storeBulkStudyWithPair")
def storeBulkStudy(sources: List[(String, StudySource)]): Future[Unit] =
storeBulkStudy(sources.map(StudySourceWithId.apply.tupled))

override def storeBulkGame(sources: List[GameSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/game")
.post(StoreBulkGameInput(sources))
.map(_ => ())

@targetName("storeBulkGameWithPair")
def storeBulkGame(sources: List[(String, GameSource)]): Future[Unit] =
storeBulkGame(sources.map(GameSourceWithId.apply.tupled))

override def storeBulkForum(sources: List[ForumSourceWithId]): Future[Unit] =
client
.url(s"$baseUrl/store-bulk/forum")
.post(StoreBulkForumInput(sources))
.map(_ => ())

@targetName("storeBulkForumWithPair")
def storeBulkForum(sources: List[(String, ForumSource)]): Future[Unit] =
storeBulkForum(sources.map(ForumSourceWithId.apply.tupled))

override def store(source: Source, id: String): Future[Unit] =
override def store(id: String, source: Source): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
.post(source)
.map(_ => ())

def storeForum(id: String, source: ForumSource): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
.post(Source.forum(source))
.map(_ => ())

def storeGame(id: String, source: GameSource): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
.post(Source.game(source))
.map(_ => ())

def storeStudy(id: String, source: StudySource): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
.post(Source.study(source))
.map(_ => ())

def storeTeam(id: String, source: TeamSource): Future[Unit] =
client
.url(s"$baseUrl/store/$id")
.post(Source.team(source))
.map(_ => ())

override def refresh(index: Index): Future[Unit] =
client
.url(s"$baseUrl/refresh/${index.name}")
Expand Down Expand Up @@ -144,4 +103,4 @@ object implicits:
BodyReadable(res => readFromArray(res.bodyAsBytes.toArray))

def apply(client: StandaloneWSClient, url: String)(using ExecutionContext): SearchService[Future] =
PlayClient(client, url)
PlaySearchClient(client, url)
45 changes: 45 additions & 0 deletions modules/client/src/main/scala/SearchClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lila.search
package client

import lila.search.spec.*
import play.api.libs.ws.StandaloneWSClient
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.annotation.targetName

trait SearchClient extends SearchService[Future] { client =>
@targetName("storeBulkTeamWithPair")
def storeBulkTeam(sources: List[(String, TeamSource)]): Future[Unit] =
client.storeBulkTeam(sources.map(TeamSourceWithId.apply.tupled))

@targetName("storeBulkStudyWithPair")
def storeBulkStudy(sources: List[(String, StudySource)]): Future[Unit] =
client.storeBulkStudy(sources.map(StudySourceWithId.apply.tupled))

@targetName("storeBulkGameWithPair")
def storeBulkGame(sources: List[(String, GameSource)]): Future[Unit] =
client.storeBulkGame(sources.map(GameSourceWithId.apply.tupled))

@targetName("storeBulkForumWithPair")
def storeBulkForum(sources: List[(String, ForumSource)]): Future[Unit] =
client.storeBulkForum(sources.map(ForumSourceWithId.apply.tupled))

def storeForum(id: String, source: ForumSource): Future[Unit] =
client.store(id, Source.forum(source))

def storeGame(id: String, source: GameSource): Future[Unit] =
client.store(id, Source.game(source))

def storeStudy(id: String, source: StudySource): Future[Unit] =
client.store(id, Source.study(source))

def storeTeam(id: String, source: TeamSource): Future[Unit] =
client.store(id, Source.team(source))
}

object SearchClient:

def noop: SearchClient = NoopSearchClient

def play(client: StandaloneWSClient, baseUrl: String)(using ec: ExecutionContext): SearchClient =
PlaySearchClient(client, baseUrl)
8 changes: 4 additions & 4 deletions modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.noop.NoOpLogger
import lila.search.app.AppResources
import lila.search.app.SearchApp
import lila.search.client.PlayClient
import lila.search.client.PlaySearchClient
import lila.search.app.{ AppConfig, ElasticConfig, HttpServerConfig }
import com.comcast.ip4s.*
import akka.actor.ActorSystem
Expand All @@ -22,14 +22,14 @@ object CompatSuite extends weaver.IOSuite:

given Logger[IO] = NoOpLogger[IO]

override type Res = PlayClient
override type Res = PlaySearchClient

override def sharedResource: Resource[IO, Res] =
val res = AppResources(fakeClient)
SearchApp(res, testAppConfig)
.run()
.flatMap(_ => wsClient)
.map(PlayClient(_, "http://localhost:9999"))
.map(PlaySearchClient(_, "http://localhost:9999"))

test("search endpoint"): client =>
val query = Query.Forum("foo")
Expand All @@ -53,7 +53,7 @@ object CompatSuite extends weaver.IOSuite:

test("store endpoint"): client =>
val source = Source.team(lila.search.spec.TeamSource("names", "desc", 100))
IO.fromFuture(IO(client.store(source, "id"))).map(expect.same(_, ()))
IO.fromFuture(IO(client.store("id", source))).map(expect.same(_, ()))

test("store bulk forum endpoint"): client =>
val sources = List(
Expand Down

0 comments on commit 58d53ad

Please sign in to comment.