-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
Upload progress recipe #1528
Upload progress recipe #1528
Conversation
private final MediaType contentType; | ||
private final File file; | ||
|
||
public ProgressRequestBody(MediaType contentType, File file, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change this so that it wraps another RequestBody? That way the caller can do whatever they want: upload data being generated, a file, or whatever.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to, but from what I've gathered I can't get the upload data out of the request body, so it would have to look like this, file
being passed to the RequestBody
and again to the ProgressRequestBody
so it can be accessed for contentLength()
etc.
File file = new File("website/static/logo-square.png");
RequestBody requestBody = RequestBody.create(MEDIA_TYPE_PNG, file);
Request request = new Request.Builder()
.header("Authorization", "Client-ID " + IMGUR_CLIENT_ID)
.url("https://api.imgur.com/3/image")
.post(new ProgressRequestBody(requestBody, file, progressListener)) // Here file is passed a second time
.build();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can, but you need to be a bit sneaky. You'd just need to call delegate.writeTo(buffer(progressSink(sink)))
in your own writeTo
method.
I think this is pretty awesome. Consider building on the |
This is great! |
The only difference is The reason why I used the imgur example was that the README is below 2KB which means there's no real progress to watch, but I guess it should just serve as an example? |
super.write(source, byteCount); | ||
totalBytesWritten += byteCount; | ||
progressListener.update(totalBytesWritten, contentLength(), | ||
totalBytesWritten == contentLength()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(You can look at the PostStreaming example to see an unknown content length in practice.)
@aried3r follow me on the PostStreaming thing? |
Sorry, been busy. I'll try to get this done during the weekend. Stay tuned! ;) |
I'm going to close this for now. If you'd like to revisit, please do! |
Does the code found here sticks to your recommendations @swankjesse ? |
Let's have it, @swankjesse! ;)
There's a few things I'm not quite sure about how to handle (basically the whole
writeTo()
method), so I'm awaiting your comments. But I hope I'll stay below 10 this time :p