Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(executor): Add logging to HttpElasticExecutor #16

Merged
merged 7 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package zio.elasticsearch

import zio.json.{DeriveJsonDecoder, JsonDecoder, jsonField}

final case class ElasticCreateResponse(
private[elasticsearch] final case class ElasticCreateResponse(
@jsonField("_id")
id: String
)

object ElasticCreateResponse {
implicit val decoder: JsonDecoder[ElasticCreateResponse] =
DeriveJsonDecoder.gen[ElasticCreateResponse]
private[elasticsearch] object ElasticCreateResponse {
implicit val decoder: JsonDecoder[ElasticCreateResponse] = DeriveJsonDecoder.gen[ElasticCreateResponse]
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package zio.elasticsearch

import sttp.client3.ziojson._
import sttp.client3.{SttpBackend, UriContext, basicRequest => request}
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._
import zio.{Task, ZIO}

private[elasticsearch] final class HttpElasticExecutor private (config: ElasticConfig, client: SttpBackend[Task, Any])
extends ElasticExecutor {
Expand All @@ -29,12 +30,12 @@ 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))
request
.get(uri)
.response(asJson[ElasticGetResponse])
.send(client)
.map(_.body.toOption)
.map(_.flatMap(d => if (d.found) Some(Document.from(d.source)) else None))

sendRequestWithCustomResponse[ElasticGetResponse](
request
.get(uri)
.response(asJson[ElasticGetResponse])
).map(_.body.toOption).map(_.flatMap(d => if (d.found) Some(Document.from(d.source)) else None))
}

private def executeCreate(r: Create): Task[Option[DocumentId]] = {
Expand All @@ -45,46 +46,65 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap))
}

request
.post(uri)
.contentType(ApplicationJson)
.response(asJson[ElasticCreateResponse])
.body(r.document.json)
.send(client)
.map(_.body.toOption)
.map(_.flatMap(body => DocumentId.make(body.id).toOption))
sendRequestWithCustomResponse[ElasticCreateResponse](
request
.post(uri)
.contentType(ApplicationJson)
.response(asJson[ElasticCreateResponse])
.body(r.document.json)
).map(_.body.toOption).map(_.flatMap(body => DocumentId.make(body.id).toOption))
}

private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] =
request
.put(uri"$basePath/${createIndex.name}")
.contentType(ApplicationJson)
.body(createIndex.definition.getOrElse(""))
.send(client)
.unit
sendRequest(
request
.put(uri"$basePath/${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))
request.put(uri).contentType(ApplicationJson).body(r.document.json).send(client).unit

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))
request.head(uri).send(client).map(_.code.equals(Ok))

sendRequest(request.head(uri)).map(_.code.equals(Ok))
}

private def executeDeleteIndex(r: DeleteIndex): Task[Unit] =
request.delete(uri"$basePath/${r.name}").send(client).unit
sendRequest(request.delete(uri"$basePath/${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))
request
.delete(uri)
.response(asJson[ElasticDeleteResponse])
.send(client)
.map(_.body.toOption)
.map(_.filter(_.result == "deleted").map(_ => ()))

sendRequestWithCustomResponse(
request
.delete(uri)
.response(asJson[ElasticDeleteResponse])
).map(_.body.toOption).map(_.filter(_.result == "deleted").map(_ => ()))
}

private def sendRequestWithCustomResponse[A](
req: RequestT[Identity, Either[ResponseException[String, String], A], Any]
): ZIO[Any, Throwable, Response[Either[ResponseException[String, String], A]]] =
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
for {
_ <- logDebug(s"[es-req]: ${req.show(includeBody = true, includeHeaders = true, sensitiveHeaders = Set())}")
resp <- req.send(client)
_ <- logDebug(s"[es-res]: ${resp.show(includeBody = true, includeHeaders = true, sensitiveHeaders = Set())}")
} yield resp

private def sendRequest(
req: RequestT[Identity, Either[String, String], Any]
): ZIO[Any, Throwable, Response[Either[String, String]]] =
dbulaja98 marked this conversation as resolved.
Show resolved Hide resolved
for {
_ <- logDebug(s"[es-req]: ${req.show(includeBody = true, includeHeaders = true, sensitiveHeaders = Set())}")
resp <- req.send(client)
_ <- logDebug(s"[es-res]: ${resp.show(includeBody = true, includeHeaders = true, sensitiveHeaders = Set())}")
} yield resp
}

private[elasticsearch] object HttpElasticExecutor {
Expand Down