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 1 commit
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
87 changes: 87 additions & 0 deletions src/client_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::{Client, RequestBuilder};
use std::sync::Arc;
/// Extension trait that adds http request methods
///
/// Blanket implementation provided for all `http_client::HttpClient`s
pub trait ClientExt {
/// Construct a new surf Client
fn client(&self) -> 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> ClientExt for HC {
fn client(&self) -> Client {
Client::with_http_client(Arc::new(self.clone()))
Copy link
Member

@Fishrock123 Fishrock123 Sep 14, 2020

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?

}
}

#[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::ClientExt;
let mut response = MyClient.get("http://hello.example").await?;
assert_eq!(response.body_string().await?, "hello");
assert!(response.status().is_success());

Ok(())
}
}
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 client_ext;
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 client_ext::ClientExt;
pub use request::Request;
pub use request_builder::RequestBuilder;
pub use response::{DecodeError, Response};
Expand Down