diff options
author | Bjørn Madsen <bm@aeons.dk> | 2017-08-08 08:16:36 +0200 |
---|---|---|
committer | Bjørn Madsen <bm@aeons.dk> | 2017-08-08 08:16:36 +0200 |
commit | 18db8e9d1ee240d11b558cdb9b5c850c5b063080 (patch) | |
tree | da599b595872cbf26813529fec022b1c0bb99b39 /core/src/main | |
parent | e79d090ed707942f534db98abb1a1754c3584329 (diff) | |
download | sttp-18db8e9d1ee240d11b558cdb9b5c850c5b063080.tar.gz sttp-18db8e9d1ee240d11b558cdb9b5c850c5b063080.tar.bz2 sttp-18db8e9d1ee240d11b558cdb9b5c850c5b063080.zip |
Expand BodySerializer to carry a default content type
And update circe module to use this
Diffstat (limited to 'core/src/main')
3 files changed, 34 insertions, 9 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/BodySerializer.scala b/core/src/main/scala/com/softwaremill/sttp/BodySerializer.scala new file mode 100644 index 0000000..040c563 --- /dev/null +++ b/core/src/main/scala/com/softwaremill/sttp/BodySerializer.scala @@ -0,0 +1,29 @@ +package com.softwaremill.sttp + +import com.softwaremill.sttp.model.BasicRequestBody + +/** + * Provide an implicit value of this type to serialize arbitrary classes into a request body. + * Handlers might also provide special logic for serializer instances which they define (e.g. to handle streaming). + */ +trait BodySerializer[B] { + def apply(body: B): BasicRequestBody + def defaultContentType: Option[String] +} + +object BodySerializer { + final def apply[B](implicit instance: BodySerializer[B]): BodySerializer[B] = + instance + + final def instance[B](f: B => BasicRequestBody) = + new BodySerializer[B] { + def apply(body: B): BasicRequestBody = f(body) + val defaultContentType: Option[String] = None + } + + final def instance[B](f: B => BasicRequestBody, contentType: String) = + new BodySerializer[B] { + def apply(body: B): BasicRequestBody = f(body) + val defaultContentType: Option[String] = Option(contentType) + } +} diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala index 4e38662..c17be59 100644 --- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala +++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala @@ -203,9 +203,11 @@ case class RequestT[U[_], T, +S]( * If content type is not yet specified, will be set to * `application/octet-stream`. */ - def body[B: BodySerializer](b: B): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = SerializableBody(implicitly[BodySerializer[B]], b)) + def body[B](b: B)(implicit serializer: BodySerializer[B]): RequestT[U, T, S] = + setContentTypeIfMissing( + serializer.defaultContentType.getOrElse( + ApplicationOctetStreamContentType)) + .copy(body = SerializableBody(serializer, b)) //def multipartData(parts: MultiPart*): RequestTemplate[U] = ??? diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index 884d2f9..cca86dd 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -22,12 +22,6 @@ package object sttp { ".get(...), .post(...) etc. to obtain a non-partial request.") private[sttp] type IsIdInRequest[U[_]] = U[Unit] =:= Id[Unit] - /** - * Provide an implicit value of this type to serialize arbitrary classes into a request body. - * Handlers might also provide special logic for serializer instances which they define (e.g. to handle streaming). - */ - type BodySerializer[B] = B => BasicRequestBody - // constants private[sttp] val ContentTypeHeader = "Content-Type" |