From 18db8e9d1ee240d11b558cdb9b5c850c5b063080 Mon Sep 17 00:00:00 2001 From: Bjørn Madsen Date: Tue, 8 Aug 2017 08:16:36 +0200 Subject: Expand BodySerializer to carry a default content type And update circe module to use this --- .../com/softwaremill/sttp/BodySerializer.scala | 29 ++++++++++++++++++++++ .../scala/com/softwaremill/sttp/RequestT.scala | 8 +++--- .../main/scala/com/softwaremill/sttp/package.scala | 6 ----- 3 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 core/src/main/scala/com/softwaremill/sttp/BodySerializer.scala (limited to 'core') 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" -- cgit v1.2.3