emilk / ehttp

Minimal Rust HTTP client for both native and WASM
Apache License 2.0
316 stars 30 forks source link

Keep `ureq::Agent` for native clients #4

Open KmolYuan opened 2 years ago

KmolYuan commented 2 years ago

I love the simple API of this crate! It can be useful if ehttp supports saving cookies, such as login function. Currently, the "agent" (ureq::Agent type) is not continuous between each request.

A cross-platform and object-oriented example might look like this:

pub struct Agent {
    #[cfg(not(target_arch = "wasm32"))]
    agent: ureq::Agent,
}

// save the session if we need (compatible syntax)
let session = Agent::new();
session.fetch(req, |r| match r {
    Ok(r) if r.ok => alert("Login successfully!"),
    _ => alert("Login failed!"),
});

// original "fetch" function
fetch(req, |r| match r {
    Ok(r) if r.ok => alert("Login successfully!"),
    _ => alert("Login failed!"),
});
emilk commented 1 year ago

Good point. We should probably make an API like that, wrapping ureq::Agent.