Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class Token(token: String)
- case class Login(username: String, password: String)
- object JsonCodec {
- implicit val decodeToken : Decoder[Token] = Decoder.forProduct1("token")(Token.apply)
- implicit val encodeToken : Encoder[Token] = Encoder.forProduct1("token")(u => u.token)
- implicit val decodeLogin : Decoder[Login] = Decoder.forProduct2("username", "password")(Login.apply)
- implicit val encodeLogin : Encoder[Login] = Encoder.forProduct2("username", "password")(u => (u.username, u.password))
- }
- import JsonCodec._
- val client = SimpleHttp1Client()
- val uri = Uri.fromString(url).valueOr(throw _)
- val list = List[Header](`Content-Type`(MediaType.`application/json`), `Accept`(MediaType.`application/json`))
- val req = Request(uri = uri, method = Method.POST).withBody(Login("f", "b")).map(_.replaceAllHeaders(list :_*))
- val result = client.expect(req)(jsonOf[Token]).unsafePerformSync
- Some(result.token)
- Error:(49, 66) could not find implicit value for parameter w: org.http4s.EntityEncoder[Login]
- val req = Request(uri = uri, method = Method.POST).withBody(Login("f", "b")).map(_.replaceAllHeaders(list :_*))
- import io.circe.generic.auto._
- import io.circe._
- object JsonHelpers {
- implicit val loginEncoder : EntityEncoder[Login] = jsonEncoderOf[Login]
- implicit val loginDecoder : EntityDecoder[Login] = jsonOf[Login]
- implicit val tokenEncoder: EntityEncoder[Token] = jsonEncoderOf[Token]
- implicit val tokenDecoder : EntityDecoder[Token] = jsonOf[Token]
- implicit val decodeToken : Decoder[Token] = Decoder.forProduct1("token")(Token.apply)
- implicit val encodeToken : Encoder[Token] = Encoder.forProduct1("token")(u => u.token)
- implicit val decodeLogin : Decoder[Login] = Decoder.forProduct2("username", "password")(Login.apply)
- implicit val encodeLogin : Encoder[Login] = Encoder.forProduct2("username", "password")(u => (u.username, u.password))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement