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

add surf::Methods #229

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")]

mod client;
mod methods;
mod request;
mod request_builder;
mod response;
Expand All @@ -94,6 +95,7 @@ pub use http_client::HttpClient;
pub use url;

pub use client::Client;
pub use methods::Methods;
pub use request::Request;
pub use request_builder::RequestBuilder;
pub use response::{DecodeError, Response};
Expand Down
87 changes: 87 additions & 0 deletions src/methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::RequestBuilder;

/// Trait that adds http request methods.
Copy link
Member

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.

Copy link
Member Author

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

///
/// Blanket implementation provided for all `http_client::HttpClient`s
pub trait Methods {
/// Construct a new surf Client
fn client(&self) -> crate::Client;

/// Builds a `CONNECT` request.
fn connect(&self, path: &str) -> RequestBuilder {
self.client().connect(path)
}

/// Builds a `DELETE` request.
fn delete(&self, path: &str) -> RequestBuilder {
self.client().delete(path)
}

/// Builds a `GET` request.
fn get(&self, path: &str) -> RequestBuilder {
self.client().get(path)
}

/// Builds a `HEAD` request.
fn head(&self, path: &str) -> RequestBuilder {
self.client().head(path)
}

/// Builds an `OPTIONS` request.
fn options(&self, path: &str) -> RequestBuilder {
self.client().options(path)
}

/// Builds a `PATCH` request.
fn patch(&self, path: &str) -> RequestBuilder {
self.client().patch(path)
}

/// Builds a `POST` request.
fn post(&self, path: &str) -> RequestBuilder {
self.client().post(path)
}

/// Builds a `PUT` request.
fn put(&self, path: &str) -> RequestBuilder {
self.client().put(path)
}

/// Builds a `TRACE` request.
fn trace(&self, path: &str) -> RequestBuilder {
self.client().trace(path)
}
}

impl<HC: http_client::HttpClient + Clone> Methods for HC {
fn client(&self) -> crate::Client {
crate::Client::with_http_client(std::sync::Arc::new(self.clone()))
}
}

#[cfg(test)]
mod tests {
use futures_util::future::BoxFuture;
use http_types::{Body, Error, Request, Response};

#[async_std::test]
async fn with_a_fake_client() -> http_types::Result<()> {
#[derive(Debug, Clone)]
struct MyClient;
impl http_client::HttpClient for MyClient {
fn send(&self, _req: Request) -> BoxFuture<'static, Result<Response, Error>> {
Box::pin(async move {
let mut response = Response::new(200);
response.set_body(Body::from_string(String::from("hello")));
Ok(response)
})
}
}
use super::Methods;
let mut response = MyClient.get("http://hello.example").await?;
assert_eq!(response.body_string().await?, "hello");
assert!(response.status().is_success());

Ok(())
}
}