How to determine if StandaloneWSResponse body is present in memory?


I’m updating our integrations from "play-ws" % "2.5.18" to "play-ws-standalone" % "2.0.0-M2" in preparation for adopting play 2.7.

Given a StandaloneWSResponse and a dependency on play-ws-standalone (e.g. not necessarily play-ahc-ws-standalone), is it possible to know if the response body is present in memory?

I’d like to log the response body, but the implementation needs to vary depending on the answer to that question. I’d love to call bodyAsBytes on a body in memory immediately, but certainly not on one that will be streamed. Pattern matching is out since trait StandaloneWSResponse is not sealed, and decorators do exist.

Here’s a naive attempt:

if (!standaloneWSResponse.underlying[AnyRef].isInstanceOf[Publisher[_]]) {
  // assume body is in memory?
  val byteString = standaloneWSResponse.bodyAsBytes // non-blocking ?
} else {
  // don't eagerly consume the stream
  val tappedBody = standaloneWSResponse.bodyAsSource.alsoTo(loggingSink)
  new DecoratedStandaloneWSResponse(standaloneWSResponse, tappedBody)

Is there a better way to do this?