-
Notifications
You must be signed in to change notification settings - Fork 123
unclear docs: How to signal to StreamWriter that body is done? #194
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
Comments
@weissi You provide it with a length at the start and once you've supplied that data, it completes. I'm not a great fan of this interface. It is really confusing what is happening. Here is my attempt at implementing one https://github.com/swift-aws/aws-sdk-swift-core/blob/bdbac3768398b8387e2a81be193b6c42da75979e/Sources/AWSSDKSwiftCore/HTTP/AsyncHTTPClient.swift#L23 |
@adam-fowler but how do I do streaming? Ie. if I don't know the content length? Then chunked encoding should be selected... |
CC @artemredkin |
Yes that's a good question. That would be very useful. |
@weissi is this something that could be resolved for 1.2. It doesn't look like that complex a thing to do. Return an empty ByteBuffer() to indicate end or something like that and add a chunked header. |
Oh shit just thought this might be a breaking change. Although you could just add another version of the stream function. |
@weissi answering your question, you just return succeeded future from write method: .stream() { writer in
writer.write()
writer.write()
...
writer.write()
return eventLoop.makeSucceededFuture(())
} I'm not a fan of this interface, it was designed mainly around how |
Obviously, this example has absolutely no backpressure, so another alternative will be something like: .stream() { writer in
writer.write().flatMap {
writer.write()
}
} |
Sorry @artem I completely missed that. For some reason I had got it into my head you were required to pass in a content length |
No worries, our docs in this area are not good, and API is not very clear at all. I'll try to improve streaming docs/API's after |
@artemredkin Hmm, I don't get it. A user gets handed a |
user sets a body and passes it a closure, that has a singature: (StreamWriter) -> EventLoopFuture<Void> as soon as this future completes, we send |
@weissi I found it is a little confusing initially. I have a working version here, where you supply a stream function that returns ByteBuffers until you have finished streaming or have hit a specified size. https://github.com/swift-aws/aws-sdk-swift-core/blob/16cce9fe9fbc0f5949fb48a6fd7dce90b3645357/Sources/AWSSDKSwiftCore/HTTP/AsyncHTTPClient.swift#L78 |
@artemredkin oooh, so the user's meant to allocate a I think we should create a new API that just gives you a Or was there a use-case that this was designed for? |
@weissi they need to return a future. I allocated the promise so I wouldnt have to chain all the futures |
I built it around how NonBlockingFileIO works, |
@adam-fowler yeah but to return a future that they can fulfil when they're done, they need to allocate a promise that they hold elsewhere. So basically users need to keep track of two things:
Confusingly, the feed new data through the stream writer but send end through the promise. |
@artemredkin The future you return in |
it totally is super confusing, yeah, it was like a mirror API, so it turned out quite strange |
Yeah it is confusing. Took me a while to work out how to get it working. Personally I would prefer, an API where a stream closure returning an |
How about a an enum: enum Part {
case next(ByteBuffer)
case end
} wdyt? |
So it returns an |
@artemredkin we already got the
I mean I'm happy with an
I'd just use methods but don't mind. Either is a big step up from where we are right now. One other thing that I found very inconvenient was that I had to wait for a callback to get the |
Methods seem a nicer API, moving to 2.0.0 to introduce a new API |
@artemredkin we need to do this before 2.0.0. We can do this with deprecations, pretty sure. |
I hope this is the "right" way to do this, because that's what I did. I read through the rest of this convo and it appears that there's a more intuitive approach on the horizon, but in the meantime can we get this example (or whatever would be best) in the docs? |
If at all possible, you can use the async/await APIs and their new Body type. That has a much clearer interface. |
looks like I can't because of #819 😢 |
StreamWriter
's complete docs areI understand how I can provide chunks: by calling
write
but how do I tell theStreamWriter
that I'm done sending request body bytes?The text was updated successfully, but these errors were encountered: