Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make stream headers accessible in return value of stream method #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mrArkwright
Copy link

@mrArkwright mrArkwright commented Sep 7, 2024

Addresses #172

Refactors Resquester.stream() to return a

trait ReadableWithStreamHeaders extends geny.Readable {
  def streamHeaders: StreamHeaders
}

to make the stream headers directly accessible.

This changes the semantics of stream() to immediately perform the request and retrieve the headers. The body is still only retrieved when the stream is consumed.

A cleaner approach would probably be to return a tuple. However to avoid a braking change in the API the approach with ReadableWithStreamHeaders was chosen.

The onHeadersReceived can probably be deprecated as it should no longer be needed.

@lihaoyi
Copy link
Member

lihaoyi commented Sep 16, 2024

So I'm not sure the fix works given the API we conform to. geny.Readable is intentionally lazy and re-usable, and so accessing the streamHeaders outside the scope of a geny.Readable#readBytesThrough or writeBytesTo callback doesn't really make sense.

I think the correct way to resolve this is to make ReadableWithStreamHeaders provide an additional context def readWithHeaders[T](f: (StreamHeaders, InputStream) => T): T callback that provides the headers at time of use, and thus has a chance to provide different headers each time a single Readable is re-used. Then your example in the initial issue would look something like

requests.get.stream(…).readWithHeaders{ (headers, stream) =>
  if (headers.statusCode == 200) {
    os.write(..., stream)
  } else {
    println("Error. Not streaming file.")
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants