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): Provide simple HTTP executor #7

Merged
merged 26 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
strategy:
fail-fast: false
matrix:
java: ['8', '11', '17']
java: ['11', '17']
scala: ['2.12.16', '2.13.8']
steps:
- name: Checkout current branch
Expand Down
14 changes: 11 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ lazy val library =
project
.in(file("modules/library"))
.settings(stdSettings("zio-elasticsearch"))
.settings(scalacOptions += "-language:higherKinds")
.settings(
libraryDependencies ++= List(
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1"
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3"
)
)

lazy val example =
project
.in(file("modules/example"))
.settings(stdSettings("example"))
.settings(
libraryDependencies ++= List(
"dev.zio" %% "zio" % "2.0.4"
)
)
.dependsOn(library)
.settings(
publish / skip := true
Expand Down
12 changes: 12 additions & 0 deletions modules/example/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'

services:
elasticsearch:
image: elasticsearch:7.17.6
container_name: zio-elasticsearch-example
ports:
- "9200:9200"
environment:
discovery.type: "single-node"
xpack.security.enabled: "false"
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
21 changes: 21 additions & 0 deletions modules/example/src/main/scala/example/ExampleApp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package example

import sttp.client3.httpclient.zio.HttpClientZioBackend
import zio._
import zio.elasticsearch._

object ExampleApp extends ZIOAppDefault {

override def run: Task[Unit] = {
val index = IndexName("examples")
val docId = DocumentId("test-document-1")
val routing = Some(Routing("10"))

(for {
_ <- Console.printLine("Welcome to an example app...")
_ <- Console.printLine(s"Looking for the document '$docId' in '$index' index...'")
res <- ElasticRequest.getById[ExampleDocument](index, docId, routing).execute
_ <- Console.printLine(res)
} yield ()).provide(ElasticExecutor.local, HttpClientZioBackend.layer())
}
}
9 changes: 9 additions & 0 deletions modules/example/src/main/scala/example/ExampleDocument.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package example

import zio.schema.{DeriveSchema, Schema}

final case class ExampleDocument(id: String, name: String, count: Int)

object ExampleDocument {
implicit val schema: Schema[ExampleDocument] = DeriveSchema.gen[ExampleDocument]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zio.elasticsearch

import zio.json.ast.Json
import zio.schema.Schema
import zio.schema.codec.JsonCodec.JsonDecoder
import zio.schema.codec.{DecodeError, JsonCodec}
Expand All @@ -12,4 +13,6 @@ private[elasticsearch] object Document {
def from[A](doc: A)(implicit schema: Schema[A]): Document = Document(
JsonCodec.jsonEncoder(schema).encodeJson(doc, indent = None).toString
)

def from(json: Json): Document = new Document(json.toString)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package zio.elasticsearch

final case class DocumentId(value: String) extends AnyVal
final case class DocumentId(value: String) extends AnyVal {
override def toString: String = value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package zio.elasticsearch

final case class ElasticConfig(host: String, port: Int)

object ElasticConfig {
lazy val Default: ElasticConfig = ElasticConfig("localhost", 9200)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package zio.elasticsearch

import sttp.client3.SttpBackend
import zio.{Task, ZIO, ZLayer}

trait ElasticExecutor {
def execute[A](request: ElasticRequest[A]): Task[A]
}

object ElasticExecutor {
lazy val live: ZLayer[ElasticConfig with SttpBackend[Task, Any], Throwable, ElasticExecutor] =
ZLayer {
for {
conf <- ZIO.service[ElasticConfig]
sttp <- ZIO.service[SttpBackend[Task, Any]]
} yield HttpElasticExecutor(conf, sttp)
}

lazy val local: ZLayer[SttpBackend[Task, Any], Throwable, ElasticExecutor] =
ZLayer.succeed(ElasticConfig.Default) >>> live

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package zio.elasticsearch
import zio.elasticsearch.ElasticError.DocumentRetrievingError._
import zio.elasticsearch.ElasticError._
import zio.schema.Schema
import zio.{RIO, ZIO}

sealed trait ElasticRequest[+A] { self =>
final def map[B](f: A => B): ElasticRequest[B] = ElasticRequest.Map(self, f)

final def execute: RIO[ElasticExecutor, A] =
ZIO.serviceWithZIO[ElasticExecutor](_.execute(self))
}

object ElasticRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package zio.elasticsearch

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

final case class ElasticResponse(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use ElasticDocumentResponse? Not sure if this response is the same for every ElasticRequest.

Also, we need to consider which meta fields we require.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, it makes sense to remove all unused fields. We need only found and source at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will probably need id as well but i can remove it for now.

@jsonField("_index")
index: String,
@jsonField("_type")
`type`: String,
@jsonField("_id")
id: String,
@jsonField("_version")
version: Int,
@jsonField("_seq_no")
seqNo: Int,
@jsonField("_primary_term")
primaryTerm: Int,
found: Boolean,
@jsonField("_source")
source: Json
)

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

import sttp.client3._
import sttp.client3.ziojson._
import sttp.model.Uri
import zio.elasticsearch.ElasticRequest._
import zio.{Task, ZIO}

private[elasticsearch] final class HttpElasticExecutor private (config: ElasticConfig, client: SttpBackend[Task, Any])
extends ElasticExecutor {

import HttpElasticExecutor._

private val uri = Uri(config.host, config.port)

override def execute[A](request: ElasticRequest[A]): Task[A] =
request match {
case r: GetById => executeGetById(r)
case _: Create => ZIO.attempt(())
case _: CreateOrUpdate => ZIO.attempt(())
case map @ Map(_, _) => execute(map.request).map(map.mapper)
}

private def executeGetById(getById: GetById): Task[Option[Document]] = {
val u =
uri.withWholePath(s"${getById.index}/$Doc/${getById.id}").withParam("routing", getById.routing.map(_.value))
basicRequest
.get(u)
.response(asJson[ElasticResponse])
.send(client)
.map(_.body.toOption)
.map(_.flatMap(d => if (d.found) Option(Document.from(d.source)) else None))
}

}

private[elasticsearch] object HttpElasticExecutor {

private final val Doc = "_doc"

def apply(config: ElasticConfig, client: SttpBackend[Task, Any]) =
new HttpElasticExecutor(config, client)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package zio.elasticsearch

final case class IndexName(name: String) extends AnyVal
final case class IndexName(name: String) extends AnyVal {
override def toString: String = name
}