http-rs / surf

Fast and friendly HTTP client framework for async Rust
https://docs.rs/surf
Apache License 2.0
1.45k stars 119 forks source link

Callback for download requests #216

Open sandmor opened 4 years ago

sandmor commented 4 years ago

So I want to download a very big file, but with the current API I can only store it in memory and then write to a file, that is a HUGE problem, also I want to show a progress bar or something like that, so I figure out a manner to make this possible:

struct Manager;

impl Manager {
    pub fn new() -> Manager {
        Manager {}
   }
}

impl surf::DownloadManager for Manager {
    async fn process_headers(&self, headers: HEADERS) {
        // Process content-length, content-disposition, etc
    }

    async fn process_chunk(&self, chunk: &[u8]) {
        // Write chunk to disk and report download progress...
    }
}

async fn get() -> Result<(), surf::Exception> {
    let mngr = Manager::new();
    let mut res = surf::callback_request::get("https://httpbin.org/get", mngr).await?;
    Ok(())
}

What do you think? for now, I am using curl directly

Fishrock123 commented 4 years ago

I think what you want is a custom struct that impls AsyncBufReader and can compare the amount of bytes being read to e.g. a Content-Length header or stat'd file size.

sandmor commented 4 years ago

Yeah, I don't see I can pass a reader to surf::Request::body, Are you thinking on put that more... explicitly on the docs?

Fishrock123 commented 4 years ago

You want the Response, and the Response implements AsyncBufRead, which is what I was referring to (mistakenly mis-spelt).

sandmor commented 4 years ago

Ok, thank you and sorry for the issue