Skip to content

Commit 77ecfe0

Browse files
committed
Allow different content types for StreamedBody.RawBinary
1 parent facbbb0 commit 77ecfe0

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

rest/jetty/src/main/scala/io/udash/rest/jetty/JettyRestClient.scala

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,9 @@ final class JettyRestClient(
8989
if (contentLength == -1) {
9090
val contentTypeOpt = response.getHeaders.get(HttpHeader.CONTENT_TYPE).opt
9191
val mediaTypeOpt = contentTypeOpt.map(MimeTypes.getContentTypeWithoutCharset)
92-
val bodyOpt = mediaTypeOpt matchOpt {
93-
case Opt(HttpBody.OctetStreamType) =>
94-
StreamedBody.RawBinary(content = rawContentSubject.doOnSubscriptionCancel(cancelRequest))
95-
case Opt(HttpBody.JsonType) =>
96-
val charset = contentTypeOpt.map(MimeTypes.getCharsetFromContentType).getOrElse(HttpBody.Utf8Charset)
92+
val charsetOpt = contentTypeOpt.map(MimeTypes.getCharsetFromContentType)
93+
val bodyOpt = (mediaTypeOpt, charsetOpt) matchOpt {
94+
case (Opt(HttpBody.JsonType), Opt(charset)) =>
9795
// suboptimal - maybe "online" parsing is possible using Jackson / other lib without waiting for full content ?
9896
StreamedBody.JsonList(
9997
elements = Observable
@@ -109,6 +107,11 @@ final class JettyRestClient(
109107
.onErrorFallbackTo(Observable.raiseError(JettyRestClient.Streaming)),
110108
charset = charset,
111109
)
110+
case (Opt(mediaType), _) =>
111+
StreamedBody.binary(
112+
content = rawContentSubject.doOnSubscriptionCancel(cancelRequest),
113+
contentType = contentTypeOpt.getOrElse(mediaType),
114+
)
112115
}
113116
bodyOpt.mapOr(
114117
{

rest/src/main/scala/io/udash/rest/raw/StreamedBody.scala

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ object StreamedBody extends StreamedBodyLowPrio {
3535
* The content is delivered as a stream of byte arrays which can be processed incrementally.
3636
* Useful for large binary files or content that is generated dynamically.
3737
*/
38-
final case class RawBinary(content: Observable[Array[Byte]]) extends NonEmpty {
39-
val contentType: String = HttpBody.OctetStreamType
40-
41-
override def toString: String = super.toString
42-
}
38+
final case class RawBinary(
39+
content: Observable[Array[Byte]],
40+
override val contentType: String,
41+
) extends NonEmpty
4342

4443
/**
4544
* Represents a streamed list of JSON values.
@@ -52,8 +51,6 @@ object StreamedBody extends StreamedBodyLowPrio {
5251
customBatchSize: Opt[Int] = Opt.Empty,
5352
) extends NonEmpty {
5453
val contentType: String = s"${HttpBody.JsonType};charset=$charset"
55-
56-
override def toString: String = super.toString
5754
}
5855

5956
/**
@@ -67,6 +64,12 @@ object StreamedBody extends StreamedBodyLowPrio {
6764

6865
def empty: StreamedBody = Empty
6966

67+
def binary(
68+
content: Observable[Array[Byte]],
69+
contentType: String = HttpBody.OctetStreamType,
70+
): StreamedBody =
71+
RawBinary(content, contentType)
72+
7073
def fromHttpBody(body: HttpBody): StreamedBody = body match {
7174
case HttpBody.Empty => StreamedBody.Empty
7275
case nonEmpty: HttpBody.NonEmpty => StreamedBody.Single(nonEmpty)
@@ -89,7 +92,7 @@ object StreamedBody extends StreamedBodyLowPrio {
8992

9093
implicit val rawBinaryBodyForByteArray: AsRawReal[StreamedBody, Observable[Array[Byte]]] =
9194
AsRawReal.create(
92-
bytes => RawBinary(bytes),
95+
bytes => StreamedBody.binary(bytes),
9396
body => StreamedBody.castOrFail[RawBinary](body).content,
9497
)
9598
}

rest/src/test/scala/io/udash/rest/raw/RawRestTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ class RawRestTest extends AnyFunSuite with ScalaFutures with Matchers {
126126
elements.toListL.map { list =>
127127
s" application/json;charset=$charset\n[${list.map(_.value).mkString(",")}]"
128128
}
129-
case StreamedBody.RawBinary(content) =>
129+
case StreamedBody.RawBinary(content, tpe) =>
130130
content.toListL.map { list =>
131-
s" application/octet-stream\n${list.flatMap(_.iterator.map(b => f"$b%02X")).mkString}"
131+
s" $tpe\n${list.flatMap(_.iterator.map(b => f"$b%02X")).mkString}"
132132
}
133133
case StreamedBody.Single(body) =>
134134
Task.now(repr(body, inNewLine = false))

0 commit comments

Comments
 (0)