rpc: genericize client constructors
This commit is contained in:
committed by
Trent Nelson
parent
4213ece7bd
commit
aeda27433f
@ -38,14 +38,14 @@ impl HttpSender {
|
|||||||
///
|
///
|
||||||
/// The URL is an HTTP URL, usually for port 8899, as in
|
/// The URL is an HTTP URL, usually for port 8899, as in
|
||||||
/// "http://localhost:8899". The sender has a default timeout of 30 seconds.
|
/// "http://localhost:8899". The sender has a default timeout of 30 seconds.
|
||||||
pub fn new(url: String) -> Self {
|
pub fn new<U: ToString>(url: U) -> Self {
|
||||||
Self::new_with_timeout(url, Duration::from_secs(30))
|
Self::new_with_timeout(url, Duration::from_secs(30))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an HTTP RPC sender.
|
/// Create an HTTP RPC sender.
|
||||||
///
|
///
|
||||||
/// The URL is an HTTP URL, usually for port 8899.
|
/// The URL is an HTTP URL, usually for port 8899.
|
||||||
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
pub fn new_with_timeout<U: ToString>(url: U, timeout: Duration) -> Self {
|
||||||
let client = Arc::new(
|
let client = Arc::new(
|
||||||
reqwest::Client::builder()
|
reqwest::Client::builder()
|
||||||
.timeout(timeout)
|
.timeout(timeout)
|
||||||
@ -55,7 +55,7 @@ impl HttpSender {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
client,
|
client,
|
||||||
url,
|
url: url.to_string(),
|
||||||
request_id: AtomicU64::new(0),
|
request_id: AtomicU64::new(0),
|
||||||
stats: RwLock::new(RpcTransportStats::default()),
|
stats: RwLock::new(RpcTransportStats::default()),
|
||||||
}
|
}
|
||||||
|
@ -74,13 +74,13 @@ pub struct MockSender {
|
|||||||
/// from [`RpcRequest`] to a JSON [`Value`] response, Any entries in this map
|
/// from [`RpcRequest`] to a JSON [`Value`] response, Any entries in this map
|
||||||
/// override the default behavior for the given request.
|
/// override the default behavior for the given request.
|
||||||
impl MockSender {
|
impl MockSender {
|
||||||
pub fn new(url: String) -> Self {
|
pub fn new<U: ToString>(url: U) -> Self {
|
||||||
Self::new_with_mocks(url, Mocks::default())
|
Self::new_with_mocks(url, Mocks::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_with_mocks(url: String, mocks: Mocks) -> Self {
|
pub fn new_with_mocks<U: ToString>(url: U, mocks: Mocks) -> Self {
|
||||||
Self {
|
Self {
|
||||||
url,
|
url: url.to_string(),
|
||||||
mocks: RwLock::new(mocks),
|
mocks: RwLock::new(mocks),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,7 @@ impl RpcClient {
|
|||||||
/// let url = "http://localhost:8899".to_string();
|
/// let url = "http://localhost:8899".to_string();
|
||||||
/// let client = RpcClient::new(url);
|
/// let client = RpcClient::new(url);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new(url: String) -> Self {
|
pub fn new<U: ToString>(url: U) -> Self {
|
||||||
Self::new_with_commitment(url, CommitmentConfig::default())
|
Self::new_with_commitment(url, CommitmentConfig::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,7 +220,7 @@ impl RpcClient {
|
|||||||
/// let commitment_config = CommitmentConfig::processed();
|
/// let commitment_config = CommitmentConfig::processed();
|
||||||
/// let client = RpcClient::new_with_commitment(url, commitment_config);
|
/// let client = RpcClient::new_with_commitment(url, commitment_config);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_with_commitment(url: String, commitment_config: CommitmentConfig) -> Self {
|
pub fn new_with_commitment<U: ToString>(url: U, commitment_config: CommitmentConfig) -> Self {
|
||||||
Self::new_sender(
|
Self::new_sender(
|
||||||
HttpSender::new(url),
|
HttpSender::new(url),
|
||||||
RpcClientConfig::with_commitment(commitment_config),
|
RpcClientConfig::with_commitment(commitment_config),
|
||||||
@ -246,7 +246,7 @@ impl RpcClient {
|
|||||||
/// let timeout = Duration::from_secs(1);
|
/// let timeout = Duration::from_secs(1);
|
||||||
/// let client = RpcClient::new_with_timeout(url, timeout);
|
/// let client = RpcClient::new_with_timeout(url, timeout);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_with_timeout(url: String, timeout: Duration) -> Self {
|
pub fn new_with_timeout<U: ToString>(url: U, timeout: Duration) -> Self {
|
||||||
Self::new_sender(
|
Self::new_sender(
|
||||||
HttpSender::new_with_timeout(url, timeout),
|
HttpSender::new_with_timeout(url, timeout),
|
||||||
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
||||||
@ -275,8 +275,8 @@ impl RpcClient {
|
|||||||
/// commitment_config,
|
/// commitment_config,
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_with_timeout_and_commitment(
|
pub fn new_with_timeout_and_commitment<U: ToString>(
|
||||||
url: String,
|
url: U,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
commitment_config: CommitmentConfig,
|
commitment_config: CommitmentConfig,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
@ -318,8 +318,8 @@ impl RpcClient {
|
|||||||
/// confirm_transaction_initial_timeout,
|
/// confirm_transaction_initial_timeout,
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_with_timeouts_and_commitment(
|
pub fn new_with_timeouts_and_commitment<U: ToString>(
|
||||||
url: String,
|
url: U,
|
||||||
timeout: Duration,
|
timeout: Duration,
|
||||||
commitment_config: CommitmentConfig,
|
commitment_config: CommitmentConfig,
|
||||||
confirm_transaction_initial_timeout: Duration,
|
confirm_transaction_initial_timeout: Duration,
|
||||||
@ -353,7 +353,7 @@ impl RpcClient {
|
|||||||
/// let url = "fails".to_string();
|
/// let url = "fails".to_string();
|
||||||
/// let successful_client = RpcClient::new_mock(url);
|
/// let successful_client = RpcClient::new_mock(url);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_mock(url: String) -> Self {
|
pub fn new_mock<U: ToString>(url: U) -> Self {
|
||||||
Self::new_sender(
|
Self::new_sender(
|
||||||
MockSender::new(url),
|
MockSender::new(url),
|
||||||
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
||||||
@ -387,7 +387,7 @@ impl RpcClient {
|
|||||||
/// let url = "succeeds".to_string();
|
/// let url = "succeeds".to_string();
|
||||||
/// let client = RpcClient::new_mock_with_mocks(url, mocks);
|
/// let client = RpcClient::new_mock_with_mocks(url, mocks);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_mock_with_mocks(url: String, mocks: Mocks) -> Self {
|
pub fn new_mock_with_mocks<U: ToString>(url: U, mocks: Mocks) -> Self {
|
||||||
Self::new_sender(
|
Self::new_sender(
|
||||||
MockSender::new_with_mocks(url, mocks),
|
MockSender::new_with_mocks(url, mocks),
|
||||||
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
RpcClientConfig::with_commitment(CommitmentConfig::default()),
|
||||||
|
Reference in New Issue
Block a user