Circe Encoders and Decoders with Http4s
Asked Answered
F

1

7

I am trying to use http4s, circe and http4s-circe.

Below I am trying to use the auto derivation feature of circe.

import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s.Status.ResponseClass.Successful
import io.circe.syntax._
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._
import scalaz.concurrent.Task
import io.circe._

final case class Login(username: String, password: String)
final case class Token(token: String)

object JsonHelpers {
   import io.circe.generic.auto._
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
   implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
   implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
   implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}

object Http4sTest2 extends App {
   import JsonHelpers._
   val url = "http://"
   val uri = Uri.fromString(url).valueOr(throw _)
   val list = List[Header](`Content-Type`(MediaType.`application/json`), `Accept`(MediaType.`application/json`))
   val request = Request(uri = uri, method = Method.POST)
      .withBody(Login("foo", "bar").asJson)
      .map{r => r.replaceAllHeaders(list :_*)}.run
   val client = SimpleHttp1Client()
   val result = client.fetch[Option[Token]](request){
      case Successful(response) => response.as[Token].map(Some(_))
      case _ => Task(Option.empty[Token])
   }.run
   println(result)
}

I get multiple instances of these two compiler errors

Error:scalac: missing or invalid dependency detected while loading class file 'GenericInstances.class'.
Could not access type Secondary in object io.circe.Encoder,
because it (or its dependencies) are missing. Check your build definition for
missing or conflicting dependencies. (Re-run with `-Ylog-classpath` to see the problematic classpath.)
A full rebuild may help if 'GenericInstances.class' was compiled against an incompatible version of io.circe.Encoder.


Error:(25, 74) could not find implicit value for parameter encoder: io.circe.Encoder[Login]
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
Fluellen answered 27/1, 2017 at 1:12 Comment(1)
I'm not terribly familiar with the design decisions in http4s, but I assume it's intentional that it only provides EntityEncoder instances for io.circe.Json, not anything with an io.circe.Encoder instance. In your case you could skip all eight explicitly-defined instances by using circe's generic derivation and calling asJson on the Login in withBody.Zen
F
10

I was able to solve this. I did a search on google on sbt circe dependency and I copy pasted the first search result. that was circe 0.1 and that is why things were not working for me.

I changed my dependencies to

libraryDependencies ++= Seq(
  "org.http4s" %% "http4s-core" % http4sVersion,
  "org.http4s" %% "http4s-dsl" % http4sVersion,
  "org.http4s" %% "http4s-blaze-client" % http4sVersion,
  "org.http4s" %% "http4s-circe" % http4sVersion,
  "io.circe" %% "circe-core" % "0.7.0",
  "io.circe" %% "circe-generic" % "0.7.0"
)

and now automatic derivation works fine and I am able to compile the code below

import org.http4s.client.blaze.SimpleHttp1Client
import org.http4s._
import org.http4s.headers._
import org.http4s.circe._

import scalaz.concurrent.Task
import io.circe.syntax._
import io.circe.generic.auto._
import org.http4s.Status.ResponseClass.Successful

case class Login(username: String, password: String)
case class Token(token: String)

object JsonHelpers {
   implicit val loginEntityEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
   implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
   implicit val tokenEntityEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
   implicit val tokenEntityDecoder : EntityDecoder[Token] = jsonOf[Token]
}

object Http4sTest2 extends App {
   import JsonHelpers._
   val url = "http://"
   val uri = Uri.fromString(url).valueOr(throw _)
   val list = List[Header](`Content-Type`(MediaType.`application/json`), `Accept`(MediaType.`application/json`))
   val request = Request(uri = uri, method = Method.POST)
      .withBody(Login("foo", "bar").asJson)
      .map{r => r.replaceAllHeaders(list :_*)}.run
   val client = SimpleHttp1Client()
   val result = client.fetch[Option[Token]](request){
      case Successful(response) => response.as[Token].map(Some(_))
      case _ => Task(Option.empty[Token])
   }.run
   println(result)
}
Fluellen answered 29/1, 2017 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.