From 293793f08186bc1476f0ee62212b25293fd10a9e Mon Sep 17 00:00:00 2001 From: Dimitrije Bulaja Date: Thu, 15 Dec 2022 10:42:41 +0100 Subject: [PATCH] Refactor HttpElasticExecutor --- .../zio/elasticsearch/HttpExecutorSpec.scala | 6 +++--- .../zio/elasticsearch/ElasticConfig.scala | 7 +++++-- .../elasticsearch/HttpElasticExecutor.scala | 19 ++++++++----------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala index 970b52ab9..63b2ad855 100644 --- a/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala +++ b/modules/library/src/it/scala/zio/elasticsearch/HttpExecutorSpec.scala @@ -3,7 +3,7 @@ package zio.elasticsearch import sttp.client3.httpclient.zio.HttpClientZioBackend import sttp.client3.{SttpBackend, UriContext, basicRequest} import sttp.model.StatusCode.{NotFound, Ok} -import zio.elasticsearch.ElasticConfig.Uri +import zio.elasticsearch.ElasticConfig.Default import zio.{Task, ZIO} import zio.elasticsearch.ElasticError.DocumentRetrievingError.{DecoderError, DocumentNotFound} import zio.test.Assertion.{equalTo, isFalse, isLeft, isRight, isTrue, isUnit} @@ -140,7 +140,7 @@ object HttpExecutorSpec extends IntegrationSpec { _ <- ElasticRequest.createIndex(name, None).execute sttp <- ZIO.service[SttpBackend[Task, Any]] indexExists <- basicRequest - .head(uri"$Uri/$name") + .head(uri"${Default.uri}/$name") .send(sttp) .map(_.code.equals(Ok)) } yield indexExists @@ -156,7 +156,7 @@ object HttpExecutorSpec extends IntegrationSpec { _ <- ElasticRequest.deleteIndex(name).execute sttp <- ZIO.service[SttpBackend[Task, Any]] deleted <- basicRequest - .head(uri"$Uri/$name") + .head(uri"${Default.uri}/$name") .send(sttp) .map(_.code.equals(NotFound)) } yield deleted diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticConfig.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticConfig.scala index 6a9a2ef37..6cd61ec2a 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticConfig.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticConfig.scala @@ -1,8 +1,11 @@ package zio.elasticsearch -final case class ElasticConfig(host: String, port: Int) +import sttp.model.Uri + +final case class ElasticConfig(host: String, port: Int) { + lazy val uri: Uri = Uri(host, port) +} object ElasticConfig { lazy val Default: ElasticConfig = ElasticConfig("localhost", 9200) - lazy val Uri: String = "http://localhost:9200" } diff --git a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala index 42e42c448..06adeb072 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/HttpElasticExecutor.scala @@ -4,7 +4,6 @@ import sttp.client3.ziojson._ import sttp.client3.{Identity, RequestT, Response, ResponseException, SttpBackend, UriContext, basicRequest => request} import sttp.model.MediaType.ApplicationJson import sttp.model.StatusCode.Ok -import sttp.model.Uri import zio.Task import zio.ZIO.logDebug import zio.elasticsearch.ElasticRequest._ @@ -14,8 +13,6 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC import HttpElasticExecutor._ - private val basePath = Uri(config.host, config.port) - override def execute[A](request: ElasticRequest[A]): Task[A] = request match { case r: Create => executeCreate(r) @@ -29,7 +26,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC } private def executeGetById(r: GetById): Task[Option[Document]] = { - val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequestWithCustomResponse[ElasticGetResponse]( request @@ -41,9 +38,9 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreate(r: Create): Task[Option[DocumentId]] = { val uri = r.id match { case Some(documentId) => - uri"$basePath/${r.index}/$Create/$documentId".withParam("routing", r.routing.map(Routing.unwrap)) + uri"${config.uri}/${r.index}/$Create/$documentId".withParam("routing", r.routing.map(Routing.unwrap)) case None => - uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap)) + uri"${config.uri}/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap)) } sendRequestWithCustomResponse[ElasticCreateResponse]( @@ -58,28 +55,28 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] = sendRequest( request - .put(uri"$basePath/${createIndex.name}") + .put(uri"${config.uri}/${createIndex.name}") .contentType(ApplicationJson) .body(createIndex.definition.getOrElse("")) ).unit private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = { - val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequest(request.put(uri).contentType(ApplicationJson).body(r.document.json)).unit } private def executeExists(r: Exists): Task[Boolean] = { - val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequest(request.head(uri)).map(_.code.equals(Ok)) } private def executeDeleteIndex(r: DeleteIndex): Task[Unit] = - sendRequest(request.delete(uri"$basePath/${r.name}")).unit + sendRequest(request.delete(uri"${config.uri}/${r.name}")).unit private def executeDeleteById(r: DeleteById): Task[Option[Unit]] = { - val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) + val uri = uri"${config.uri}/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap)) sendRequestWithCustomResponse( request