Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // here we add custom extension to the web request so that it will have the following header:
- // "Accept: application/json;profile=my-profile"
- // build.sbt
- libraryDependencies ++= Seq(
- "org.slf4j" % "slf4j-nop" % "1.7.21",
- "io.circe" %% "circe-generic" % "0.10.0",
- "org.http4s" %% "http4s-circe" % "0.18.23",
- "org.http4s" %% "http4s-blaze-client" % "0.18.23"
- )
- // Hello.scala:
- import scala.language.higherKinds
- import cats.Monad
- import cats.effect.{Effect, IO}
- import io.circe.Decoder
- import org.http4s._
- import org.http4s.client.blaze.Http1Client
- import org.http4s.circe._
- import io.circe.generic.semiauto._
- object Hello extends App {
- case class User(name: String)
- implicit val userDecoder: Decoder[User] = deriveDecoder
- def jsonExtensionsDecoder[F[_]: Monad: Effect, A](extensions: Map[String, String])
- (implicit decoder: Decoder[A]): EntityDecoder[F, A] = {
- new EntityDecoder[F, A] {
- override def decode(msg: Message[F], strict: Boolean): DecodeResult[F, A] = jsonOf[F, A].decode(msg, strict)
- override def consumes: Set[MediaRange] = Set(MediaType.`application/json`.withExtensions(extensions))
- }
- }
- val httpClient = Http1Client[IO]().unsafeRunSync()
- val header = Header("x", "y")
- val usersIO = httpClient.expect(Request[IO](uri = Uri(path = "https://google.com"), headers = Headers(header)))(
- jsonExtensionsDecoder[IO, Seq[User]](Map("profile" -> "my-profile"))
- )
- val users = usersIO.unsafeRunSync()
- println(s"Users: $users")
- }
Add Comment
Please, Sign In to add comment