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

Abstract away from cats-effect IO #214

Merged
merged 1 commit into from
Jul 9, 2018
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016-2018 47 Degrees, LLC. <http://www.47deg.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package github4s.cats.effect

import scala.concurrent.{ExecutionContext, Future}

import cats.Eval
import cats.effect.Async
import fr.hmil.roshttp.response.SimpleHttpResponse
import github4s.{HttpRequestBuilder, HttpRequestBuilderExtension, HttpRequestBuilderExtensionJS}
import github4s.GithubResponses.GHResponse
import io.circe.Decoder

trait AsyncHttpRequestBuilderExtensionJS extends HttpRequestBuilderExtensionJS {

import monix.execution.Scheduler.Implicits.global

implicit def extensionAsyncJS[F[_]: Async]: HttpRequestBuilderExtension[SimpleHttpResponse, F] =
new HttpRequestBuilderExtension[SimpleHttpResponse, F] {

def run[A](rb: HttpRequestBuilder[SimpleHttpResponse, F])(
implicit D: Decoder[A]): F[GHResponse[A]] =
runMapWrapper[A](rb, decodeEntity[A])

def runEmpty(rb: HttpRequestBuilder[SimpleHttpResponse, F]): F[GHResponse[Unit]] =
runMapWrapper[Unit](rb, emptyResponse)

private[this] def runMapWrapper[A](
rb: HttpRequestBuilder[SimpleHttpResponse, F],
mapResponse: SimpleHttpResponse => GHResponse[A]): F[GHResponse[A]] =
fromFuture(runMap[A, F](rb, mapResponse))

/** Taken from Http4s **/
private def fromFuture[A](future: Eval[Future[A]])(implicit ec: ExecutionContext): F[A] =
Copy link

@kubukoz kubukoz Jul 9, 2018

Choose a reason for hiding this comment

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

Note that there was an issue about having this in cats-effect. Although it'd take a => Future[A], but that's a minor detail. Note that the suggested implementation would short-circuit in case of completed futures (wouldn't call async if the future was Future.successful or Future.failed): typelevel/cats-effect#199 (comment)

Copy link
Contributor Author

@BenFradet BenFradet Jul 9, 2018

Choose a reason for hiding this comment

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

The problem is that the reference implementation uses TrampolineEC and I didn't feel like copying that over too.

just understood what you meant, I'll see what we can change

Async[F].async { cb =>
import scala.util.{Failure, Success}
future.value.onComplete {
case Failure(e) => cb(Left(e))
case Success(a) => cb(Right(a))
}
}
private def fromFuture[A](future: => Future[A])(implicit ec: ExecutionContext): F[A] =
fromFuture(Eval.always(future))

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package github4s.cats.effect.js

import cats.effect.IO
import cats.effect.Async
import fr.hmil.roshttp.response.SimpleHttpResponse
import github4s.HttpRequestBuilderExtensionJS
import github4s.cats.effect.{IOCaptureInstance, IOHttpRequestBuilderExtensionJS}
import github4s.cats.effect.{AsyncHttpRequestBuilderExtensionJS, SyncCaptureInstance}
import github4s.free.interpreters.Interpreters
import github4s.implicits._

trait ImplicitsJS extends IOHttpRequestBuilderExtensionJS with IOCaptureInstance {
implicit val intInstanceIORosHttp =
new Interpreters[IO, SimpleHttpResponse]
}
trait ImplicitsJS extends AsyncHttpRequestBuilderExtensionJS with SyncCaptureInstance {
implicit def intInstanceAsyncRosHttp[F[_]: Async] =
new Interpreters[F, SimpleHttpResponse]
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ class CatsEffectJSSpec extends AsyncFunSuite with Matchers {

// only here for the 80% coverage, to remove once JS makes use of Captures
test("IOCapture == IO.apply") {
ioCaptureInstance.capture("a").unsafeRunSync shouldBe IO("a").unsafeRunSync
syncCaptureInstance[IO].capture("a").unsafeRunSync shouldBe IO("a").unsafeRunSync
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package github4s.cats.effect.jvm

import cats.effect.IO
import cats.effect.Sync
import github4s.HttpRequestBuilderExtensionJVM
import github4s.cats.effect.IOCaptureInstance
import github4s.cats.effect.SyncCaptureInstance
import github4s.free.interpreters.Interpreters
import github4s.implicits._
import scalaj.http.HttpResponse

trait ImplicitsJVM extends HttpRequestBuilderExtensionJVM with IOCaptureInstance {
implicit val intInstanceIOScalaJ =
new Interpreters[IO, HttpResponse[String]]
}
trait ImplicitsJVM extends HttpRequestBuilderExtensionJVM with SyncCaptureInstance {
implicit def intInstanceSyncScalaJ[F[_]: Sync] =
new Interpreters[F, HttpResponse[String]]
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package github4s.cats.effect

import cats.effect.IO
import cats.effect.Sync
import github4s.free.interpreters.Capture

trait IOCaptureInstance {
implicit val ioCaptureInstance = new Capture[IO] {
override def capture[A](a: A): IO[A] = IO(a)
trait SyncCaptureInstance {
implicit def syncCaptureInstance[F[_]: Sync] = new Capture[F] {
override def capture[A](a: A): F[A] = Sync[F].delay(a)
}
}
10 changes: 5 additions & 5 deletions docs/src/main/tut/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ an [HttpClient][http-client].
The previously mentioned implicit classes carry out of the box
instances for working with [scalaj][scalaj] (for JVM-compatible apps) and [roshttp][roshttp] (for
scala-js-compatible apps). Take into account that in the latter case, you can only use `Future` and
`cats.effect.IO` (if you pull in the `github4s-cats-effect` dependency) in place of `M[_]`.
`cats.effect.Async` (if you pull in the `github4s-cats-effect` dependency) in place of `M[_]`.

A few examples follow with different `MonadError[M, Throwable]`.

Expand Down Expand Up @@ -124,9 +124,9 @@ object ProgramTask {

Note that you'll need a dependency to the `github4s-scalaz` pacakge to leverage `scalaz.Task`.

### Using `cats.effect.IO`
### Using `cats.effect.{Async, Sync}`

On the JVM:
On the JVM, you can use any `cats.effect.Sync`, here we're using `cats.effect.IO`:
```tut:silent
import cats.effect.IO
import github4s.cats.effect.jvm.Implicits._
Expand All @@ -137,7 +137,7 @@ object ProgramTask {
}
```

Using scala-js:
Using scala-js, you can use any `cats.effect.Async`, here we're using `cats.effect.IO`:
```tut:silent
import github4s.cats.effect.js.Implicits._
import fr.hmil.roshttp.response.SimpleHttpResponse
Expand All @@ -152,7 +152,7 @@ object ProgramTask {
```

Note that you'll need a dependency to the `github4s-cats-effect` package to leverage
`cats.effect.IO`.
cats-effect integration.

## Specifying custom headers

Expand Down