From b6bd2c47d9792ebc680e8e9b954ad4a2c747a972 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Sat, 11 May 2024 13:41:36 +0700 Subject: [PATCH] Better naming for client and introduce noop client --- modules/api/src/main/smithy/search.smithy | 7 +-- .../app/src/main/scala/service.search.scala | 2 +- .../src/main/scala/NoopSearchClient.scala | 30 ++++++++++++ ...layClient.scala => PlaySearchClient.scala} | 49 ++----------------- .../client/src/main/scala/SearchClient.scala | 45 +++++++++++++++++ modules/e2e/src/test/scala/CompatSuite.scala | 6 +-- 6 files changed, 87 insertions(+), 52 deletions(-) create mode 100644 modules/client/src/main/scala/NoopSearchClient.scala rename modules/client/src/main/scala/{PlayClient.scala => PlaySearchClient.scala} (65%) create mode 100644 modules/client/src/main/scala/SearchClient.scala diff --git a/modules/api/src/main/smithy/search.smithy b/modules/api/src/main/smithy/search.smithy index dcf3861f..accac4e3 100644 --- a/modules/api/src/main/smithy/search.smithy +++ b/modules/api/src/main/smithy/search.smithy @@ -135,12 +135,13 @@ structure RefreshInput { structure StoreInput { - @required - source: Source - @httpLabel @required id: String + + @required + source: Source + } structure StoreBulkForumInput { diff --git a/modules/app/src/main/scala/service.search.scala b/modules/app/src/main/scala/service.search.scala index c03d791f..45778036 100644 --- a/modules/app/src/main/scala/service.search.scala +++ b/modules/app/src/main/scala/service.search.scala @@ -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) diff --git a/modules/client/src/main/scala/NoopSearchClient.scala b/modules/client/src/main/scala/NoopSearchClient.scala new file mode 100644 index 00000000..24da0beb --- /dev/null +++ b/modules/client/src/main/scala/NoopSearchClient.scala @@ -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(()) diff --git a/modules/client/src/main/scala/PlayClient.scala b/modules/client/src/main/scala/PlaySearchClient.scala similarity index 65% rename from modules/client/src/main/scala/PlayClient.scala rename to modules/client/src/main/scala/PlaySearchClient.scala index f9119136..c658824e 100644 --- a/modules/client/src/main/scala/PlayClient.scala +++ b/modules/client/src/main/scala/PlaySearchClient.scala @@ -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 @@ -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}") @@ -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) diff --git a/modules/client/src/main/scala/SearchClient.scala b/modules/client/src/main/scala/SearchClient.scala new file mode 100644 index 00000000..5f8f0bf8 --- /dev/null +++ b/modules/client/src/main/scala/SearchClient.scala @@ -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) diff --git a/modules/e2e/src/test/scala/CompatSuite.scala b/modules/e2e/src/test/scala/CompatSuite.scala index d1716695..ac78f623 100644 --- a/modules/e2e/src/test/scala/CompatSuite.scala +++ b/modules/e2e/src/test/scala/CompatSuite.scala @@ -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 @@ -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")