diff options
Diffstat (limited to 'circe')
-rw-r--r-- | circe/src/main/scala/com/softwaremill/sttp/circe.scala | 22 | ||||
-rw-r--r-- | circe/src/test/scala/com/softwaremill/sttp/CirceTests.scala | 8 |
2 files changed, 9 insertions, 21 deletions
diff --git a/circe/src/main/scala/com/softwaremill/sttp/circe.scala b/circe/src/main/scala/com/softwaremill/sttp/circe.scala index 0233f08..f273e42 100644 --- a/circe/src/main/scala/com/softwaremill/sttp/circe.scala +++ b/circe/src/main/scala/com/softwaremill/sttp/circe.scala @@ -1,11 +1,6 @@ package com.softwaremill.sttp -import com.softwaremill.sttp.model.{ - RequestBody, - ResponseAs, - SerializableBody, - StringBody -} +import com.softwaremill.sttp.model.{ResponseAs, StringBody} import io.circe.parser._ import io.circe.{Decoder, Encoder} @@ -14,17 +9,10 @@ import scala.language.higherKinds package object circe { private[sttp] val ApplicationJsonContentType = "application/json" - private def serializeBody[B](body: B)( - implicit encoder: Encoder[B]): RequestBody[Nothing] = - SerializableBody((b: B) => StringBody(encoder(b).noSpaces, Utf8), body) - - implicit final class CirceRequestTOps[U[_], T, +S](val req: RequestT[U, T, S]) - extends AnyVal { - def jsonBody[B: Encoder](body: B): RequestT[U, T, S] = - req - .setContentTypeIfMissing(ApplicationJsonContentType) - .copy(body = serializeBody(body)) - } + implicit def circeBodySerializer[B: Encoder]: BodySerializer[B] = + BodySerializer.instance( + body => StringBody(Encoder[B].apply(body).noSpaces, Utf8), + ApplicationJsonContentType) def asJson[B: Decoder]: ResponseAs[Either[io.circe.Error, B], Nothing] = asString(Utf8).map(decode[B]) diff --git a/circe/src/test/scala/com/softwaremill/sttp/CirceTests.scala b/circe/src/test/scala/com/softwaremill/sttp/CirceTests.scala index abd20ef..81b90e6 100644 --- a/circe/src/test/scala/com/softwaremill/sttp/CirceTests.scala +++ b/circe/src/test/scala/com/softwaremill/sttp/CirceTests.scala @@ -13,7 +13,7 @@ class CirceTests extends FlatSpec with Matchers with EitherValues { val body = Outer(Inner(42, true, "horses"), "cats") val expected = """{"foo":{"a":42,"b":true,"c":"horses"},"bar":"cats"}""" - val req = sttp.jsonBody(body) + val req = sttp.body(body) extractBody(req) shouldBe expected } @@ -38,7 +38,7 @@ class CirceTests extends FlatSpec with Matchers with EitherValues { it should "should encode and decode back to the same thing" in { val outer = Outer(Inner(42, true, "horses"), "cats") - val encoded = extractBody(sttp.jsonBody(outer)) + val encoded = extractBody(sttp.body(outer)) val decoded = runJsonResponseAs(asJson[Outer])(encoded) decoded.right.value shouldBe outer @@ -46,7 +46,7 @@ class CirceTests extends FlatSpec with Matchers with EitherValues { it should "set the content type" in { val body = Outer(Inner(42, true, "horses"), "cats") - val req = sttp.jsonBody(body) + val req = sttp.body(body) val ct = req.headers.toMap.get("Content-Type") @@ -55,7 +55,7 @@ class CirceTests extends FlatSpec with Matchers with EitherValues { it should "only set the content type if it was not set earlier" in { val body = Outer(Inner(42, true, "horses"), "cats") - val req = sttp.contentType("horses/cats").jsonBody(body) + val req = sttp.contentType("horses/cats").body(body) val ct = req.headers.toMap.get("Content-Type") |