Skip to content

Commit

Permalink
Refactor logging in HttpElasticExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
dbulaja98 committed Dec 8, 2022
1 parent 7569425 commit a7f7bf0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
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
Expand Up @@ -31,7 +31,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))

sendClientWithResponse[ElasticGetResponse](
sendRequestWithCustomResponse[ElasticGetResponse](
request
.get(uri)
.response(asJson[ElasticGetResponse])
Expand All @@ -46,7 +46,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
uri"$basePath/${r.index}/$Doc".withParam("routing", r.routing.map(Routing.unwrap))
}

sendClientWithResponse[ElasticCreateResponse](
sendRequestWithCustomResponse[ElasticCreateResponse](
request
.post(uri)
.contentType(ApplicationJson)
Expand All @@ -56,7 +56,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
}

private def executeCreateIndex(createIndex: CreateIndex): Task[Unit] =
sendClientWithoutResponse(
sendRequest(
request
.put(uri"$basePath/${createIndex.name}")
.contentType(ApplicationJson)
Expand All @@ -66,29 +66,29 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
private def executeCreateOrUpdate(r: CreateOrUpdate): Task[Unit] = {
val uri = uri"$basePath/${r.index}/$Doc/${r.id}".withParam("routing", r.routing.map(Routing.unwrap))

sendClientWithoutResponse(request.put(uri).contentType(ApplicationJson).body(r.document.json)).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))

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

private def executeDeleteIndex(r: DeleteIndex): Task[Unit] =
sendClientWithoutResponse(request.delete(uri"$basePath/${r.name}")).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))

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

private def sendClientWithResponse[A](
private def sendRequestWithCustomResponse[A](
req: RequestT[Identity, Either[ResponseException[String, String], A], Any]
): ZIO[Any, Throwable, Response[Either[ResponseException[String, String], A]]] =
for {
Expand All @@ -97,7 +97,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
_ <- logDebug(s"RESPONSE LOG: ${resp.show(includeBody = true, includeHeaders = true, sensitiveHeaders = Set())}")
} yield resp

private def sendClientWithoutResponse(
private def sendRequest(
req: RequestT[Identity, Either[String, String], Any]
): ZIO[Any, Throwable, Response[Either[String, String]]] =
for {
Expand Down

0 comments on commit a7f7bf0

Please sign in to comment.