Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- final case class Login(username: String, password: String)
- final case class Token(token: String)
- object JsonHelpers {
- import io.circe.generic.auto._
- // implicit val tokenDecoder : Decoder[Token] = Decoder.forProduct1("token")(Token.apply)
- // implicit val tokenEncoder : Encoder[Token] = Encoder.forProduct1("token")(u => u.token)
- // implicit val loginDecoder : Decoder[Login] = Decoder.forProduct2("username", "password")(Login.apply)
- // implicit val loginEncoder : Encoder[Login] = Encoder.forProduct2("username", "password")(u => (u.username, u.password))
- 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://www.myserver.com/api/foo"
- 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[Option[Token]]
- case _ => Task(Option.empty[Token])
- }.run
- println(result)
- }
- 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]
- }
- Error:(23, 67) could not find implicit value for parameter decoder: io.circe.Decoder[Login]
- implicit val loginEntityDecoder : EntityDecoder[Login] = jsonOf[Login]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement