Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Merge pull request #4 from Overmuse/SR/flexibility
Browse files Browse the repository at this point in the history
refactor: make api more flexible
  • Loading branch information
SebRollen authored Nov 4, 2021
2 parents 7880332 + 08e733b commit 9f90060
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
38 changes: 17 additions & 21 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -21,7 +21,7 @@ enum Authentication {
pub struct Client<'a> {
inner: Arc<ReqwestClient>,
base_url: Cow<'a, str>,
auth: Option<Authentication>,
auth: Option<Authentication<'a>>,
}

impl<'a> Client<'a> {
Expand All @@ -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<S: Into<Cow<'a, str>>>(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<S: Into<Cow<'a, str>>>(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<S: Into<Cow<'a, str>>>(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
}
Expand Down Expand Up @@ -94,17 +98,9 @@ impl<'a> Client<'a> {
}

/// Send a single `Request`
pub fn send<R: Request>(&'a self, request: &R) -> impl Future<Output = Result<R::Response>> + '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<R: Request>(&self, request: &R) -> Result<R::Response> {
let req = self.format_request(request)?;
self.send_raw(req).await
}

/// Send multiple `Request`s, returing a stream of results
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/authentication/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/authentication/bearer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/authentication/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit 9f90060

Please sign in to comment.