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

add an optional base_url to Client #215

Closed jbr closed 4 years ago

jbr commented 4 years ago

Encountered a need for this when trying to build a testing library for tide based on surf, but this seems like a generally useful thing when reusing a client.

I'm not sure if this is the right implementation, but it seemed like the least-invasive change to support this functionality

let mut client = surf::client();
client.set_base_url(Url::parse("http://example.com/api/v1")?);

let posts: Vec<Post> = client.get("/posts.json").recv_json().await?;
let users: Vec<User> = client.get("/users.json").recv_json().await?;
goto-bus-stop commented 4 years ago

Yeah I've wanted this too :) is this something that could/should be a middleware? Not sure where we want to draw that line. This apprach seems good too.

jbr commented 4 years ago

@goto-bus-stop I agree it would be nice if it were somehow more deeply integrated into surf, like a middleware, but there's no way to make a Url from a path fragment, and we need to have a Url to build a Request currently. We'd have to change http-types Requests to hold onto a String (or http-types enum Uri { Url(Url), Path(String) } or something like that) so that we can transform it into a Url at a later step, and that seemed like a much more sweeping change.

goto-bus-stop commented 4 years ago

Makes sense. I think I prefer this approach to having an enum like that in http-types :)