Skip to content

Commit

Permalink
Merge pull request #161 from davenverse/ce3
Browse files Browse the repository at this point in the history
Cats-Effect 3
  • Loading branch information
ChristopherDavenport authored Aug 4, 2021
2 parents 8b70a16 + 94b5bad commit e9922df
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 32 deletions.
20 changes: 8 additions & 12 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
ThisBuild / crossScalaVersions := Seq("2.12.14", "2.13.6")

val catsV = "2.6.1"
val catsEffectV = "2.5.2"
val fs2V = "2.5.9"
val scodecV = "1.11.8"
val catsEffectV = "3.2.1"
val fs2V = "3.0.6"
val scodecCatsV = "1.1.0"
val http4sV = "0.22.1"
val http4sV = "0.23.0"
val circeV = "0.14.1"
val specs2V = "4.12.3"

val mulesV = "0.4.0"

val kindProjectorV = "0.11.0"
val betterMonadicForV = "0.3.1"
val mulesV = "0.5.0-M1"

// Projects
lazy val `mules-http4s` = project.in(file("."))
Expand All @@ -36,8 +32,8 @@ lazy val core = project.in(file("core"))
"org.http4s" %% "http4s-client" % http4sV,

"io.chrisdavenport" %% "mules" % mulesV,
"io.chrisdavenport" %% "cats-effect-time" % "0.1.2",
"com.comcast" %% "ip4s-test-kit" % "2.0.3"
"io.chrisdavenport" %% "cats-effect-time" % "0.2.0",
"com.comcast" %% "ip4s-test-kit" % "3.0.3"
) ++ testingDeps
)

Expand All @@ -46,7 +42,7 @@ lazy val scodec = project.in(file("scodec"))
.settings(
name := "mules-http4s-scodec",
libraryDependencies ++= Seq(
"org.scodec" %% "scodec-core" % scodecV,
"org.scodec" %% "scodec-core" % (if (isDotty.value) "2.0.0" else "1.11.8"),
"org.scodec" %% "scodec-cats" % scodecCatsV,
) ++ testingDeps
)
Expand All @@ -70,6 +66,6 @@ lazy val testingDeps = Seq(
"org.specs2" %% "specs2-core" % specs2V % Test,
"org.specs2" %% "specs2-scalacheck" % specs2V % Test,
"io.chrisdavenport" %% "cats-scalacheck" % "0.3.1" % Test,
"com.codecommit" %% "cats-effect-testing-specs2" % "0.4.0" % Test,
"org.typelevel" %% "cats-effect-testing-specs2" % "1.2.0" % Test,
"org.http4s" %% "http4s-dsl" % http4sV % Test,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.http4s._
import org.http4s.client.Client
import io.chrisdavenport.mules.http4s.internal._
import cats.data.{Kleisli, OptionT}
import fs2.Stream
import cats.arrow.FunctionK

/**
Expand All @@ -18,7 +17,7 @@ import cats.arrow.FunctionK
**/
object CacheMiddleware {

def client[F[_]: Bracket[*[_], Throwable]: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit compiler: Stream.Compiler[F, F]): Client[F] => Client[F] = {
def client[F[_]: Concurrent: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType): Client[F] => Client[F] = {
val caching = new Caching[F](cache, cacheType){}
{client: Client[F] =>
Client(req =>
Expand All @@ -27,7 +26,7 @@ object CacheMiddleware {
}
}

def httpApp[F[_]: Bracket[*[_], Throwable]: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit compiler: Stream.Compiler[F, F]): HttpApp[F] => HttpApp[F] = {
def httpApp[F[_]: Concurrent: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType): HttpApp[F] => HttpApp[F] = {
val caching = new Caching[F](cache, cacheType){}
{app: HttpApp[F] =>
Kleisli{ req =>
Expand All @@ -36,7 +35,7 @@ object CacheMiddleware {
}
}

def httpRoutes[F[_]: Bracket[*[_], Throwable]: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit compiler: Stream.Compiler[F, F]): HttpRoutes[F] => HttpRoutes[F] = {
def httpRoutes[F[_]: Concurrent: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType): HttpRoutes[F] => HttpRoutes[F] = {
val caching = new Caching[F](cache, cacheType){}
{app: HttpRoutes[F] =>
Kleisli{ req =>
Expand All @@ -49,7 +48,7 @@ object CacheMiddleware {
* Used on a Server rather than Client Caching Mechanism. Headers Applied Are Saved in the cache but caching headers
* are not relayed to external callers.
*/
def internalHttpApp[F[_]: Bracket[*[_], Throwable]: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit compiler: Stream.Compiler[F, F]): HttpApp[F] => HttpApp[F] = {
def internalHttpApp[F[_]: Concurrent: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType): HttpApp[F] => HttpApp[F] = {
val caching = new Caching[F](cache, cacheType){}
{app: HttpApp[F] =>
Kleisli{ req =>
Expand All @@ -65,7 +64,7 @@ object CacheMiddleware {
* Used on a Server rather than Client Caching Mechanism. Headers Applied Are Saved in the cache but caching headers
* are not relayed to external callers.
*/
def internalHttpRoutes[F[_]: Bracket[*[_], Throwable]: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit compiler: Stream.Compiler[F, F]): HttpRoutes[F] => HttpRoutes[F] = {
def internalHttpRoutes[F[_]: Concurrent: JavaTime](cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType): HttpRoutes[F] => HttpRoutes[F] = {
val caching = new Caching[F](cache, cacheType){}
{app: HttpRoutes[F] =>
Kleisli{ req =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final case class CachedResponse(

object CachedResponse {

def fromResponse[F[_], G[_]: Functor](response: Response[F])(implicit compiler: Stream.Compiler[F,G]): G[CachedResponse] = {
def fromResponse[F[_], G[_]: Functor](response: Response[F])(implicit compiler: Compiler[F,G]): G[CachedResponse] = {
response.body.compile.to(ByteVector).map{bv =>
new CachedResponse(
response.status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import org.http4s._
import io.chrisdavenport.mules._
import io.chrisdavenport.cats.effect.time._
import cats._
import cats.implicits._
import cats.syntax.all._
import cats.data._
import fs2.Stream
import cats.effect._
import org.http4s.Header.ToRaw.modelledHeadersToRaw

private[http4s] class Caching[F[_]: MonadError[*[_], Throwable]: JavaTime] private[http4s] (cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType)(implicit Compiler: Stream.Compiler[F,F]){
private[http4s] class Caching[F[_]: Concurrent: JavaTime] private[http4s] (cache: Cache[F, (Method, Uri), CacheItem], cacheType: CacheType){

def request[G[_]: FlatMap](app: Kleisli[G, Request[F], Response[F]], fk: F ~> G)(req: Request[F]): G[Response[F]] = {
if (CacheRules.requestCanUseCached(req)) {
for {
cachedValue <- fk(cache.lookup((req.method, req.uri)))
now <- fk(JavaTime[F].getInstant.map(HttpDate.fromInstant).rethrow)
now <- fk(JavaTime[F].getInstant.map(HttpDate.fromInstant(_)).rethrow)
out <- cachedValue match {
case None =>
if (CacheRules.onlyIfCached(req)) fk(Response[F](Status.GatewayTimeout).pure[F])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package io.chrisdavenport.mules.http4s
import cats.implicits._
import cats.data._
import cats.effect._
import cats.effect.concurrent._
import cats.effect.testing.specs2.CatsIO
import cats.effect.testing.specs2.CatsEffect
import org.http4s._
import org.http4s.implicits._
import org.http4s.headers._
// import org.http4s.dsl.io._
import scala.concurrent.duration._
import io.chrisdavenport.cats.effect.time.JavaTime

class ResponseCachingSpec extends org.specs2.mutable.Specification with CatsIO {
class ResponseCachingSpec extends org.specs2.mutable.Specification with CatsEffect {
"Caching Responses" should {
"never cache a response that should never be cached" in {
for {
Expand Down Expand Up @@ -185,7 +184,7 @@ class ResponseCachingSpec extends org.specs2.mutable.Specification with CatsIO {
firstResp <- newApp.run(request)
first <- firstResp.as[String]

_ <- Timer[IO].sleep(2.seconds)
_ <- Temporal[IO].sleep(2.seconds)

secondResp <- newApp.run(request)
second <- secondResp.as[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.chrisdavenport.mules.http4s._
import org.http4s._
import org.http4s.implicits._
import Arbitraries._
import cats.effect.unsafe.implicits.global
class CodecSpec extends org.specs2.mutable.Specification with org.specs2.ScalaCheck {

"CachedResponse Codec" should {
Expand Down
8 changes: 3 additions & 5 deletions site/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ libraryDependencies ++= Seq(
import cats._
import cats.implicits._
import cats.effect._
import cats.effect.concurrent.Ref
import io.chrisdavenport.mules._
import io.chrisdavenport.mules.caffeine._
import io.chrisdavenport.mules.http4s._
Expand All @@ -32,10 +31,7 @@ import org.http4s.implicits._
import org.http4s.client.Client
import org.http4s.asynchttpclient.client._

implicit val T = IO.timer(scala.concurrent.ExecutionContext.global)
implicit val CS = IO.contextShift(scala.concurrent.ExecutionContext.global)

def testMiddleware[F[_]: Bracket[*[_], Throwable]](c: Client[F], ref: Ref[F, Int]): Client[F] = {
def testMiddleware[F[_]: Concurrent](c: Client[F], ref: Ref[F, Int]): Client[F] = {
Client{req => c.run(req).evalMap(resp => ref.update(_ + 1).as(resp))}
}

Expand All @@ -54,6 +50,8 @@ val exampleCached = AsyncHttpClient.resource[IO]().use{ client =>
} yield (count1, count2)
}

import cats.effect.unsafe.implicits.global // DON'T DO THIS IN PROD

exampleCached.unsafeRunSync()

val dadJokesRequest = Request[IO](Method.GET, uri"https://icanhazdadjoke.com/")
Expand Down

0 comments on commit e9922df

Please sign in to comment.