From 8b08db7a302501df4895d5b399b32a0a64f301f9 Mon Sep 17 00:00:00 2001 From: threema-donat <129288638+threema-donat@users.noreply.github.com> Date: Mon, 5 Aug 2024 12:15:25 +0200 Subject: [PATCH] Rename client builders to match their function --- examples/custom_client.rs | 3 ++- src/authenticator.rs | 42 +++++++++++++++++++++------------------ src/client.rs | 12 +++++------ src/lib.rs | 4 ++-- tests/tests.rs | 24 ++++++++++++---------- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/examples/custom_client.rs b/examples/custom_client.rs index 1d39ba34..441ff1a5 100644 --- a/examples/custom_client.rs +++ b/examples/custom_client.rs @@ -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 diff --git a/src/authenticator.rs b/src/authenticator.rs index 935d5770..4e153b4c 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -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; @@ -207,8 +207,8 @@ impl InstalledFlowAuthenticator { pub fn builder( app_secret: ApplicationSecret, method: InstalledFlowReturnMethod, - ) -> AuthenticatorBuilder { - Self::with_client(app_secret, method, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + Self::with_client(app_secret, method, DefaultHyperClientBuilder::default()) } /// Construct a new Authenticator that uses the installed flow and the provided http client. @@ -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 { - Self::with_client(app_secret, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + Self::with_client(app_secret, DefaultHyperClientBuilder::default()) } /// Construct a new Authenticator that uses the installed flow and the provided http client. @@ -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 { - Self::with_client(service_account_key, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + Self::with_client(service_account_key, DefaultHyperClientBuilder::default()) } /// Construct a new Authenticator that uses the installed flow and the provided http client. @@ -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 { - Self::with_client(opts, DefaultHyperClient::default()).await + ) -> ApplicationDefaultCredentialsTypes { + 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 @@ -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 { - Self::with_client(authorized_user_secret, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + Self::with_client(authorized_user_secret, DefaultHyperClientBuilder::default()) } /// Construct a new Authenticator that uses the installed flow and the provided http client. @@ -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 { - Self::with_client(external_account_secret, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + Self::with_client( + external_account_secret, + DefaultHyperClientBuilder::default(), + ) } /// Construct a new Authenticator that uses the installed flow and the provided http client. @@ -463,8 +466,8 @@ impl AccessTokenAuthenticator { /// the builder pattern for the authenticator pub fn builder( access_token: String, - ) -> AuthenticatorBuilder { - Self::with_client(access_token, DefaultHyperClient::default()) + ) -> AuthenticatorBuilder { + 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 @@ -499,11 +502,11 @@ impl ServiceAccountImpersonationAuthenticator { pub fn builder( authorized_user_secret: AuthorizedUserSecret, service_account_email: &str, - ) -> AuthenticatorBuilder { + ) -> AuthenticatorBuilder { Self::with_client( authorized_user_secret, service_account_email, - DefaultHyperClient::default(), + DefaultHyperClientBuilder::default(), ) } @@ -525,7 +528,7 @@ impl ServiceAccountImpersonationAuthenticator { /// # async fn foo() { /// # let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build_http::(); /// # 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 @@ -960,6 +963,7 @@ mod tests { fn ensure_send_sync() { use super::*; fn is_send_sync() {} - is_send_sync::::Connector>>() + is_send_sync::::Connector>>( + ) } } diff --git a/src/client.rs b/src/client.rs index fb3f3cfb..e07db45b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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, } #[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); @@ -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; @@ -162,7 +162,7 @@ impl HyperClientBuilder for DefaultHyperClient { /// Intended for using an existing hyper client with `yup-oauth2`. Instantiate /// with [`CustomHyperClient::from`] -pub struct CustomHyperClient +pub struct CustomHyperClientBuilder where C: Connect + Clone + Send + Sync + 'static, { @@ -170,7 +170,7 @@ where timeout: Option, } -impl From> for CustomHyperClient +impl From> for CustomHyperClientBuilder where C: Connect + Clone + Send + Sync + 'static, { @@ -182,7 +182,7 @@ where } } -impl HyperClientBuilder for CustomHyperClient +impl HyperClientBuilder for CustomHyperClientBuilder where C: Connect + Clone + Send + Sync + 'static, { diff --git a/src/lib.rs b/src/lib.rs index 9157f01a..46b940b4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::{ diff --git a/tests/tests.rs b/tests/tests.rs index f8160fc0..7766dff6 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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, @@ -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); } @@ -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<::Connector>); + struct FD(HttpClient<::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. @@ -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()) @@ -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"), ) @@ -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"), ) @@ -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