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

stream headers not accessible before starting stream #172

Open
mrArkwright opened this issue Sep 7, 2024 · 0 comments
Open

stream headers not accessible before starting stream #172

mrArkwright opened this issue Sep 7, 2024 · 0 comments

Comments

@mrArkwright
Copy link

Right now it is not possible to do something like the following

val stream = requests.get.stream(…)

val responseStatusCode = ??? // not possible to get hold of header data here

if (responseStatusCode == 200) {
  os.write(..., stream)
} else {
  println("Error. Not streaming file.")
}

There seem to be two issues that prevent the status code (and other header data) to be accessible:

  1. The stream headers are only made available through a callback. They will therefore live in a separate scope and are not trivially accessible in the scope of the stream and can't be used to determine how to proceed with processing the stream.
  2. The http request is only started once the stream is accessed. Only then the aforementioned callback is called. This is ibviously too late to make a decision about what to do with the stream.

I came across this while porting a script from python (using the python requests library) in which the desired functionality does work.

Expected behaviour: the return value of requests.get.stream(…) makes contains the stream headers. This means upon call the method performs the request, fetches the headers but does not start to stream the body yet. This behaviour would also resemble that of the python equivalent.

Proposed solution: stream(…) returns a ReadableWithStreamHeaders or a Readable with WithStreamHeaders like this

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

trait WithStreamHeaders {
  def streamHeaders: StreamHeaders
}

I've already successfully tried to refactor stream(…) to incorporate this solution. Am happy to provide a PR.

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

No branches or pull requests

1 participant