Skip to content

Commit 27ea219

Browse files
committed
refactor!: build Request's with authorization
- add `AuthProvider` as public type. - add new `.with_auth()` method to `Request`, it sets the `authorization` token, if any. - update `.call()` and `.batch_call()` to use the newly added `.with_auth()`, instead of updating request's authorization field directly. - update `authorization` field in `Request` to private.
1 parent c4f1af1 commit 27ea219

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ pub mod utils;
5858
pub use api::ElectrumApi;
5959
pub use batch::Batch;
6060
pub use client::*;
61-
pub use config::{Config, ConfigBuilder, Socks5Config};
61+
pub use config::{AuthProvider, Config, ConfigBuilder, Socks5Config};
6262
pub use types::*;

src/raw_client.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,13 @@ impl<S: Read + Write> RawClient<S> {
764764
let (sender, receiver) = channel();
765765
self.waiting_map.lock()?.insert(req.id, sender);
766766

767-
// Apply authorization token if provider is set
768-
let mut req = req;
769-
if let Some(provider) = &self.auth_provider {
770-
req.authorization = provider();
771-
}
767+
// apply `authorization` token into `Request`, if any.
768+
let authorization = self
769+
.auth_provider
770+
.as_ref()
771+
.and_then(|auth_provider| auth_provider());
772+
773+
let req = req.with_auth(authorization);
772774

773775
let mut raw = serde_json::to_vec(&req)?;
774776
trace!("==> {}", String::from_utf8_lossy(&raw));
@@ -887,19 +889,20 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
887889

888890
// Add our listener to the map before we send the request
889891

890-
for (index, (method, params)) in batch.iter().enumerate() {
891-
let mut req = Request::new_id(
892+
for (method, params) in batch.iter() {
893+
// if there's an `auth_provider` available, it should get the `authorization`, if any.
894+
// it'll be set into into `Request`.
895+
let authorization = self
896+
.auth_provider
897+
.as_ref()
898+
.and_then(|auth_provider| auth_provider());
899+
900+
let req = Request::new_id(
892901
self.last_id.fetch_add(1, Ordering::SeqCst),
893902
method,
894903
params.to_vec(),
895-
);
896-
897-
// Servers expect auth token only in the first request in batched requests
898-
if index == 0 {
899-
if let Some(provider) = &self.auth_provider {
900-
req.authorization = provider();
901-
}
902-
}
904+
)
905+
.with_auth(authorization);
903906

904907
// Add distinct channel to each request so when we remove our request id (and sender) from the waiting_map
905908
// we can be sure that the response gets sent to the correct channel in self.recv

src/types.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,14 @@ pub struct Request<'a> {
7171
pub method: &'a str,
7272
/// The request parameters
7373
pub params: Vec<Param>,
74-
/// Optional authorization token (e.g., JWT Bearer token)
74+
75+
/// Authorization token (e.g. `"Bearer <token>"`) included in the JSON-RPC request, if any.
7576
#[serde(skip_serializing_if = "Option::is_none")]
76-
pub authorization: Option<String>,
77+
authorization: Option<String>,
7778
}
7879

7980
impl<'a> Request<'a> {
80-
/// Creates a new request with a default id
81+
/// Creates a new [`Request`] with a default `id`.
8182
fn new(method: &'a str, params: Vec<Param>) -> Self {
8283
Self {
8384
id: 0,
@@ -88,13 +89,19 @@ impl<'a> Request<'a> {
8889
}
8990
}
9091

91-
/// Creates a new request with a user-specified id
92+
/// Creates a new [`Request`] with a user-specified `id`.
9293
pub fn new_id(id: usize, method: &'a str, params: Vec<Param>) -> Self {
9394
let mut instance = Self::new(method, params);
9495
instance.id = id;
9596

9697
instance
9798
}
99+
100+
/// Sets the `authorization` token for this [`Request`].
101+
pub fn with_auth(mut self, authorization: Option<String>) -> Self {
102+
self.authorization = authorization;
103+
self
104+
}
98105
}
99106

100107
#[doc(hidden)]

0 commit comments

Comments
 (0)