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

Update to cats-effect 3 #508

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 9 additions & 11 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ inThisBuild(
))

val Versions = Map(
"circe" -> "0.13.0",
"circe" -> "0.14.1",
"monocle" -> "2.1.0",
"collection-compat" -> "2.3.2",
"atto" -> "0.9.0",
"cats" -> "2.2.0",
"cats-effect" -> "2.2.0",
"collection-compat" -> "2.5.0",
"atto" -> "0.9.5",
"cats" -> "2.6.1",
"cats-effect" -> "3.2.0",
"simulacrum" -> "1.0.1",
"scalatest" -> "3.2.3",
"scalacheck" -> "1.15.2",
"scalatest" -> "3.2.9",
"scalacheck" -> "1.15.4",
"scalatestplusScalaCheck" -> "3.2.2.0",
"scalatestplusMockito" -> "1.0.0-M2",
"discipline" -> "1.1.3",
"discipline-scalatest" -> "2.1.1",
"discipline" -> "1.1.5",
"discipline-scalatest" -> "2.1.5",
"macro-paradise" -> "2.1.1",
"kind-projector" -> "0.10.3",
"akka-http" -> "10.1.10",
Expand Down Expand Up @@ -270,8 +270,6 @@ lazy val docs = project
Map("title" -> "changelog", "section" -> "changelog", "position" -> "99")
)
),
micrositeCompilingDocsTool := WithMdoc,
mdocIn := tutSourceDirectory.value,
scalacOptions ~= filterConsoleScalacOptions,
scalacOptions += "-language:postfixOps"
)
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/scala/hammock/http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ object Ops {

class HttpRequestC[F[_]](implicit I: InjectK[HttpF, F]) {
def options(uri: Uri, headers: Map[String, String]): Free[F, HttpResponse] =
Free.inject(Options(HttpRequest(uri, headers, None)))
Free.liftInject(Options(HttpRequest(uri, headers, None)))
def get(uri: Uri, headers: Map[String, String]): Free[F, HttpResponse] =
Free.inject(Get(HttpRequest(uri, headers, None)))
Free.liftInject(Get(HttpRequest(uri, headers, None)))
def head(uri: Uri, headers: Map[String, String]): Free[F, HttpResponse] =
Free.inject(Head(HttpRequest(uri, headers, None)))
Free.liftInject(Head(HttpRequest(uri, headers, None)))
def post(uri: Uri, headers: Map[String, String], entity: Option[Entity]): Free[F, HttpResponse] =
Free.inject(Post(HttpRequest(uri, headers, entity)))
Free.liftInject(Post(HttpRequest(uri, headers, entity)))
def put(uri: Uri, headers: Map[String, String], entity: Option[Entity]): Free[F, HttpResponse] =
Free.inject(Put(HttpRequest(uri, headers, entity)))
Free.liftInject(Put(HttpRequest(uri, headers, entity)))
def delete(uri: Uri, headers: Map[String, String]): Free[F, HttpResponse] =
Free.inject(Delete(HttpRequest(uri, headers, None)))
Free.liftInject(Delete(HttpRequest(uri, headers, None)))
def trace(uri: Uri, headers: Map[String, String]): Free[F, HttpResponse] =
Free.inject(Trace(HttpRequest(uri, headers, None)))
Free.liftInject(Trace(HttpRequest(uri, headers, None)))
def patch(uri: Uri, headers: Map[String, String], entity: Option[Entity]): Free[F, HttpResponse] =
Free.inject(Patch(HttpRequest(uri, headers, entity)))
Free.liftInject(Patch(HttpRequest(uri, headers, entity)))
}

object HttpRequestC {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ object Log {
case class Error(msg: String) extends LogF[Unit]

class LogC[F[_]](implicit I: InjectK[LogF, F]) {
def info(msg: String): Free[F, Unit] = Free.inject(Info(msg))
def error(msg: String): Free[F, Unit] = Free.inject(Error(msg))
def info(msg: String): Free[F, Unit] = Free.liftInject(Info(msg))
def error(msg: String): Free[F, Unit] = Free.liftInject(Error(msg))
}

object LogC {
Expand Down Expand Up @@ -82,8 +82,8 @@ object IOEff {
case class Write(msg: String) extends IOF[Unit]

class IOC[F[_]](implicit I: InjectK[IOF, F]) {
def read: Free[F, String] = Free.inject(Read)
def write(str: String): Free[F, Unit] = Free.inject(Write(str))
def read: Free[F, String] = Free.liftInject(Read)
def write(str: String): Free[F, Unit] = Free.liftInject(Write(str))
}

object IOC {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions docs/src/main/mdoc/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import _root_.akka.stream.ActorMaterializer
import _root_.akka.util.ByteString
import cats._
import cats.data.Kleisli
import cats.effect.{Async, ContextShift, Sync}
import cats.effect.{Async, Sync}
import cats.implicits._
import scala.concurrent.{ExecutionContext, Future}

object AkkaInterpreter {

def apply[F[_]](implicit F: InterpTrans[F]): InterpTrans[F] = F

implicit def instance[F[_]: Async: ContextShift](
implicit def instance[F[_]: Async](
implicit
client: HttpExt,
materializer: ActorMaterializer,
Expand All @@ -33,15 +33,15 @@ object AkkaInterpreter {
override def trans: HttpF ~> F = transK andThen λ[Kleisli[F, HttpExt, *] ~> F](_.run(client))
}

def transK[F[_]: Async: ContextShift](
def transK[F[_]: Async](
implicit materializer: ActorMaterializer,
executionContext: ExecutionContext): HttpF ~> Kleisli[F, HttpExt, *] = {

def doReq(req: HttpF[HttpResponse]): Kleisli[F, HttpExt, HttpResponse] = Kleisli { http =>
for {
akkaRequest <- mapRequest(req)
responseFuture <- Sync[F].delay(http.singleRequest(akkaRequest).flatMap(mapResponse))
responseF <- Async.fromFuture(Async[F].delay(responseFuture))
responseF <- Async[F].fromFuture(Sync[F].delay(responseFuture))
} yield responseF
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ import _root_.akka.http.scaladsl.model.{
}
import _root_.akka.stream.ActorMaterializer
import _root_.akka.util.ByteString
import cats.effect.{ContextShift, IO}
import cats.effect.IO
import cats.free.Free
import hammock.akka.AkkaInterpreter._
import org.mockito.Mockito._
import org.scalatest.BeforeAndAfter
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.scalatestplus.mockito.MockitoSugar

import cats.effect.unsafe.implicits.global
import scala.concurrent.{ExecutionContext, Future}

class AkkaInterpreterSpec extends AnyWordSpec with MockitoSugar with Matchers with BeforeAndAfter {

implicit val system: ActorSystem = ActorSystem("test")
implicit val mat: ActorMaterializer = ActorMaterializer()
implicit val ec: ExecutionContext = ExecutionContext.Implicits.global
implicit val cs: ContextShift[IO] = IO.contextShift(ec)
implicit val client: HttpExt = mock[HttpExt]

after {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.net.URI
import cats._
import cats.data.Kleisli
import cats.effect._
import cats.effect.unsafe.implicits.global
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpUriRequest
import org.apache.http.entity.StringEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import cats.implicits._
import cats.data.Kleisli
import cats.effect._
import org.asynchttpclient._
import java.util.{concurrent => jc}
import java.util.concurrent.CompletableFuture
import scala.util._
import scala.jdk.CollectionConverters._

Expand All @@ -22,30 +22,27 @@ object AsyncHttpClientInterpreter {

def transK[F[_]: Async]: HttpF ~> Kleisli[F, AsyncHttpClient, *] = {

def toF[A](future: jc.Future[A]): F[A] =
Async[F].async(_(Try(future.get) match {
case Failure(err) => Left(err)
case Success(a) => Right(a)
}))
def toF[A](futureF: F[CompletableFuture[A]]): F[A] =
Async[F].fromCompletableFuture(futureF)

λ[HttpF ~> Kleisli[F, AsyncHttpClient, *]] {
case reqF @ (Get(_) | Options(_) | Delete(_) | Head(_) | Options(_) | Trace(_) | Post(_) | Put(_) | Patch(_)) =>
Kleisli { implicit client =>
for {
req <- mapRequest[F](reqF)
ahcResponse <- toF(req.execute())
ahcResponse <- toF(Sync[F].delay(req.execute().toCompletableFuture()))
hammockResponse <- mapResponse[F](ahcResponse)
} yield hammockResponse
}
}
}

def mapRequest[F[_]: Async](reqF: HttpF[HttpResponse])(implicit client: AsyncHttpClient): F[BoundRequestBuilder] = {
def mapRequest[F[_]: Sync](reqF: HttpF[HttpResponse])(implicit client: AsyncHttpClient): F[BoundRequestBuilder] = {

def putHeaders(req: BoundRequestBuilder, headers: Map[String, String]): F[Unit] =
Async[F].delay {
Sync[F].delay {
req.setSingleHeaders(headers.map(kv => kv._1.asInstanceOf[CharSequence] -> kv._2).asJava)
} *> ().pure[F]
}.void

def getBuilder(reqF: HttpF[HttpResponse]): BoundRequestBuilder = reqF match {
case Get(_) => client.prepareGet(reqF.req.uri.show)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asynchttpclient

import cats.implicits._
import cats.effect._
import cats.effect.unsafe.implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.asynchttpclient._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resttemplate

import cats.implicits._
import cats.effect._
import cats.effect.unsafe.implicits.global
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.scalatestplus.mockito._
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.4.6
sbt.version=1.5.5
16 changes: 8 additions & 8 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("com.47deg" % "sbt-microsites" % "1.2.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.18.0")
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.16")
addSbtPlugin("org.tpolecat" % "tut-plugin" % "0.6.13")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.3")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.5")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.1")
addSbtPlugin("com.47deg" % "sbt-microsites" % "1.3.4")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.13")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.18.0")
addSbtPlugin("com.lucidchart" % "sbt-scalafmt" % "1.16")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.23")
addSbtPlugin("io.get-coursier" % "sbt-coursier" % "1.0.3")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.5")