-
Notifications
You must be signed in to change notification settings - Fork 121
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
add surf::Methods #229
add surf::Methods #229
Conversation
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'm confused. Why does this need to exist? These methods already exist on Client
: https://docs.rs/surf/2.0.0-alpha.5/surf/struct.Client.html
src/client_ext.rs
Outdated
|
||
impl<HC: http_client::HttpClient + Clone> ClientExt for HC { | ||
fn client(&self) -> Client { | ||
Client::with_http_client(Arc::new(self.clone())) |
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.
It looks like this often may make something like Arc<Arc<dyn HttpClient>>
. Maybe the signature of Client::with_http_client()
needs to be improved.
Edit: Now that I look again I don't see it, maybe I was mistaken?
Turns out this adds these to any arbitrary HttpClient. I'm not quite sure what I think of it.
Maybe this can be feature flagged while we figure out how we like it? Is that acceptable? |
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'm quite sold that this is a good idea. But what I'm less sure on is the name + location of the trait. We already have a surf::Client
at the root; surf::ClientExt
doesn't extend surf::Client
so I can see that cause issues.
Crates like anyhow
provide extension traits such as Context
that are based on functionality rather than the *Ext
scheme, and I feel we could perhaps do something similar. But really quite unsure about the naming...
|
Agreed on the name issue. |
@@ -0,0 +1,87 @@ | |||
use crate::RequestBuilder; | |||
|
|||
/// Trait that adds http request methods. |
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 think this could use better documentation about what exactly this is. I think that is my primary concern at this point, it may be confusing for someone to see a trait named "Methods" in the docs without more exposition.
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.
Agreed, but not sure what to write. Suggestions appreciated
Yeah, I think it might? Been re-revisiting this PR over the past few days, and that's def the feel I get. Which constraints are we designing around?Something I've still been thinking of is whether the This trait attempts to overcome two hurdles that are in converting Tide server -> Surf client:
A solution to 1. could be to make it so each A solution for 2 would be for Alternate API proposalPutting these two together, we could imagine a new constructor method that could look like this: let app = tide::new();
let client = Client::connect_with("https://example.com", app);
client.get("/").recv_string()?; This is slightly longer than the let mut app = tide::new();
app.subdomain("blog").at("/").get(|_| async { Ok("welcome to my blog") });
app.at("/index.html").get(|_| async { Ok("welcome to my site") };
let blog_client = Client::connect_with("https://blog.example.com", app.clone());
assert_eq!(blog_client.get("/").recv_string()?, "welcome to my blog".into()));
let client = Client::connect_with("https://example.com", app);
assert_eq!(client.get("/index.html").recv_string()?, "welcome to my site".into())); Not being able to control the domain name in the client may actually also become an issue when testing other url-related things. So we should probably consider that as part of the design for a testing setup. |
I'm really not sure where you are getting this from... neither this PR or that Tide PR do that directly in any way. That is functionality that only just so happens to also exist but is not really related? I do not agree with making |
I see this PR in particular as not really being fundamental in any way - it is purely ergonomics. With http-rs/tide#697 you could just as well write this: let server = tide::new();
let client = surf::Client::with_http_client(server);
client.get("http://example.com/").await?; |
Exactly what @Fishrock123 said — this PR isn't all that important to the testing thing. I thought it would be convenient for "anyone who wants to add the full list of http verbs without having to copy and paste them" but maybe that's an infrequent enough problem that we should just close this issue and offer a tide-specific testing extension that does this specifically for tide::Servers. Unless someone objects, I'll write that up and close this PR |
This PR adds a trait called
surf::Methods
which adds http methods directly to any http_client::HttpClient when the trait is in scope. In concert with http-rs/tide#697, this means that a minimal example for testing a tide app might be:This also just saves anyone else from having to type out all of the verbs — anyone that wants to implement this trait just needs to provide a
fn client() -> Client
and they get all of the one-off builders.I'm not sure if this should be the only thing in a
surf::prelude::*
, but that makes sense to me as an extension trait