Skip to content

Commit

Permalink
Refactor HttpElasticExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 committed Dec 15, 2022
1 parent 204e015 commit 293793f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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](
Expand All @@ -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
Expand Down

0 comments on commit 293793f

Please sign in to comment.