Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting an http timeout, and arbitrary options #277

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions ic-agent/src/agent/http_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#![cfg(feature = "reqwest")]

use crate::{agent::agent_error::HttpErrorPayload, ic_types::Principal, AgentError, RequestId};
use reqwest::Method;
use std::{future::Future, pin::Pin, sync::Arc};
use reqwest::{ClientBuilder, Method};
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};

/// Implemented by the Agent environment to cache and update an HTTP Auth password.
/// It returns a tuple of `(username, password)`.
Expand All @@ -30,6 +30,25 @@ pub struct ReqwestHttpReplicaV2Transport {

impl ReqwestHttpReplicaV2Transport {
pub fn create<U: Into<String>>(url: U) -> Result<Self, AgentError> {
Self::create_with_modified_http_options(url, |cb| cb)
}

pub fn create_with_timeout<U: Into<String>>(
url: U,
http_timeout: Duration,
) -> Result<Self, AgentError> {
Self::create_with_modified_http_options(url, |cb| cb.timeout(http_timeout))
}

/// This method allows you to create a `ReqwestHttpReplicaV2Transport`
/// with arbitrary http options by using `Reqwest::ClientBuilder`.
pub fn create_with_modified_http_options<
U: Into<String>,
F: FnOnce(ClientBuilder) -> ClientBuilder,
>(
url: U,
modify_http_options: F,
) -> Result<Self, AgentError> {
let mut tls_config = rustls::ClientConfig::new();

// Advertise support for HTTP/2
Expand All @@ -45,10 +64,11 @@ impl ReqwestHttpReplicaV2Transport {
url: reqwest::Url::parse(&url)
.and_then(|url| url.join("api/v2/"))
.map_err(|_| AgentError::InvalidReplicaUrl(url.clone()))?,
client: reqwest::Client::builder()
.use_preconfigured_tls(tls_config)
.build()
.expect("Could not create HTTP client."),
client: modify_http_options(
reqwest::Client::builder().use_preconfigured_tls(tls_config),
)
.build()
.expect("Could not create HTTP client."),
password_manager: None,
})
}
Expand Down