Skip to content

Commit

Permalink
feat(mercury): Replace zhttp with zio-http (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed Nov 18, 2022
1 parent fcaaeda commit be6afb3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package io.iohk.atala

import zio._

import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zhttp.http.{Method, Headers}

import io.iohk.atala.mercury._
import io.iohk.atala.mercury.{given}
import io.circe.Printer
Expand All @@ -20,7 +17,7 @@ import io.circe._, io.circe.parser._
@main def AgentClientAlice() = {

val app = AgentPrograms.pickupMessageProgram
.provide(AgentService.alice, HttpClientZhttp.layer)
.provide(AgentService.alice, ZioHttpClient.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.iohk.atala

import zio._
import zio.http.service._
import io.iohk.atala.mercury._
import io.iohk.atala.mercury.model.UnpackMessage
import io.iohk.atala.mercury.protocol.mailbox.Mailbox.ReadMessage
import org.didcommx.didcomm.message.Attachment.Data.Json
import zhttp.service._

@main def AgentClientBob() = {

val app = AgentPrograms.senderProgram
.provide(AgentService.bob, HttpClientZhttp.layer)
.provide(AgentService.bob, ZioHttpClient.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.iohk.atala

import zio._
import zhttp.service.{ChannelFactory, Client, EventLoopGroup}
import zhttp.http.{Headers, Method}
import io.iohk.atala.mercury._
import io.iohk.atala.mercury.model.UnpackMessage
import io.iohk.atala.mercury.protocol.mailbox.Mailbox.ReadMessage
Expand All @@ -13,21 +11,21 @@ import org.didcommx.didcomm.message.Attachment.Data.Json
val app =
InvitationPrograms
.getInvitationProgram("http://localhost:8000/oob_url")
.provide(HttpClientZhttp.layer)
.provide(ZioHttpClient.layer)

Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

}

// val env = zio.http.Client.default ++ zio.Scope.default

// @main def AgentClientCoordinateMediationWithRootsId() = {
// val env = ChannelFactory.auto ++ EventLoopGroup.auto()
// val mediatorURL = "http://localhost:8000"
// val app = CoordinateMediationPrograms
// .senderMediationRequestProgram(mediatorURL)
// .provide(AgentService.charlie, HttpClientZhttp.layer)

// Unsafe.unsafe { implicit u => Runtime.default.unsafe.run(app).getOrThrowFiberFailure() }

// }

// @main def AgentClientCoordinateMediation() = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package io.iohk.atala.mercury
import scala.jdk.CollectionConverters.*

import zio._
import zio.http._
import zio.http.model._
import zio.http.service._
import java.io.IOException
import io.iohk.atala.resolvers.PeerDidMediatorSecretResolver
import zhttp.service.ChannelFactory
import zhttp.service.EventLoopGroup
import org.didcommx.didcomm.DIDComm
import io.iohk.atala.resolvers.UniversalDidResolver
import zhttp.service._
import zhttp.http._
import io.iohk.atala.QRcode
import io.iohk.atala.mercury.model.{_, given}
import io.iohk.atala.mercury.model.error._
import io.iohk.atala.mercury.protocol.outofbandlogin._
import io.iohk.atala.mercury.protocol.issuecredential._
import io.iohk.atala.mercury.protocol.presentproof._
import io.iohk.atala.resolvers.PeerDidMediatorSecretResolver
import io.iohk.atala.resolvers.UniversalDidResolver

/** AgentCli
* {{{
Expand Down Expand Up @@ -55,7 +54,7 @@ object AgentCli extends ZIOAppDefault {
|DID Comm V2 Agent - CLI tool for debugging - Build by Atala (IOHK)
|""".stripMargin)

val env = ChannelFactory.auto ++ EventLoopGroup.auto()
// val env = zio.http.Client.default ++ zio.Scope.default

def agentLayer(peer: PeerDID): ZLayer[Any, Nothing, AgentServiceAny] = ZLayer.succeed(
io.iohk.atala.mercury.AgentServiceAny(
Expand All @@ -73,7 +72,8 @@ object AgentCli extends ZIOAppDefault {
}
_ <- CoordinateMediationPrograms
.senderMediationRequestProgram(mediatorURL = url)
.provideSomeLayer(env)
// .provideSomeLayer(zio.http.Client.default)
// .provideSomeLayer(zio.Scope.default)
} yield ()
}

Expand Down Expand Up @@ -134,7 +134,8 @@ object AgentCli extends ZIOAppDefault {
content = Body.fromChunk(Chunk.fromArray(jsonString.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
)
.provideSomeLayer(env)
.provideSomeLayer(zio.http.Client.default)
.provideSomeLayer(zio.Scope.default)
data <- res.body.asString
_ <- Console.printLine(data)
} yield ()
Expand Down Expand Up @@ -243,7 +244,8 @@ object AgentCli extends ZIOAppDefault {
content = Body.fromChunk(Chunk.fromArray(jsonString.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
)
.provideSomeLayer(env)
.provideSomeLayer(zio.http.Client.default)
.provideSomeLayer(zio.Scope.default)
.catchNonFatalOrDie { ex => ZIO.fail(SendMessage(ex)) }
data <- res.body.asString
.catchNonFatalOrDie { ex => ZIO.fail(ParseResponse(ex)) }
Expand All @@ -256,7 +258,8 @@ object AgentCli extends ZIOAppDefault {
Http
.collectZIO[Request] {
case req @ Method.POST -> !!
if req.headersAsList.exists(h => h._1.equalsIgnoreCase(header._1) && h._2.equalsIgnoreCase(header._2)) =>
if req.headersAsList
.exists(h => h._1.toString.equalsIgnoreCase(header._1) && h._2.toString.equalsIgnoreCase(header._2)) =>
req.body.asString
.catchNonFatalOrDie(ex => ZIO.fail(ParseResponse(ex)))
.flatMap { data =>
Expand Down Expand Up @@ -292,7 +295,7 @@ object AgentCli extends ZIOAppDefault {
case "" => ZIO.succeed(defualtPort)
case str => ZIO.succeed(str.toIntOption.getOrElse(defualtPort))
}
_ <- Server.start(port, webServer(port)).debug.fork
_ <- Server.serve(webServer(port)).debug.fork
_ <- Console.printLine("Endpoint Started")
} yield ()

Expand Down Expand Up @@ -320,14 +323,14 @@ object AgentCli extends ZIOAppDefault {
} yield (peer)

didCommLayer = agentLayer(agentDID)
layers: ZLayer[Any, Nothing, AgentServiceAny & HttpClient] = didCommLayer ++ HttpClientZhttp.layer
layers: ZLayer[Any, Nothing, AgentServiceAny & HttpClient] = didCommLayer ++ ZioHttpClient.layer

_ <- options(
Seq(
"none" -> ZIO.unit,
"Show DID" -> Console.printLine(agentDID),
"Get DID Document" -> Console.printLine("DID Document:") *> Console.printLine(agentDID.getDIDDocument),
"Start WebServer endpoint" -> startEndpoint.provide(didCommLayer),
"Start WebServer endpoint" -> startEndpoint.provide(zio.http.Server.default, didCommLayer),
"Ask for Mediation Coordinate" -> askForMediation.provide(layers),
"Generate login invitation" -> generateLoginInvitation.provide(didCommLayer),
"Login with DID" -> loginInvitation.provide(didCommLayer),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package io.iohk.atala.mercury

import zio._
import zhttp.service._
import zhttp.http._
import zio.http._
import zio.http.model._
import zio.http.service._
import io.iohk.atala.mercury._

object HttpClientZhttp {
val layer = ZLayer.succeed(new HttpClientZhttp())
object ZioHttpClient {
val layer = ZLayer.succeed(new ZioHttpClient())
}

class HttpClientZhttp extends HttpClient {

val env = ChannelFactory.auto ++ EventLoopGroup.auto()
class ZioHttpClient extends HttpClient {

override def get(url: String): Task[HttpResponseBody] =
Client
zio.http.Client
.request(url)
.provideSomeLayer(env)
.provideSomeLayer(zio.http.Client.default)
.provideSomeLayer(zio.Scope.default)
.flatMap(_.body.asString)
.map(e => HttpResponseBody(e))

def postDIDComm(url: String, data: String): Task[HttpResponseBody] =
Client
zio.http.Client
.request(
url = url, // TODO make ERROR type
method = Method.POST,
Expand All @@ -30,7 +30,8 @@ class HttpClientZhttp extends HttpClient {
content = Body.fromChunk(Chunk.fromArray(data.getBytes)),
// ssl = ClientSSLOptions.DefaultSSL,
)
.provideSomeLayer(env)
.provideSomeLayer(zio.http.Client.default)
.provideSomeLayer(zio.Scope.default)
.flatMap(_.body.asString)
.map(e => HttpResponseBody(e))
}
4 changes: 1 addition & 3 deletions mercury/mercury-library/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ lazy val D = new {
val zioSLF4J = Def.setting("dev.zio" %% "zio-logging-slf4j" % V.zioLogging)
val zioJson = Def.setting("dev.zio" %% "zio-json" % V.zioJson)

// TODO waiting for https://github.com/zio/zio-http/pull/1774 to use zio-http
// val zioHttp = Def.setting("dev.zio" %% "zio-http" % "0.0.1") // FIXME USE THIS ONE
val zioHttp = Def.setting("io.d11" %% "zhttp" % V.zioHttp) // REMOVE (this is the old name)
val zioHttp = Def.setting("dev.zio" %% "zio-http" % "0.0.3")

val circeCore = Def.setting("io.circe" %% "circe-core" % V.circe)
val circeGeneric = Def.setting("io.circe" %% "circe-generic" % V.circe)
Expand Down

0 comments on commit be6afb3

Please sign in to comment.