Skip to content

Commit

Permalink
Let user configure the http pipeline (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Mariette committed Jul 18, 2023
1 parent 0f7feb8 commit 7275d07
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
37 changes: 31 additions & 6 deletions graph-http/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::blocking::BlockingClient;
use crate::traits::ODataQuery;

use crate::internal::{ExponentialBackoffRetryPolicy, ThrottleRetryPolicy, TransportPolicy};
use crate::http_pipeline::HttpPipelinePolicy;
use crate::internal::{ExponentialBackoffRetryPolicy, ThrottleRetryPolicy, TransportPolicy};
use graph_error::GraphResult;
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, USER_AGENT};
use reqwest::redirect::Policy;
Expand All @@ -24,6 +24,7 @@ struct ClientConfiguration {
connection_verbose: bool,
https_only: bool,
min_tls_version: Version,
pipeline: Vec<Arc<dyn HttpPipelinePolicy + Send + Sync>>,
}

impl ClientConfiguration {
Expand All @@ -42,6 +43,7 @@ impl ClientConfiguration {
/// TLS 1.2 required to support all features in Microsoft Graph
/// See [Reliability and Support](https://learn.microsoft.com/en-us/graph/best-practices-concept#reliability-and-support)
min_tls_version: Version::TLS_1_2,
pipeline: vec![],
}
}
}
Expand Down Expand Up @@ -136,6 +138,30 @@ impl GraphClientConfiguration {
self
}

pub fn pipeline(
mut self,
pipeline: Vec<Arc<dyn HttpPipelinePolicy + Send + Sync>>,
) -> GraphClientConfiguration {
self.config.pipeline = pipeline;
self
}

pub fn add_pipeline_policy(
mut self,
policy: Arc<dyn HttpPipelinePolicy + Send + Sync>,
) -> GraphClientConfiguration {
self.config.pipeline.push(policy);
self
}

pub fn add_default_pipeline_retries_policies(mut self) -> GraphClientConfiguration {
self.config
.pipeline
.push(Arc::new(ExponentialBackoffRetryPolicy::default()));
self.config.pipeline.push(Arc::new(ThrottleRetryPolicy {}));
self
}

pub fn build(self) -> Client {
let config = self.clone();
let headers = self.config.headers.clone();
Expand All @@ -155,16 +181,15 @@ impl GraphClientConfiguration {
builder = builder.connect_timeout(connect_timeout);
}

let mut pipeline = self.config.pipeline;
pipeline.push(Arc::new(TransportPolicy {}));

Client {
access_token: self.config.access_token.unwrap_or_default(),
inner: builder.build().unwrap(),
headers,
builder: config,
pipeline: vec![
Arc::new(ExponentialBackoffRetryPolicy::default()),
Arc::new(ThrottleRetryPolicy {}),
Arc::new(TransportPolicy {}),
],
pipeline,
}
}

Expand Down
6 changes: 3 additions & 3 deletions graph-http/src/http_pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct ExponentialBackoffRetryPolicy {
}

impl ExponentialBackoffRetryPolicy {
pub(crate) fn get_exponential_backoff_with_max_retries(&self) -> ExponentialBackoffWithMaxRetries {
fn get_exponential_backoff_with_max_retries(&self) -> ExponentialBackoffWithMaxRetries {
ExponentialBackoffWithMaxRetries {
exp: ExponentialBackoffBuilder::new()
.with_initial_interval(self.initial_interval)
Expand All @@ -86,7 +86,7 @@ impl Default for ExponentialBackoffRetryPolicy {
multiplier: 1.5,
max_interval: Duration::from_secs(450),
max_elapsed_time: Some(Duration::from_secs(450)),
max_retries: 10,
max_retries: 5,
}
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ impl HttpPipelinePolicy for ExponentialBackoffRetryPolicy {
pub struct ThrottleRetryPolicy {}

impl ThrottleRetryPolicy {
pub(crate) fn get_retries_zero_backoff(&self) -> ExponentialBackoff {
fn get_retries_zero_backoff(&self) -> ExponentialBackoff {
ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::ZERO)
.with_multiplier(0.0)
Expand Down
1 change: 1 addition & 0 deletions graph-http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ pub mod api_impl {
pub use crate::resource_identifier::{ResourceConfig, ResourceIdentifier};
pub use crate::traits::{BodyExt, ODataQuery};
pub use crate::upload_session::UploadSession;
pub use crate::http_pipeline::{HttpPipelinePolicy, ThrottleRetryPolicy, ExponentialBackoffRetryPolicy};
pub use graph_error::{GraphFailure, GraphResult};
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ pub mod http {
ODataMetadataLink, ODataNextLink, ODataQuery, ResponseBlockingExt, ResponseExt,
UploadSessionLink,
};
pub use graph_http::api_impl::{HttpPipelinePolicy, ThrottleRetryPolicy, ExponentialBackoffRetryPolicy};
pub use reqwest::tls::Version;
pub use reqwest::{Body, Method};

Expand Down

0 comments on commit 7275d07

Please sign in to comment.