Skip to content

Commit

Permalink
ZIO 2 - Release 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ex0ns authored Jul 21, 2022
1 parent 500d4f9 commit 98bea89
Show file tree
Hide file tree
Showing 32 changed files with 283 additions and 261 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
arguments: "-c .scalafmt.conf --test"
- uses: jodersky/[email protected]
with:
mill-version: 0.10.2
mill-version: 0.10.5
- name: Compile
run: mill '__.compile'
- name: Test
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish_snapshot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
uses: actions/checkout@v2
- uses: jodersky/[email protected]
with:
mill-version: 0.10.2
mill-version: 0.10.5
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
uses: actions/checkout@v2
- uses: jodersky/[email protected]
with:
mill-version: 0.10.2
mill-version: 0.10.5
- name: Import GPG key
id: import_gpg
uses: crazy-max/ghaction-import-gpg@v3
Expand Down
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.2
0.10.5
34 changes: 22 additions & 12 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ import publish._
import mill.scalalib._

object Versions {
val zioMagicVersion = "0.3.12"
val zioLoggingVersion = "0.5.14"
val sttpVersion = "3.6.2"
val zioVersion = "1.0.15"
val sttpVersion = "3.6.2"
val zioLoggingVersion = "2.0.1"
val zioVersion = "2.0.0"
val sttpVersion = "3.7.1"
val circeVersion = "0.14.2"
val pureConfigVersion = "0.17.1"
}

val scalaVersions = List("2.12.15", "2.13.8")
val scalaVersions = List("2.12.16", "2.13.8")

trait Publishable extends PublishModule {
override def artifactName = s"zmatrix"
override def publishVersion = "0.1.2"
override def publishVersion = "0.2.0"

override def pomSettings = PomSettings(
description = "Matrix.org API client written using ZIO",
Expand All @@ -39,14 +37,15 @@ class CoreModule(val crossScalaVersion: String) extends CrossScalaModule with Pu

override def ivyDeps = Agg(
ivy"dev.zio::zio:${zioVersion}",
ivy"io.github.kitlangton::zio-magic:${zioMagicVersion}",
ivy"dev.zio::zio-logging:${zioLoggingVersion}",
ivy"com.softwaremill.sttp.client3::core:${sttpVersion}",
ivy"com.softwaremill.sttp.client3::circe:${sttpVersion}",
ivy"com.softwaremill.sttp.client3::async-http-client-backend-zio1:${sttpVersion}",
ivy"com.softwaremill.sttp.client3::async-http-client-backend-zio:${sttpVersion}",
ivy"com.github.pureconfig::pureconfig:${pureConfigVersion}",
ivy"io.circe::circe-generic:${circeVersion}",
ivy"io.circe::circe-generic-extras:${circeVersion}"
ivy"io.circe::circe-generic-extras:${circeVersion}",
// https://github.com/com-lihaoyi/mill/issues/1797
ivy"org.scala-lang:scala-reflect:${crossScalaVersion}"
)

override def scalacOptions = Seq(
Expand All @@ -67,8 +66,8 @@ class CoreModule(val crossScalaVersion: String) extends CrossScalaModule with Pu
ivy"dev.zio::zio-test-sbt:${zioVersion}"
)

def testOne(args: String*) = T.command {
super.runMain("org.scalatest.run", args: _*)
def testOne(spec: String, args: String*) = T.command {
super.runMain(spec, args: _*)
}

def testFramework = "zio.test.sbt.ZTestFramework"
Expand All @@ -79,6 +78,17 @@ class CoreModule(val crossScalaVersion: String) extends CrossScalaModule with Pu
object examples extends Cross[ExamplesModule](scalaVersions: _*)
class ExamplesModule(val crossScalaVersion: String) extends CrossScalaModule {
val moduleDeps = Seq(core(crossScalaVersion))
override def scalacOptions = Seq(
"-unchecked",
"-deprecation",
"-language:_",
"-Ywarn-unused",
"-encoding",
"UTF-8",
"-feature",
"-unchecked",
"-Ywarn-dead-code"
)

def mainClass = Some("com.bot4s.zmatrix.Runner")
}
1 change: 1 addition & 0 deletions core/resources/bot.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
matrix {
api-prefix="/_matrix/client/r0"
home-server="https://matrix.org"
user-id="ziobot"
}
13 changes: 7 additions & 6 deletions core/src/com/bot4s/zmatrix/MatrixConfiguration.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.bot4s.zmatrix

import zio.{ Has, IO, Layer, Ref, UIO, URIO, ZIO }
import zio.{ IO, Layer, Ref, UIO, URIO, ZIO }
import pureconfig.ConfigSource
import pureconfig.generic.auto._
import pureconfig.error.ConfigReaderFailures
import zio.ZLayer

final case class Config(
matrix: MatrixConfigurationContent
Expand All @@ -29,19 +30,19 @@ object MatrixConfiguration {
val DEFAULT_API_PREFIX = "/_matrix/client/r0"
val DEFAULT_CONFIG_FILE = "bot.conf"

def get: URIO[Has[MatrixConfiguration], Config] = ZIO.accessM(_.get.get)
def get: URIO[MatrixConfiguration, Config] = ZIO.environmentWithZIO(_.get.get)

private def refFromFile(filename: String): IO[ConfigReaderFailures, Ref[Config]] =
ZIO.fromEither(ConfigSource.resources(filename).load[Config]) >>= Ref.make
ZIO.fromEither(ConfigSource.resources(filename).load[Config]).flatMap(e => Ref.make(e))

/**
* Create an in-memory configuration that is not persistent.
*/
def live(filename: String = DEFAULT_CONFIG_FILE): Layer[ConfigReaderFailures, Has[MatrixConfiguration]] =
refFromFile(filename).map { configRef =>
def live(filename: String = DEFAULT_CONFIG_FILE): Layer[ConfigReaderFailures, MatrixConfiguration] =
ZLayer.fromZIO(refFromFile(filename).map { configRef =>
new MatrixConfiguration {
override def get: UIO[Config] = configRef.get
}
}.toLayer
})

}
34 changes: 19 additions & 15 deletions core/src/com/bot4s/zmatrix/SyncTokenConfiguration.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.bot4s.zmatrix

import zio.{ Has, IO, Layer, Managed, Ref, UIO, URIO, ZIO }
import zio.{ IO, Layer, Ref, UIO, URIO, ZIO }
import pureconfig.{ ConfigSource, ConfigWriter }
import pureconfig.generic.auto._
import pureconfig.error.ConfigReaderFailures
import java.io.{ BufferedWriter, File, FileWriter }
import com.typesafe.config.ConfigRenderOptions
import zio.ZLayer

final case class SyncToken(
since: Option[String] = None
Expand All @@ -19,51 +20,54 @@ trait SyncTokenConfiguration {
object SyncTokenConfiguration {
val DEFAULT_TOKEN_FILE = "token.conf"

def get: URIO[Has[SyncTokenConfiguration], SyncToken] = ZIO.accessM(_.get.get)
def set(config: SyncToken): URIO[Has[SyncTokenConfiguration], Unit] = ZIO.accessM(_.get.set(config))
def get: URIO[SyncTokenConfiguration, SyncToken] = ZIO.environmentWithZIO(_.get.get)
def set(config: SyncToken): URIO[SyncTokenConfiguration, Unit] = ZIO.environmentWithZIO(_.get.set(config))

private def refFromFile(filename: String): IO[ConfigReaderFailures, Ref[SyncToken]] =
ZIO
.fromEither(ConfigSource.file(filename).load[SyncToken])
.orElse(ZIO.succeed(SyncToken(None))) >>= Ref.make
.orElse(ZIO.succeed(SyncToken(None)))
.flatMap(token => Ref.make(token))

/**
* Create an in-memory configuration that is not persistent.
* It's not recommended to use this layer as the token will not be persisted
* between runs
*/
def live(filename: String = DEFAULT_TOKEN_FILE): Layer[ConfigReaderFailures, Has[SyncTokenConfiguration]] =
refFromFile(filename).map { tokenRef =>
def live(filename: String = DEFAULT_TOKEN_FILE): Layer[ConfigReaderFailures, SyncTokenConfiguration] =
ZLayer.fromZIO(refFromFile(filename).map { tokenRef =>
new SyncTokenConfiguration {
def get: UIO[SyncToken] = tokenRef.get
def set(config: SyncToken): UIO[Unit] = tokenRef.set(config)
}
}.toLayer
})

/**
* Create a persistent (on disk storage) configuration from the given file.
* This layer, when updated, will write back its changes in the given configuration file
*/
def persistent(
filename: String = DEFAULT_TOKEN_FILE
): Layer[ConfigReaderFailures, Has[SyncTokenConfiguration]] =
refFromFile(filename).map { configRef =>
): Layer[ConfigReaderFailures, SyncTokenConfiguration] =
ZLayer.fromZIO(refFromFile(filename).map { configRef =>
new SyncTokenConfiguration {
override def get: UIO[SyncToken] = configRef.get
override def set(config: SyncToken): UIO[Unit] = {
val renderOptions = ConfigRenderOptions.concise().setFormatted(true).setJson(false)
val toWrite = ConfigWriter[SyncToken].to(config).render(renderOptions)

val updateConf = for {
file <- ZIO.effect(new File(filename))
managed = Managed.make(ZIO.effect(new BufferedWriter(new FileWriter(file))))(bw => IO.succeed(bw.close))
_ <- configRef.set(config)
_ <- managed.use(c => IO.effect(c.write(toWrite)))
file <- ZIO.attempt(new File(filename))
_ <- configRef.set(config)
_ <-
ZIO.acquireReleaseWith(ZIO.attempt(new BufferedWriter(new FileWriter(file))))(bw =>
ZIO.succeed(bw.close)
)(c => ZIO.attempt(c.write(toWrite)))
} yield ()

updateConf.catchAll(_ => IO.succeed(()))
updateConf.catchAll(_ => ZIO.succeed(()))
}
}
}.toLayer
})

}
4 changes: 2 additions & 2 deletions core/src/com/bot4s/zmatrix/WithAccess.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.bot4s.zmatrix

import zio.{ Has, URIO }
import zio.URIO
import sttp.client3.RequestT
import com.bot4s.zmatrix.services.Authentication

trait WithAccess {

def authenticate[U[_], T](request: RequestT[U, T, Any]): URIO[Has[Authentication], RequestT[U, T, Any]] =
def authenticate[U[_], T](request: RequestT[U, T, Any]): URIO[Authentication, RequestT[U, T, Any]] =
Authentication.accessToken.flatMap(_.authenticateM(request))

}
2 changes: 1 addition & 1 deletion core/src/com/bot4s/zmatrix/api/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.bot4s.zmatrix.{ AuthMatrixEnv, MatrixError }

trait Account {
def whoAmI: ZIO[AuthMatrixEnv, MatrixError, UserResponse] =
(get(Seq("account", "whoami")) >>= authenticate >>= send) >>= as[UserResponse]
sendWithAuth[UserResponse](get(Seq("account", "whoami")))
}

object accounts extends Account
2 changes: 1 addition & 1 deletion core/src/com/bot4s/zmatrix/api/DeviceManagement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait DeviceManagement {
* Documentation: https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-devices
*/
def getDevices(): ZIO[AuthMatrixEnv, MatrixError, List[Device]] =
(get(Seq("devices")) >>= authenticate >>= send).flatMap(json => as(json)(_.downField("devices").as[List[Device]]))
sendWithAuth(get(Seq("devices")))(_.downField("devices").as[List[Device]])
}

object devices extends DeviceManagement;
6 changes: 2 additions & 4 deletions core/src/com/bot4s/zmatrix/api/Login.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ trait Login {
)
.deepDropNullValues

val request = postJson(Seq("login"), json)
(request >>= send) >>= as[LoginResponse]
send[LoginResponse](postJson(Seq("login"), json))
}

def tokenLogin(
Expand All @@ -36,8 +35,7 @@ trait Login {
"device_id" -> deviceId.map(Json.fromString(_)).getOrElse(Json.Null)
)

val request = postJson(Seq("login"), json)
(request >>= send) >>= as[LoginResponse]
send[LoginResponse](postJson(Seq("login"), json))
}

}
Expand Down
4 changes: 1 addition & 3 deletions core/src/com/bot4s/zmatrix/api/RoomCreation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ trait RoomCreation {
* Documentation: https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-createroom
*/
def createRoom(roomCreation: RoomCreationData): ZIO[AuthMatrixEnv, MatrixError, RoomId] =
(postJson(Seq("createRoom"), roomCreation.asJson.dropNullValues) >>= authenticate >>= send).flatMap(json =>
as(json)(_.downField("room_id").as[RoomId])
)
sendWithAuth(postJson(Seq("createRoom"), roomCreation.asJson.dropNullValues))(_.downField("room_id").as[RoomId])

}

Expand Down
Loading

0 comments on commit 98bea89

Please sign in to comment.