scalafinagle

Download file with Finagle Http Client


I'm trying to take a byte array from the remote file. I created AsyncStream but don't have any idea how to transform it into a proper byte array.

  val client: Service[http.Request, http.Response] =
    Http
      .client
      .withStreaming(enabled = true)
      .newService("www.scala-lang.org:80")

  val request = http.Request(http.Method.Get, "/docu/files/ScalaOverview.pdf")
  request.host = "scala-lang.org"
  val response: Future[http.Response] = client(request)

  def fromReader(reader: Reader): AsyncStream[Buf] =
    AsyncStream.fromFuture(reader.read(Int.MaxValue)).flatMap {
      case None => AsyncStream.empty
      case Some(a) => a +:: fromReader(reader)
    }

  val result: Array[Byte] =
    Await.result(response.flatMap {
      case resp =>
        fromReader(resp.reader) ??? // what to do?
    })

Solution

  • You don't need fromReader, AsyncStream already has it. So, you can do something like this:

    val result: Future[Array[Byte]] = response
      .flatMap { resp => 
        AsyncStream.fromReader(resp.reader)
          .foldLeft(Buf.Empty){ _ concat _ }
          .map(Buf.ByteArray.Owned.extract)
      }