diff options
author | Omar Alejandro Mainegra Sarduy <omainegra@gmail.com> | 2017-08-11 17:45:29 -0400 |
---|---|---|
committer | Omar Alejandro Mainegra Sarduy <omainegra@gmail.com> | 2017-08-11 17:45:29 -0400 |
commit | ad28cb44c9a39b6bb5b7176358381b19b6e9ac28 (patch) | |
tree | 7a0a10f6270e3233633eda1e7f6b90ea4a12085d /core/src | |
parent | c6c1f5a34930946e8ab4e9248b9255ce2e1464fe (diff) | |
parent | 00e10184c05431d2692d2b54425481a67ee3bc8e (diff) | |
download | sttp-ad28cb44c9a39b6bb5b7176358381b19b6e9ac28.tar.gz sttp-ad28cb44c9a39b6bb5b7176358381b19b6e9ac28.tar.bz2 sttp-ad28cb44c9a39b6bb5b7176358381b19b6e9ac28.zip |
Merge branch 'master' into okhttp3-monix
# Conflicts:
# build.sbt
# okhttp-client-handler/src/main/scala/com/softwaremill/sttp/okhttp/OkHttpClientHandler.scala
Diffstat (limited to 'core/src')
4 files changed, 59 insertions, 39 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionSttpHandler.scala b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionSttpHandler.scala index 29da886..dd208f4 100644 --- a/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionSttpHandler.scala +++ b/core/src/main/scala/com/softwaremill/sttp/HttpURLConnectionSttpHandler.scala @@ -56,28 +56,25 @@ object HttpURLConnectionSttpHandler extends SttpHandler[Id, Nothing] { body match { case NoBody => // skip - case StringBody(b, encoding) => + case StringBody(b, encoding, _) => val writer = new OutputStreamWriter(c.getOutputStream, encoding) try writer.write(b) finally writer.close() - case ByteArrayBody(b) => + case ByteArrayBody(b, _) => c.getOutputStream.write(b) - case ByteBufferBody(b) => + case ByteBufferBody(b, _) => val channel = Channels.newChannel(c.getOutputStream) try channel.write(b) finally channel.close() - case InputStreamBody(b) => + case InputStreamBody(b, _) => copyStream(b, c.getOutputStream) - case PathBody(b) => + case PathBody(b, _) => Files.copy(b, c.getOutputStream) - case SerializableBody(f, t) => - setBody(f(t), c) - case StreamBody(s) => // we have an instance of nothing - everything's possible! s diff --git a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala index ff80dd5..b785e7c 100644 --- a/core/src/main/scala/com/softwaremill/sttp/RequestT.scala +++ b/core/src/main/scala/com/softwaremill/sttp/RequestT.scala @@ -100,10 +100,8 @@ case class RequestT[U[_], T, +S]( * bytes in the string using the given encoding. */ def body(b: String, encoding: String): RequestT[U, T, S] = - setContentTypeIfMissing( - contentTypeWithEncoding(TextPlainContentType, encoding)) + withBasicBody(StringBody(b, encoding)) .setContentLengthIfMissing(b.getBytes(encoding).length) - .copy(body = StringBody(b, encoding)) /** * If content type is not yet specified, will be set to @@ -113,25 +111,22 @@ case class RequestT[U[_], T, +S]( * of the given array. */ def body(b: Array[Byte]): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType) + withBasicBody(ByteArrayBody(b)) .setContentLengthIfMissing(b.length) - .copy(body = ByteArrayBody(b)) /** * If content type is not yet specified, will be set to * `application/octet-stream`. */ def body(b: ByteBuffer): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = ByteBufferBody(b)) + withBasicBody(ByteBufferBody(b)) /** * If content type is not yet specified, will be set to * `application/octet-stream`. */ def body(b: InputStream): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = InputStreamBody(b)) + withBasicBody(InputStreamBody(b)) /** * If content type is not yet specified, will be set to @@ -151,9 +146,8 @@ case class RequestT[U[_], T, +S]( * of the given file. */ def body(b: Path): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType) + withBasicBody(PathBody(b)) .setContentLengthIfMissing(b.toFile.length()) - .copy(body = PathBody(b)) /** * Encodes the given parameters as form data using `utf-8`. @@ -204,13 +198,10 @@ case class RequestT[U[_], T, +S]( * `application/octet-stream`. */ def body[B: BodySerializer](b: B): RequestT[U, T, S] = - setContentTypeIfMissing(ApplicationOctetStreamContentType).copy( - body = SerializableBody(implicitly[BodySerializer[B]], b)) - - //def multipartData(parts: MultiPart*): RequestTemplate[U] = ??? + withBasicBody(implicitly[BodySerializer[B]].apply(b)) def streamBody[S2 >: S](b: S2): RequestT[U, T, S2] = - this.copy[U, T, S2](body = StreamBody(b)) + copy[U, T, S2](body = StreamBody(b)) /** * What's the target type to which the response body should be read. @@ -239,6 +230,17 @@ case class RequestT[U[_], T, +S]( private def setContentTypeIfMissing(ct: String): RequestT[U, T, S] = if (hasContentType) this else contentType(ct) + private def withBasicBody(body: BasicRequestBody) = { + val defaultCt = body match { + case StringBody(_, encoding, Some(ct)) => + Some(contentTypeWithEncoding(ct, encoding)) + case _ => + body.defaultContentType + } + + defaultCt.fold(this)(setContentTypeIfMissing).copy(body = body) + } + private def hasContentLength: Boolean = headers.exists(_._1.equalsIgnoreCase(ContentLengthHeader)) private def setContentLengthIfMissing(l: => Long): RequestT[U, T, S] = @@ -247,10 +249,11 @@ case class RequestT[U[_], T, +S]( private def formDataBody(fs: Seq[(String, String)], encoding: String): RequestT[U, T, S] = { val b = fs - .map( - p => - URLEncoder.encode(p._1, encoding) + "=" + URLEncoder - .encode(p._2, encoding)) + .map { + case (key, value) => + URLEncoder.encode(key, encoding) + "=" + + URLEncoder.encode(value, encoding) + } .mkString("&") setContentTypeIfMissing(ApplicationFormContentType) .setContentLengthIfMissing(b.getBytes(encoding).length) diff --git a/core/src/main/scala/com/softwaremill/sttp/model/RequestBody.scala b/core/src/main/scala/com/softwaremill/sttp/model/RequestBody.scala index 0c737f0..7499048 100644 --- a/core/src/main/scala/com/softwaremill/sttp/model/RequestBody.scala +++ b/core/src/main/scala/com/softwaremill/sttp/model/RequestBody.scala @@ -4,18 +4,39 @@ import java.io.InputStream import java.nio.ByteBuffer import java.nio.file.Path -import com.softwaremill.sttp.BodySerializer +import com.softwaremill.sttp._ sealed trait RequestBody[+S] case object NoBody extends RequestBody[Nothing] -case class SerializableBody[T](f: BodySerializer[T], t: T) - extends RequestBody[Nothing] - -sealed trait BasicRequestBody extends RequestBody[Nothing] -case class StringBody(s: String, encoding: String) extends BasicRequestBody -case class ByteArrayBody(b: Array[Byte]) extends BasicRequestBody -case class ByteBufferBody(b: ByteBuffer) extends BasicRequestBody -case class InputStreamBody(b: InputStream) extends BasicRequestBody -case class PathBody(f: Path) extends BasicRequestBody + +sealed trait BasicRequestBody extends RequestBody[Nothing] { + def defaultContentType: Option[String] +} + +case class StringBody( + s: String, + encoding: String, + defaultContentType: Option[String] = Some(TextPlainContentType) +) extends BasicRequestBody + +case class ByteArrayBody( + b: Array[Byte], + defaultContentType: Option[String] = Some(ApplicationOctetStreamContentType) +) extends BasicRequestBody + +case class ByteBufferBody( + b: ByteBuffer, + defaultContentType: Option[String] = Some(ApplicationOctetStreamContentType) +) extends BasicRequestBody + +case class InputStreamBody( + b: InputStream, + defaultContentType: Option[String] = Some(ApplicationOctetStreamContentType) +) extends BasicRequestBody + +case class PathBody( + f: Path, + defaultContentType: Option[String] = Some(ApplicationOctetStreamContentType) +) extends BasicRequestBody case class StreamBody[S](s: S) extends RequestBody[S] diff --git a/core/src/main/scala/com/softwaremill/sttp/model/ResponseAs.scala b/core/src/main/scala/com/softwaremill/sttp/model/ResponseAs.scala index 24b9b2b..5a008ed 100644 --- a/core/src/main/scala/com/softwaremill/sttp/model/ResponseAs.scala +++ b/core/src/main/scala/com/softwaremill/sttp/model/ResponseAs.scala @@ -2,7 +2,6 @@ package com.softwaremill.sttp.model import java.io.{File, FileOutputStream, IOException, InputStream} import java.net.URLDecoder -import java.nio.file.Path import com.softwaremill.sttp.{MonadError, transfer} |