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

Consider adding a progress listener for Callback #26

Closed
vincentjames501 opened this issue Jan 23, 2014 · 13 comments
Closed

Consider adding a progress listener for Callback #26

vincentjames501 opened this issue Jan 23, 2014 · 13 comments

Comments

@vincentjames501
Copy link

One thing that would make this library even better is to add a simple progress callback that updates as a request is made. For POST/PUT/PATCH etc it would be great for this to monitor upload progress, and for GET/DELETE etc it would be great for download progress. Thoughts? This is another side note, but it would be nice to include a default callback class that provides stubbed implementations of the callback interface for those who don't wish to provide functionality for some of the methods.

Future<HttpResponse<JsonNode>> future = Unirest.post("http://httpbin.org/post")
  .header("accept", "application/json")
  .field("param1", "value1")
  .field("param2", "value2")
  .asJsonAsync(new Callback<JsonNode>() {

    public void failed(UnirestException e) {
        System.out.println("The request has failed");
    }

    public void completed(HttpResponse<JsonNode> response) {
         int code = response.getCode();
         Map<String, String> headers = response.getHeaders();
         JsonNode body = response.getBody();
         InputStream rawBody = response.getRawBody();
    }

    public void cancelled() {
        System.out.println("The request has been cancelled");
    }

    public void progress(long current, long total) {
        System.out.println("Progress: " + ((float)current / (float)total));
    }

});
@subnetmarco
Copy link
Member

That's a good idea. I'll start implementing it once I have some time.

@x2on
Copy link

x2on commented Apr 8, 2014

👍

@tcoxon
Copy link
Contributor

tcoxon commented Apr 18, 2014

I've done basically this on my async_progress branch: https://github.com/tcoxon/unirest-java/tree/async_progress

I won't submit a pull request because it's a mega hack and the design needs some real thought. But if you urgently need this feature, you might find it useful.

@subnetmarco
Copy link
Member

@tcoxon Since you have already built something working, if you would considering polishing a little bit I can accept a pull request and distribute it into the next release.

@tcoxon
Copy link
Contributor

tcoxon commented Jun 14, 2014

When are you planning to make your next release? I'm on a very tight schedule at the moment.

@subnetmarco
Copy link
Member

In a couple of weeks. This is a feature that it would be great to have.

tcoxon added a commit to tcoxon/unirest-java that referenced this issue Jun 21, 2014
An AbstractCallback class is provided if the extra method is undesirable.
tcoxon added a commit to tcoxon/unirest-java that referenced this issue Jun 21, 2014
An AbstractCallback class is provided if the extra method is undesirable.
@dewos
Copy link
Contributor

dewos commented Apr 2, 2015

Hello there. Any news about this?

@CrushedPixel
Copy link

Such a feature would be incredibly useful right now.

@navopw
Copy link

navopw commented May 30, 2016

Still need this, waitin' 2 years for this feature 😄

@pierr3
Copy link

pierr3 commented Jun 4, 2016

Would be nice to have this.

@sanguine-my-brother
Copy link

Would be nice to have this, even still in 2017. 😄

@joshgontijo
Copy link

After years of this issue was raised... I'm currently looking into adding this feature to the fork I have. Although is a nice thing to have, I see some caveats with it, like:

  1. The 'total' would only work when you provide a File, since Inputstream can be costly to read twice, or migh not even work.

  2. It only makes sense when you're uploading a file. An API just for file upload would make more sense.

  3. The 'progress' method should not run on the same thread, since it can slow down quite a bit the upload.

ryber added a commit that referenced this issue Feb 24, 2019
make it possible to completely re-init unirest back to original state
@ryber
Copy link
Collaborator

ryber commented Mar 30, 2019

Complete in the release which should be in mvn central shortly:

The monitor gets invokes separately for each file and field (inputstreams lack a file name unless explicitly given one. Also on inputstreams the total bytes is a guestimate.

Unirest.post("http://someplace")
           .field("myFile", new File("/file/somthing.tar")
           .uploadMonitor((fieldName, fileName, bytesWritten, totalBytes) -> {
                // draw a upload progress bar or something
           })

@ryber ryber closed this as completed Mar 30, 2019
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