Skip to content

Commit 376e085

Browse files
committed
add surf::ClientExt
1 parent 87a8fc1 commit 376e085

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Diff for: src/client_ext.rs

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use crate::{Client, RequestBuilder};
2+
use std::sync::Arc;
3+
/// Extension trait that adds http request methods
4+
///
5+
/// Blanket implementation provided for all `http_client::HttpClient`s
6+
pub trait ClientExt {
7+
/// Construct a new surf Client from this http client
8+
fn client(&self) -> Client;
9+
10+
/// Builds a `CONNECT` request.
11+
fn connect(&self, path: &str) -> RequestBuilder {
12+
self.client().connect(path)
13+
}
14+
15+
/// Builds a `DELETE` request.
16+
fn delete(&self, path: &str) -> RequestBuilder {
17+
self.client().delete(path)
18+
}
19+
20+
/// Builds a `GET` request.
21+
fn get(&self, path: &str) -> RequestBuilder {
22+
self.client().get(path)
23+
}
24+
25+
/// Builds a `HEAD` request.
26+
fn head(&self, path: &str) -> RequestBuilder {
27+
self.client().head(path)
28+
}
29+
30+
/// Builds an `OPTIONS` request.
31+
fn options(&self, path: &str) -> RequestBuilder {
32+
self.client().options(path)
33+
}
34+
35+
/// Builds a `PATCH` request.
36+
fn patch(&self, path: &str) -> RequestBuilder {
37+
self.client().patch(path)
38+
}
39+
40+
/// Builds a `POST` request.
41+
fn post(&self, path: &str) -> RequestBuilder {
42+
self.client().post(path)
43+
}
44+
45+
/// Builds a `PUT` request.
46+
fn put(&self, path: &str) -> RequestBuilder {
47+
self.client().put(path)
48+
}
49+
50+
/// Builds a `TRACE` request.
51+
fn trace(&self, path: &str) -> RequestBuilder {
52+
self.client().trace(path)
53+
}
54+
}
55+
56+
impl<HC: http_client::HttpClient + Clone> ClientExt for HC {
57+
fn client(&self) -> Client {
58+
Client::with_http_client(Arc::new(self.clone()))
59+
}
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use futures_util::future::BoxFuture;
65+
use http_types::{Body, Error, Request, Response};
66+
67+
#[async_std::test]
68+
async fn with_a_fake_client() -> http_types::Result<()> {
69+
#[derive(Debug, Clone)]
70+
struct MyClient;
71+
impl http_client::HttpClient for MyClient {
72+
fn send(&self, _req: Request) -> BoxFuture<'static, Result<Response, Error>> {
73+
Box::pin(async move {
74+
let mut response = Response::new(200);
75+
response.set_body(Body::from_string(String::from("hello")));
76+
Ok(response)
77+
})
78+
}
79+
}
80+
use super::ClientExt;
81+
let mut response = MyClient.get("http://hello.example").await?;
82+
assert_eq!(response.body_string().await?, "hello");
83+
assert!(response.status().is_success());
84+
85+
Ok(())
86+
}
87+
}

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
#![doc(html_logo_url = "https://yoshuawuyts.com/assets/http-rs/logo-rounded.png")]
7979

8080
mod client;
81+
mod client_ext;
8182
mod request;
8283
mod request_builder;
8384
mod response;
@@ -94,6 +95,7 @@ pub use http_client::HttpClient;
9495
pub use url;
9596

9697
pub use client::Client;
98+
pub use client_ext::ClientExt;
9799
pub use request::Request;
98100
pub use request_builder::RequestBuilder;
99101
pub use response::{DecodeError, Response};

0 commit comments

Comments
 (0)