From 08e733b08ff3d473be87d6c48ac5253cfc215071 Mon Sep 17 00:00:00 2001 From: Sebastian Rollen Date: Thu, 4 Nov 2021 13:01:58 -0400 Subject: [PATCH] refactor: make api more flexible --- src/client.rs | 38 ++++++++++------------ src/lib.rs | 4 +++ tests/integration/authentication/basic.rs | 2 +- tests/integration/authentication/bearer.rs | 2 +- tests/integration/authentication/query.rs | 2 +- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/client.rs b/src/client.rs index 2635e4a..9a070ff 100644 --- a/src/client.rs +++ b/src/client.rs @@ -7,10 +7,10 @@ use std::borrow::Cow; use std::sync::Arc; #[derive(Clone)] -enum Authentication { - Bearer(String), - Basic(String, String), - Query(Vec<(String, String)>), +enum Authentication<'a> { + Bearer(Cow<'a, str>), + Basic(Cow<'a, str>, Cow<'a, str>), + Query(Vec<(Cow<'a, str>, Cow<'a, str>)>), } /// The main client used for making requests. @@ -21,7 +21,7 @@ enum Authentication { pub struct Client<'a> { inner: Arc, base_url: Cow<'a, str>, - auth: Option, + auth: Option>, } impl<'a> Client<'a> { @@ -37,19 +37,23 @@ impl<'a> Client<'a> { } /// Enable bearer authentication for the client - pub fn bearer_auth(mut self, token: String) -> Self { - self.auth = Some(Authentication::Bearer(token)); + pub fn bearer_auth>>(mut self, token: S) -> Self { + self.auth = Some(Authentication::Bearer(token.into())); self } /// Enable basic authentication for the client - pub fn basic_auth(mut self, user: String, pass: String) -> Self { - self.auth = Some(Authentication::Basic(user, pass)); + pub fn basic_auth>>(mut self, user: S, pass: S) -> Self { + self.auth = Some(Authentication::Basic(user.into(), pass.into())); self } /// Enable query authentication for the client - pub fn query_auth(mut self, pairs: Vec<(String, String)>) -> Self { + pub fn query_auth>>(mut self, pairs: Vec<(S, S)>) -> Self { + let pairs = pairs + .into_iter() + .map(|(k, v)| (k.into(), v.into())) + .collect(); self.auth = Some(Authentication::Query(pairs)); self } @@ -94,17 +98,9 @@ impl<'a> Client<'a> { } /// Send a single `Request` - pub fn send(&'a self, request: &R) -> impl Future> + 'a - where - R::Response: 'a, - { - let req = self.format_request(request); - if let Err(e) = req { - return future::Either::Left(future::ready(Err(e))); - }; - let req = req.unwrap(); - - future::Either::Right(self.send_raw(req)) + pub async fn send(&self, request: &R) -> Result { + let req = self.format_request(request)?; + self.send_raw(req).await } /// Send multiple `Request`s, returing a stream of results diff --git a/src/lib.rs b/src/lib.rs index 734b662..8a2352c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +//! rest-client is a library for building strongly typed REST clients, with built-in capabilites +//! for authentication, various request and response types and pagination. +//! +//! Inspired heavily by [ring-api](https://github.com/H2CO3/ring_api) mod client; mod error; mod pagination; diff --git a/tests/integration/authentication/basic.rs b/tests/integration/authentication/basic.rs index c3549d8..2fd2f02 100644 --- a/tests/integration/authentication/basic.rs +++ b/tests/integration/authentication/basic.rs @@ -18,7 +18,7 @@ impl Request for EmptyHello { async fn basic_auth() { let server = MockServer::start().await; let uri = server.uri(); - let client = Client::new(&uri).basic_auth("user".into(), "pass".into()); + let client = Client::new(&uri).basic_auth("user", "pass"); Mock::given(method("GET")) .and(path("/hello")) diff --git a/tests/integration/authentication/bearer.rs b/tests/integration/authentication/bearer.rs index a36cc71..285eab6 100644 --- a/tests/integration/authentication/bearer.rs +++ b/tests/integration/authentication/bearer.rs @@ -18,7 +18,7 @@ impl Request for EmptyHello { async fn bearer_auth() { let server = MockServer::start().await; let uri = server.uri(); - let client = Client::new(&uri).bearer_auth("PASSWORD".into()); + let client = Client::new(&uri).bearer_auth("PASSWORD"); Mock::given(method("GET")) .and(path("/hello")) diff --git a/tests/integration/authentication/query.rs b/tests/integration/authentication/query.rs index 2fc258e..22a6ab2 100644 --- a/tests/integration/authentication/query.rs +++ b/tests/integration/authentication/query.rs @@ -18,7 +18,7 @@ impl Request for EmptyHello { async fn query_auth() { let server = MockServer::start().await; let uri = server.uri(); - let auth = vec![("key".into(), "k".into()), ("secret".into(), "s".into())]; + let auth = vec![("key", "k"), ("secret", "s")]; let client = Client::new(&uri).query_auth(auth); Mock::given(method("GET"))