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

1.1.0, streaming response body support #21

Merged
merged 9 commits into from
Oct 7, 2024
Merged

1.1.0, streaming response body support #21

merged 9 commits into from
Oct 7, 2024

Conversation

guzba
Copy link
Owner

@guzba guzba commented Oct 6, 2024

import curly

let curl = newCurly()

let stream = curl.request("GET", "https://sse.dev/test", timeout = 5)

echo stream.code
echo stream.headers

try:
  while true:
    var buffer: string
    let bytesRead = stream.read(buffer)
    if bytesRead == 0:
      break
    echo buffer
finally:
  stream.close()

echo "DONE"
  • no callbacks
  • get http status code, response headers, final url etc easily before committing to reading the body
  • stream body in chucks with your own buffer (will append if buffer already has data in it)

If curl.request returns a stream to you (the call does not throw an exception), then you must call close on the stream when you are done.

Calling close is ok after a stream has been fully read or at any time before that to end a stream early.

Calling close is a dealloc call so calling close multiple times is not ok.

Calling close is necessary because the internal Curly thread handling the requests with libcurl has no way of knowing when you don't care about the request anymore.

The meaning of timeout is as follows: the stream must open (all headers received) within that number of seconds. After the headers have been received, curl.request returns a stream. Some number of bytes must arrive within the timeout seconds interval since the last read, or the stream will raise an exception from read as a timeout.

@guzba guzba mentioned this pull request Oct 6, 2024
@guzba guzba changed the title streaming response prototype v2 1.1.0, streaming response body support Oct 7, 2024
@guzba guzba merged commit 64f8844 into master Oct 7, 2024
6 checks passed
@guzba guzba mentioned this pull request Oct 7, 2024
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.

1 participant