Skip to content

Instantly share code, notes, and snippets.

@jmcardon
Last active May 15, 2018 05:19
Show Gist options
  • Save jmcardon/71df77870eaef9c5ce506f9e11a4f379 to your computer and use it in GitHub Desktop.
Save jmcardon/71df77870eaef9c5ce506f9e11a4f379 to your computer and use it in GitHub Desktop.
package org.http4s
import cats.effect.IO
import cats.implicits._
import fs2._
import org.http4s.client.Client
import org.http4s.circe._
import org.http4s.dsl.io._
import org.http4s.client.blaze.Http1Client
object ClientExample1 extends StreamApp[IO] {
implicit val boolDecoder: EntityDecoder[IO, Boolean] = jsonOf[IO, Boolean]
def untilTrue(client: Client[IO], endpoint: Request[IO]): IO[Unit] =
client.expect[Boolean](endpoint).flatMap(if (_) IO.unit else untilTrue(client, endpoint))
def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, StreamApp.ExitCode] =
(for {
client <- Http1Client.stream[IO]()
uri <- Stream.eval(IO.fromEither(Uri.fromString("http://ayylmao.com/lolol")))
endpoint = Request[IO](GET, uri)
_ <- Stream.eval(untilTrue(client, endpoint))
} yield StreamApp.ExitCode.Success)
.handleError(_ => StreamApp.ExitCode.Error)
}
object ClientExample2 extends StreamApp[IO] {
def decodeContinualBooleans(stream: Stream[IO, Byte]): Stream[IO, Boolean] = ???
def untilTrue(client: Client[IO], endpoint: Request[IO]): Stream[IO, Boolean] =
client
.streaming(endpoint)(resp => resp.body.through(decodeContinualBooleans))
.takeWhile(!_)
def stream(args: List[String], requestShutdown: IO[Unit]): Stream[IO, StreamApp.ExitCode] =
(for {
client <- Http1Client.stream[IO]()
uri <- Stream.eval(IO.fromEither(Uri.fromString("http://ayylmao.com/lolol")))
endpoint = Request[IO](GET, uri)
_ <- untilTrue(client, endpoint)
} yield StreamApp.ExitCode.Success)
.handleError(_ => StreamApp.ExitCode.Error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment