-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fca28f3
commit aaeddfc
Showing
5 changed files
with
112 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters