Skip to content

Commit

Permalink
Add timeout to hyper client
Browse files Browse the repository at this point in the history
  • Loading branch information
threema-donat committed Jul 3, 2024
1 parent d62c641 commit 82ce134
Show file tree
Hide file tree
Showing 15 changed files with 334 additions and 152 deletions.
15 changes: 10 additions & 5 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
//!
//! It is also a better use of resources (memory, sockets, etc.)
use std::time::Duration;

use hyper_util::client::legacy::connect::Connect;
use yup_oauth2::authenticator::HyperClientBuilder;

async fn r#use<C>(
client: hyper_util::client::legacy::Client<C, String>,
Expand Down Expand Up @@ -43,11 +46,13 @@ async fn main() {
.enable_http2()
.build(),
);
let authenticator =
yup_oauth2::ServiceAccountAuthenticator::with_client(secret, client.clone())
.build()
.await
.expect("could not create an authenticator");
let authenticator = yup_oauth2::ServiceAccountAuthenticator::with_client(
secret,
yup_oauth2::CustomHyperClient::from(client.clone()).with_timeout(Duration::from_secs(10)),
)
.build()
.await
.expect("could not create an authenticator");
r#use(client, authenticator)
.await
.expect("use is successful!");
Expand Down
7 changes: 3 additions & 4 deletions src/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! The intention behind this is that if two services using the
//! same refresh token then each service will invalitate the
//! access token of the other service by generating a new token.
use crate::client::SendRequest;
use crate::error::Error;
use crate::types::TokenInfo;
use hyper_util::client::legacy::connect::Connect;

/// the flow for the access token authenticator
pub struct AccessTokenFlow {
Expand All @@ -16,14 +16,13 @@ pub struct AccessTokenFlow {

impl AccessTokenFlow {
/// just return the access token
pub(crate) async fn token<C, B, T>(
pub(crate) async fn token<T>(
&self,
_hyper_client: &hyper_util::client::legacy::Client<C, B>,
_hyper_client: &impl SendRequest,
_scopes: &[T],
) -> Result<TokenInfo, Error>
where
T: AsRef<str>,
C: Connect + Clone + Send + Sync + 'static,
{
Ok(TokenInfo {
access_token: Some(self.access_token.clone()),
Expand Down
7 changes: 3 additions & 4 deletions src/application_default_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::client::SendRequest;
use crate::error::Error;
use crate::types::TokenInfo;
use http_body_util::BodyExt;
use hyper_util::client::legacy::connect::Connect;

/// Provide options for the Application Default Credential Flow, mostly used for testing
#[derive(Default, Clone, Debug)]
Expand All @@ -20,14 +20,13 @@ impl ApplicationDefaultCredentialsFlow {
ApplicationDefaultCredentialsFlow { metadata_url }
}

pub(crate) async fn token<C, T>(
pub(crate) async fn token<T>(
&self,
hyper_client: &hyper_util::client::legacy::Client<C, String>,
hyper_client: &impl SendRequest,
scopes: &[T],
) -> Result<TokenInfo, Error>
where
T: AsRef<str>,
C: Connect + Clone + Send + Sync + 'static,
{
let scope = crate::helper::join(scopes, ",");
let token_uri = format!("{}?scopes={}", self.metadata_url, scope);
Expand Down
Loading

0 comments on commit 82ce134

Please sign in to comment.