Skip to content

Commit

Permalink
Rename client builders to match their function
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-donat committed Aug 5, 2024
1 parent e05320a commit 8b08db7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 39 deletions.
3 changes: 2 additions & 1 deletion examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ async fn main() {
);
let authenticator = yup_oauth2::ServiceAccountAuthenticator::with_client(
secret,
yup_oauth2::CustomHyperClient::from(client.clone()).with_timeout(Duration::from_secs(10)),
yup_oauth2::CustomHyperClientBuilder::from(client.clone())
.with_timeout(Duration::from_secs(10)),
)
.build()
.await
Expand Down
42 changes: 23 additions & 19 deletions src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::application_default_credentials::{
use crate::authenticator_delegate::{DeviceFlowDelegate, InstalledFlowDelegate};
use crate::authorized_user::{AuthorizedUserFlow, AuthorizedUserSecret};
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
use crate::client::DefaultHyperClient;
use crate::client::DefaultHyperClientBuilder;
use crate::client::{HttpClient, HyperClientBuilder};
use crate::device::DeviceFlow;
use crate::error::Error;
Expand Down Expand Up @@ -207,8 +207,8 @@ impl InstalledFlowAuthenticator {
pub fn builder(
app_secret: ApplicationSecret,
method: InstalledFlowReturnMethod,
) -> AuthenticatorBuilder<DefaultHyperClient, InstalledFlow> {
Self::with_client(app_secret, method, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, InstalledFlow> {
Self::with_client(app_secret, method, DefaultHyperClientBuilder::default())
}

/// Construct a new Authenticator that uses the installed flow and the provided http client.
Expand Down Expand Up @@ -239,8 +239,8 @@ impl DeviceFlowAuthenticator {
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
pub fn builder(
app_secret: ApplicationSecret,
) -> AuthenticatorBuilder<DefaultHyperClient, DeviceFlow> {
Self::with_client(app_secret, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, DeviceFlow> {
Self::with_client(app_secret, DefaultHyperClientBuilder::default())
}

/// Construct a new Authenticator that uses the installed flow and the provided http client.
Expand Down Expand Up @@ -273,8 +273,8 @@ impl ServiceAccountAuthenticator {
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
pub fn builder(
service_account_key: ServiceAccountKey,
) -> AuthenticatorBuilder<DefaultHyperClient, ServiceAccountFlowOpts> {
Self::with_client(service_account_key, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, ServiceAccountFlowOpts> {
Self::with_client(service_account_key, DefaultHyperClientBuilder::default())
}

/// Construct a new Authenticator that uses the installed flow and the provided http client.
Expand Down Expand Up @@ -333,8 +333,8 @@ impl ApplicationDefaultCredentialsAuthenticator {
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
pub async fn builder(
opts: ApplicationDefaultCredentialsFlowOpts,
) -> ApplicationDefaultCredentialsTypes<DefaultHyperClient> {
Self::with_client(opts, DefaultHyperClient::default()).await
) -> ApplicationDefaultCredentialsTypes<DefaultHyperClientBuilder> {
Self::with_client(opts, DefaultHyperClientBuilder::default()).await
}

/// Use the builder pattern to deduce which model of authenticator should be used and allow providing a hyper client
Expand Down Expand Up @@ -389,8 +389,8 @@ impl AuthorizedUserAuthenticator {
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
pub fn builder(
authorized_user_secret: AuthorizedUserSecret,
) -> AuthenticatorBuilder<DefaultHyperClient, AuthorizedUserFlow> {
Self::with_client(authorized_user_secret, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, AuthorizedUserFlow> {
Self::with_client(authorized_user_secret, DefaultHyperClientBuilder::default())
}

/// Construct a new Authenticator that uses the installed flow and the provided http client.
Expand Down Expand Up @@ -426,8 +426,11 @@ impl ExternalAccountAuthenticator {
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
pub fn builder(
external_account_secret: ExternalAccountSecret,
) -> AuthenticatorBuilder<DefaultHyperClient, ExternalAccountFlow> {
Self::with_client(external_account_secret, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, ExternalAccountFlow> {
Self::with_client(
external_account_secret,
DefaultHyperClientBuilder::default(),
)
}

/// Construct a new Authenticator that uses the installed flow and the provided http client.
Expand Down Expand Up @@ -463,8 +466,8 @@ impl AccessTokenAuthenticator {
/// the builder pattern for the authenticator
pub fn builder(
access_token: String,
) -> AuthenticatorBuilder<DefaultHyperClient, AccessTokenFlow> {
Self::with_client(access_token, DefaultHyperClient::default())
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, AccessTokenFlow> {
Self::with_client(access_token, DefaultHyperClientBuilder::default())
}
/// Construct a new Authenticator that uses the installed flow and the provided http client.
/// the client itself is not used
Expand Down Expand Up @@ -499,11 +502,11 @@ impl ServiceAccountImpersonationAuthenticator {
pub fn builder(
authorized_user_secret: AuthorizedUserSecret,
service_account_email: &str,
) -> AuthenticatorBuilder<DefaultHyperClient, ServiceAccountImpersonationFlow> {
) -> AuthenticatorBuilder<DefaultHyperClientBuilder, ServiceAccountImpersonationFlow> {
Self::with_client(
authorized_user_secret,
service_account_email,
DefaultHyperClient::default(),
DefaultHyperClientBuilder::default(),
)
}

Expand All @@ -525,7 +528,7 @@ impl ServiceAccountImpersonationAuthenticator {
/// # async fn foo() {
/// # let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http::<String>();
/// # let app_secret = yup_oauth2::read_application_secret("/tmp/foo").await.unwrap();
/// let authenticator = yup_oauth2::DeviceFlowAuthenticator::with_client(app_secret, yup_oauth2::CustomHyperClient::from(client))
/// let authenticator = yup_oauth2::DeviceFlowAuthenticator::with_client(app_secret, yup_oauth2::CustomHyperClientBuilder::from(client))
/// .persist_tokens_to_disk("/tmp/tokenfile.json")
/// .build()
/// .await
Expand Down Expand Up @@ -960,6 +963,7 @@ mod tests {
fn ensure_send_sync() {
use super::*;
fn is_send_sync<T: Send + Sync>() {}
is_send_sync::<Authenticator<<DefaultHyperClient as HyperClientBuilder>::Connector>>()
is_send_sync::<Authenticator<<DefaultHyperClientBuilder as HyperClientBuilder>::Connector>>(
)
}
}
12 changes: 6 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ pub(crate) trait SendRequest {
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
#[derive(Default)]
pub struct DefaultHyperClient {
pub struct DefaultHyperClientBuilder {
timeout: Option<Duration>,
}

#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
impl DefaultHyperClient {
impl DefaultHyperClientBuilder {
/// Set the duration after which a request times out
pub fn with_timeout(mut self, timeout: Duration) -> Self {
self.timeout = Some(timeout);
Expand All @@ -128,7 +128,7 @@ impl DefaultHyperClient {

#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))))]
impl HyperClientBuilder for DefaultHyperClient {
impl HyperClientBuilder for DefaultHyperClientBuilder {
#[cfg(feature = "hyper-rustls")]
type Connector =
hyper_rustls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>;
Expand Down Expand Up @@ -162,15 +162,15 @@ impl HyperClientBuilder for DefaultHyperClient {

/// Intended for using an existing hyper client with `yup-oauth2`. Instantiate
/// with [`CustomHyperClient::from`]
pub struct CustomHyperClient<C>
pub struct CustomHyperClientBuilder<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
client: HttpClient<C>,
timeout: Option<Duration>,
}

impl<C> From<LegacyClient<C>> for CustomHyperClient<C>
impl<C> From<LegacyClient<C>> for CustomHyperClientBuilder<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
Expand All @@ -182,7 +182,7 @@ where
}
}

impl<C> HyperClientBuilder for CustomHyperClient<C>
impl<C> HyperClientBuilder for CustomHyperClientBuilder<C>
where
C: Connect + Clone + Send + Sync + 'static,
{
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ pub use crate::authenticator::ServiceAccountAuthenticator;
pub use crate::authenticator::AccessTokenAuthenticator;

#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
pub use crate::client::DefaultHyperClient;
pub use crate::client::{CustomHyperClient, HttpClient, HyperClientBuilder};
pub use crate::client::DefaultHyperClientBuilder;
pub use crate::client::{CustomHyperClientBuilder, HttpClient, HyperClientBuilder};

#[doc(inline)]
pub use crate::authenticator::{
Expand Down
24 changes: 13 additions & 11 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use yup_oauth2::{
authenticator::DefaultAuthenticator,
authenticator_delegate::{DeviceAuthResponse, DeviceFlowDelegate, InstalledFlowDelegate},
client::{DefaultHyperClient, HttpClient, HyperClientBuilder},
client::{DefaultHyperClientBuilder, HttpClient, HyperClientBuilder},
error::SendError,
AccessTokenAuthenticator, ApplicationDefaultCredentialsAuthenticator,
ApplicationDefaultCredentialsFlowOpts, ApplicationSecret, DeviceFlowAuthenticator,
Expand Down Expand Up @@ -57,7 +57,7 @@ async fn create_device_flow_auth_with_timeout(
}
}

let mut client = DefaultHyperClient::default();
let mut client = DefaultHyperClientBuilder::default();
if let Some(duration) = timeout {
client = client.with_timeout(duration);
}
Expand Down Expand Up @@ -233,7 +233,7 @@ async fn create_installed_flow_auth(
"client_secret": "iuMPN6Ne1PD7cos29Tk9rlqH",
"redirect_uris": ["urn:ietf:wg:oauth:2.0:oob","http://localhost"],
});
struct FD(HttpClient<<DefaultHyperClient as HyperClientBuilder>::Connector>);
struct FD(HttpClient<<DefaultHyperClientBuilder as HyperClientBuilder>::Connector>);
impl InstalledFlowDelegate for FD {
/// Depending on need_code, return the pre-set code or send the code to the server at
/// the redirect_uri given in the url.
Expand Down Expand Up @@ -276,7 +276,7 @@ async fn create_installed_flow_auth(
}
}

let client = DefaultHyperClient::default()
let client = DefaultHyperClientBuilder::default()
.build_hyper_client()
.expect("Hyper client to be built");
let mut builder = InstalledFlowAuthenticator::with_client(app_secret, method, client.clone())
Expand Down Expand Up @@ -398,7 +398,7 @@ async fn create_service_account_auth(server: &Server) -> DefaultAuthenticator {

ServiceAccountAuthenticator::with_client(
key,
DefaultHyperClient::default()
DefaultHyperClientBuilder::default()
.build_hyper_client()
.expect("Hyper client to be built"),
)
Expand Down Expand Up @@ -733,7 +733,7 @@ async fn test_default_application_credentials_from_metadata_server() {
};
let authenticator = match ApplicationDefaultCredentialsAuthenticator::with_client(
opts,
DefaultHyperClient::default()
DefaultHyperClientBuilder::default()
.build_hyper_client()
.expect("Hyper client to be built"),
)
Expand All @@ -754,11 +754,13 @@ async fn test_default_application_credentials_from_metadata_server() {

#[tokio::test]
async fn test_token() {
let authenticator =
AccessTokenAuthenticator::with_client("0815".to_string(), DefaultHyperClient::default())
.build()
.await
.unwrap();
let authenticator = AccessTokenAuthenticator::with_client(
"0815".to_string(),
DefaultHyperClientBuilder::default(),
)
.build()
.await
.unwrap();
let access_token = authenticator
.token(&["https://googleapis.com/some/scope"])
.await
Expand Down

0 comments on commit 8b08db7

Please sign in to comment.