diff --git a/core/src/main/scala/sttp/client4/internal/encoders/ContentCodec.scala b/core/src/main/scala/sttp/client4/internal/encoders/ContentCodec.scala index 2e84003f26..9f3c15d435 100644 --- a/core/src/main/scala/sttp/client4/internal/encoders/ContentCodec.scala +++ b/core/src/main/scala/sttp/client4/internal/encoders/ContentCodec.scala @@ -8,7 +8,6 @@ import sttp.model.MediaType import scala.annotation.tailrec - trait ContentCodec[C <: ContentEncoding] { type BodyWithLength = (BasicBodyPart, Int) @@ -23,31 +22,28 @@ trait ContentCodec[C <: ContentEncoding] { abstract class AbstractContentCodec[C <: ContentEncoding] extends ContentCodec[C] { - override def encode(body: BasicBodyPart): Either[EncoderError, BodyWithLength] = { + override def encode(body: BasicBodyPart): Either[EncoderError, BodyWithLength] = body match { case StringBody(s, encoding, ct) => encode(s.getBytes(encoding), ct) - case ByteArrayBody(b, ct) => encode(b, ct) - case ByteBufferBody(b, ct) => encode(b.array(), ct) - case InputStreamBody(b, ct) => encode(b.readAllBytes(), ct) - case FileBody(f, ct) => encode(f.readAsByteArray, ct) + case ByteArrayBody(b, ct) => encode(b, ct) + case ByteBufferBody(b, ct) => encode(b.array(), ct) + case InputStreamBody(b, ct) => encode(b.readAllBytes(), ct) + case FileBody(f, ct) => encode(f.readAsByteArray, ct) } - } - private def encode(bytes: Array[Byte], ct: MediaType): Either[EncoderError, BodyWithLength] = { + private def encode(bytes: Array[Byte], ct: MediaType): Either[EncoderError, BodyWithLength] = encode(bytes).map(r => ByteArrayBody(r, ct) -> r.length) - } override def decode(body: BasicBodyPart): Either[EncoderError, BodyWithLength] = body match { case StringBody(s, encoding, ct) => decode(s.getBytes(encoding), ct) - case ByteArrayBody(b, ct) => decode(b, ct) - case ByteBufferBody(b, ct) => decode(b.array(), ct) - case InputStreamBody(b, ct) => decode(b.readAllBytes(), ct) - case FileBody(f, ct) => decode(f.readAsByteArray, ct) + case ByteArrayBody(b, ct) => decode(b, ct) + case ByteBufferBody(b, ct) => decode(b.array(), ct) + case InputStreamBody(b, ct) => decode(b.readAllBytes(), ct) + case FileBody(f, ct) => decode(f.readAsByteArray, ct) } - private def decode(bytes: Array[Byte], ct: MediaType): Either[EncoderError, BodyWithLength] = { + private def decode(bytes: Array[Byte], ct: MediaType): Either[EncoderError, BodyWithLength] = decode(bytes).map(r => ByteArrayBody(r, ct) -> r.length) - } def encode(bytes: Array[Byte]): Either[EncoderError, Array[Byte]] def decode(bytes: Array[Byte]): Either[EncoderError, Array[Byte]] @@ -59,25 +55,24 @@ object ContentCodec { private val deflateCodec = new DeflateContentCodec - def encode(b: BasicBodyPart, codec: List[ContentEncoding]): Either[EncoderError, (BasicBodyPart, Int)] = { - foldLeftInEither(codec, b -> 0) { case ((l,_), r) => + def encode(b: BasicBodyPart, codec: List[ContentEncoding]): Either[EncoderError, (BasicBodyPart, Int)] = + foldLeftInEither(codec, b -> 0) { case ((l, _), r) => r match { case _: Gzip => gzipCodec.encode(l) case _: Deflate => deflateCodec.encode(l) case e => Left(UnsupportedEncoding(e)) } } - } @tailrec - private def foldLeftInEither[T, R, E](elems: List[T], zero: R)(f: (R, T) => Either[E, R]): Either[E, R] = { + private def foldLeftInEither[T, R, E](elems: List[T], zero: R)(f: (R, T) => Either[E, R]): Either[E, R] = elems match { - case Nil => Right[E,R](zero) - case head :: tail => f(zero, head) match { - case l :Left[E, R] => l - case Right(v) => foldLeftInEither(tail, v)(f) - } + case Nil => Right[E, R](zero) + case head :: tail => + f(zero, head) match { + case l: Left[E, R] => l + case Right(v) => foldLeftInEither(tail, v)(f) + } } - } -} \ No newline at end of file +} diff --git a/core/src/main/scala/sttp/client4/internal/encoders/DeflateContentCodec.scala b/core/src/main/scala/sttp/client4/internal/encoders/DeflateContentCodec.scala index 9e23ee4b77..d3f26bf65a 100644 --- a/core/src/main/scala/sttp/client4/internal/encoders/DeflateContentCodec.scala +++ b/core/src/main/scala/sttp/client4/internal/encoders/DeflateContentCodec.scala @@ -20,7 +20,7 @@ class DeflateContentCodec extends AbstractContentCodec[Deflate] { }.toEither.left.map(ex => EncoderError.EncodingFailure(encoding, ex.getMessage)) override def decode(bytes: Array[Byte]): Either[EncoderError, Array[Byte]] = - Using(new ByteArrayOutputStream()){ bos => + Using(new ByteArrayOutputStream()) { bos => val buf = new Array[Byte](1024) val decompresser = new Inflater() decompresser.setInput(bytes, 0, bytes.length) diff --git a/core/src/main/scala/sttp/client4/internal/encoders/GzipContentCodec.scala b/core/src/main/scala/sttp/client4/internal/encoders/GzipContentCodec.scala index d6015d8986..a2c0913fa6 100644 --- a/core/src/main/scala/sttp/client4/internal/encoders/GzipContentCodec.scala +++ b/core/src/main/scala/sttp/client4/internal/encoders/GzipContentCodec.scala @@ -10,21 +10,19 @@ import scala.util.Using class GzipContentCodec extends AbstractContentCodec[Gzip] { - override def encode(bytes: Array[Byte]): Either[EncodingFailure, Array[Byte]] = { - Using(new ByteArrayOutputStream){ baos => - Using(new GZIPOutputStream(baos)){ gzos => + override def encode(bytes: Array[Byte]): Either[EncodingFailure, Array[Byte]] = + Using(new ByteArrayOutputStream) { baos => + Using(new GZIPOutputStream(baos)) { gzos => gzos.write(bytes) gzos.finish() baos.toByteArray } }.flatMap(identity).toEither.left.map(ex => EncodingFailure(encoding, ex.getMessage)) - } - override def decode(bytes: Array[Byte]): Either[EncodingFailure, Array[Byte]] = { + override def decode(bytes: Array[Byte]): Either[EncodingFailure, Array[Byte]] = Using(new GZIPInputStream(new ByteArrayInputStream(bytes))) { b => b.readAllBytes() }.toEither.left.map(ex => EncodingFailure(encoding, ex.getMessage)) - } override def encoding: Gzip = ContentEncoding.gzip diff --git a/core/src/main/scala/sttp/client4/request.scala b/core/src/main/scala/sttp/client4/request.scala index c95ae77a80..6351855e2a 100644 --- a/core/src/main/scala/sttp/client4/request.scala +++ b/core/src/main/scala/sttp/client4/request.scala @@ -145,20 +145,19 @@ case class Request[T]( * Known exceptions are converted by backends to one of [[SttpClientException]]. Other exceptions are thrown * unchanged. */ - def send[F[_]](backend: Backend[F]): F[Response[T]] = { + def send[F[_]](backend: Backend[F]): F[Response[T]] = (this.options.encoding, this.body) match { case (Nil, _) => backend.send(this) case (codecs, b: BasicBodyPart) if codecs.nonEmpty => val (newBody, newLength) = ContentCodec.encode(b, codecs) match { case Left(err) => throw new EncodingException(this, err) - case Right(v) => v + case Right(v) => v } val newReq = this.contentLength(newLength.toLong).copyWithBody(newBody) backend.send(newReq) case _ => backend.send(this) } - } /** Sends the request synchronously, using the given backend. * @@ -170,19 +169,18 @@ case class Request[T]( * Known exceptions are converted by backends to one of [[SttpClientException]]. Other exceptions are thrown * unchanged. */ - def send(backend: SyncBackend): Response[T] = { + def send(backend: SyncBackend): Response[T] = (this.options.encoding, this.body) match { case (codecs, b: BasicBodyPart) if codecs.nonEmpty => val (newBody, newLength) = ContentCodec.encode(b, codecs) match { case Left(err) => throw new EncodingException(this, err) - case Right(v) => v + case Right(v) => v } val newReq = this.contentLength(newLength.toLong).copyWithBody(newBody) backend.send(newReq) case _ => backend.send(this) } - } } object Request { diff --git a/core/src/main/scala/sttp/client4/requestBuilder.scala b/core/src/main/scala/sttp/client4/requestBuilder.scala index 1418c60876..e18b6d0bb0 100644 --- a/core/src/main/scala/sttp/client4/requestBuilder.scala +++ b/core/src/main/scala/sttp/client4/requestBuilder.scala @@ -1,6 +1,6 @@ package sttp.client4 -import sttp.client4.internal.{ContentEncoding, SttpFile, Utf8, contentTypeWithCharset} +import sttp.client4.internal.{contentTypeWithCharset, ContentEncoding, SttpFile, Utf8} import sttp.client4.logging.LoggingOptions import sttp.client4.wrappers.DigestAuthenticationBackend import sttp.model.HasHeaders