Skip to content

Commit

Permalink
feature/softwaremill#1918 scalafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ifedorov committed Apr 23, 2024
1 parent 737d90a commit 5542ffc
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import sttp.model.MediaType

import scala.annotation.tailrec


trait ContentCodec[C <: ContentEncoding] {

type BodyWithLength = (BasicBodyPart, Int)
Expand All @@ -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]]
Expand All @@ -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)
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions core/src/main/scala/sttp/client4/request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/sttp/client4/requestBuilder.scala
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 5542ffc

Please sign in to comment.