Skip to content

Commit 625696a

Browse files
committed
Add HTTP headers feature for RPC Client
Signed-off-by: Ozan Selte <[email protected]>
1 parent df080d3 commit 625696a

File tree

3 files changed

+393
-0
lines changed

3 files changed

+393
-0
lines changed

rpc-client/src/http_sender.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ impl HttpSender {
5858
)
5959
}
6060

61+
/// Create an HTTP RPC sender.
62+
///
63+
/// The URL is an HTTP URL, usually for port 8899.
64+
/// The sender has a default timeout of 30 seconds.
65+
pub fn new_with_headers<U: ToString>(url: U, headers: header::HeaderMap) -> Self {
66+
Self::new_with_timeout_and_headers(url, Duration::from_secs(30), headers)
67+
}
68+
69+
/// Create an HTTP RPC sender.
70+
///
71+
/// The URL is an HTTP URL, usually for port 8899.
72+
pub fn new_with_timeout_and_headers<U: ToString>(
73+
url: U,
74+
timeout: Duration,
75+
headers: header::HeaderMap,
76+
) -> Self {
77+
Self::new_with_client(
78+
url,
79+
reqwest::Client::builder()
80+
.default_headers(headers)
81+
.timeout(timeout)
82+
.pool_idle_timeout(timeout)
83+
.build()
84+
.expect("build rpc client"),
85+
)
86+
}
87+
6188
/// Create an HTTP RPC sender.
6289
///
6390
/// Most flexible way to create a sender. Pass a created `reqwest::Client`.

rpc-client/src/nonblocking/rpc_client.rs

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use {
2222
base64::{prelude::BASE64_STANDARD, Engine},
2323
bincode::serialize,
2424
log::*,
25+
reqwest::header,
2526
serde_json::{json, Value},
2627
solana_account_decoder_client_types::{
2728
token::{TokenAccountType, UiTokenAccount, UiTokenAmount},
@@ -312,6 +313,188 @@ impl RpcClient {
312313
)
313314
}
314315

316+
/// Create an HTTP `RpcClient` with desired HTTP headers.
317+
///
318+
/// The URL is an HTTP URL, usually for port 8899, as in
319+
/// "http://localhost:8899".
320+
///
321+
/// The client has a default timeout of 30 seconds, and a default [commitment
322+
/// level][cl] of [`Finalized`](CommitmentLevel::Finalized).
323+
///
324+
/// [cl]: https://solana.com/docs/rpc#configuring-state-commitment
325+
///
326+
/// # Examples
327+
///
328+
/// ```
329+
/// # use reqwest::header;
330+
/// # use solana_rpc_client::nonblocking::rpc_client::RpcClient;
331+
/// let url = "http://localhost:8899".to_string();
332+
/// let mut headers = HeaderMap::new();
333+
/// headers.append(header::AUTHORIZATION, header::HeaderValue::from_str("Bearer <token>").unwrap());
334+
/// let client = RpcClient::new_with_headers(url, headers);
335+
/// ```
336+
pub fn new_with_headers(url: String, headers: header::HeaderMap) -> Self {
337+
Self::new_with_commitment_and_headers(url, CommitmentConfig::default(), headers)
338+
}
339+
340+
/// Create an HTTP `RpcClient` with specified [commitment level][cl] and desired HTTP headers.
341+
///
342+
/// [cl]: https://solana.com/docs/rpc#configuring-state-commitment
343+
///
344+
/// The URL is an HTTP URL, usually for port 8899, as in
345+
/// "http://localhost:8899".
346+
///
347+
/// The client has a default timeout of 30 seconds, and a user-specified
348+
/// [`CommitmentLevel`] via [`CommitmentConfig`].
349+
///
350+
/// # Examples
351+
///
352+
/// ```
353+
/// # use reqwest::header;
354+
/// # use solana_sdk::commitment_config::CommitmentConfig;
355+
/// # use solana_rpc_client::nonblocking::rpc_client::RpcClient;
356+
/// let url = "http://localhost:8899".to_string();
357+
/// let commitment_config = CommitmentConfig::processed();
358+
/// let mut headers = HeaderMap::new();
359+
/// headers.append(header::AUTHORIZATION, header::HeaderValue::from_str("Bearer <token>").unwrap());
360+
/// let client = RpcClient::new_with_commitment_and_headers(url, commitment_config, headers);
361+
/// ```
362+
pub fn new_with_commitment_and_headers(
363+
url: String,
364+
commitment_config: CommitmentConfig,
365+
headers: header::HeaderMap,
366+
) -> Self {
367+
Self::new_sender(
368+
HttpSender::new_with_headers(url, headers),
369+
RpcClientConfig::with_commitment(commitment_config),
370+
)
371+
}
372+
373+
/// Create an HTTP `RpcClient` with specified timeout and desired HTTP headers.
374+
///
375+
/// The URL is an HTTP URL, usually for port 8899, as in
376+
/// "http://localhost:8899".
377+
///
378+
/// The client has and a default [commitment level][cl] of
379+
/// [`Finalized`](CommitmentLevel::Finalized).
380+
///
381+
/// [cl]: https://solana.com/docs/rpc#configuring-state-commitment
382+
///
383+
/// # Examples
384+
///
385+
/// ```
386+
/// # use reqwest::header;
387+
/// # use std::time::Duration;
388+
/// # use solana_rpc_client::nonblocking::rpc_client::RpcClient;
389+
/// let url = "http://localhost::8899".to_string();
390+
/// let timeout = Duration::from_secs(1);
391+
/// let mut headers = HeaderMap::new();
392+
/// headers.append(header::AUTHORIZATION, header::HeaderValue::from_str("Bearer <token>").unwrap());
393+
/// let client = RpcClient::new_with_timeout_and_headers(url, timeout, headers);
394+
/// ```
395+
pub fn new_with_timeout_and_headers(
396+
url: String,
397+
timeout: Duration,
398+
headers: header::HeaderMap,
399+
) -> Self {
400+
Self::new_sender(
401+
HttpSender::new_with_timeout_and_headers(url, timeout, headers),
402+
RpcClientConfig::with_commitment(CommitmentConfig::default()),
403+
)
404+
}
405+
406+
/// Create an HTTP `RpcClient` with specified timeout, [commitment level][cl]
407+
/// and decired HTTP headers.
408+
///
409+
/// [cl]: https://solana.com/docs/rpc#configuring-state-commitment
410+
///
411+
/// The URL is an HTTP URL, usually for port 8899, as in
412+
/// "http://localhost:8899".
413+
///
414+
/// # Examples
415+
///
416+
/// ```
417+
/// # use reqwest::header;
418+
/// # use std::time::Duration;
419+
/// # use solana_rpc_client::nonblocking::rpc_client::RpcClient;
420+
/// # use solana_sdk::commitment_config::CommitmentConfig;
421+
/// let url = "http://localhost::8899".to_string();
422+
/// let timeout = Duration::from_secs(1);
423+
/// let commitment_config = CommitmentConfig::processed();
424+
/// let mut headers = HeaderMap::new();
425+
/// headers.append(header::AUTHORIZATION, header::HeaderValue::from_str("Bearer <token>").unwrap());
426+
/// let client = RpcClient::new_with_timeout_and_commitment_and_headers(
427+
/// url,
428+
/// timeout,
429+
/// commitment_config,
430+
/// headers,
431+
/// );
432+
/// ```
433+
pub fn new_with_timeout_and_commitment_and_headers(
434+
url: String,
435+
timeout: Duration,
436+
commitment_config: CommitmentConfig,
437+
headers: header::HeaderMap,
438+
) -> Self {
439+
Self::new_sender(
440+
HttpSender::new_with_timeout_and_headers(url, timeout, headers),
441+
RpcClientConfig::with_commitment(commitment_config),
442+
)
443+
}
444+
445+
/// Create an HTTP `RpcClient` with specified timeout, [commitment level][cl]
446+
/// and decired HTTP headers.
447+
///
448+
/// [cl]: https://solana.com/docs/rpc#configuring-state-commitment
449+
///
450+
/// The URL is an HTTP URL, usually for port 8899, as in
451+
/// "http://localhost:8899".
452+
///
453+
/// The `confirm_transaction_initial_timeout` argument specifies the amount of
454+
/// time to allow for the server to initially process a transaction, when
455+
/// confirming a transaction via one of the `_with_spinner` methods, like
456+
/// [`RpcClient::send_and_confirm_transaction_with_spinner`]. In
457+
/// other words, setting `confirm_transaction_initial_timeout` to > 0 allows
458+
/// `RpcClient` to wait for confirmation of a transaction that the server
459+
/// has not "seen" yet.
460+
///
461+
/// # Examples
462+
///
463+
/// ```
464+
/// # use reqwest::header;
465+
/// # use std::time::Duration;
466+
/// # use solana_rpc_client::nonblocking::rpc_client::RpcClient;
467+
/// # use solana_sdk::commitment_config::CommitmentConfig;
468+
/// let url = "http://localhost::8899".to_string();
469+
/// let timeout = Duration::from_secs(1);
470+
/// let commitment_config = CommitmentConfig::processed();
471+
/// let confirm_transaction_initial_timeout = Duration::from_secs(10);
472+
/// let mut headers = HeaderMap::new();
473+
/// headers.append(header::AUTHORIZATION, header::HeaderValue::from_str("Bearer <token>").unwrap());
474+
/// let client = RpcClient::new_with_timeouts_and_commitment_and_headers(
475+
/// url,
476+
/// timeout,
477+
/// commitment_config,
478+
/// confirm_transaction_initial_timeout,
479+
/// headers,
480+
/// );
481+
/// ```
482+
pub fn new_with_timeouts_and_commitment_and_headers(
483+
url: String,
484+
timeout: Duration,
485+
commitment_config: CommitmentConfig,
486+
confirm_transaction_initial_timeout: Duration,
487+
headers: header::HeaderMap,
488+
) -> Self {
489+
Self::new_sender(
490+
HttpSender::new_with_timeout_and_headers(url, timeout, headers),
491+
RpcClientConfig {
492+
commitment_config,
493+
confirm_transaction_initial_timeout: Some(confirm_transaction_initial_timeout),
494+
},
495+
)
496+
}
497+
315498
/// Create a mock `RpcClient`.
316499
///
317500
/// A mock `RpcClient` contains an implementation of [`RpcSender`] that does

0 commit comments

Comments
 (0)