Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 5 additions & 1 deletion src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ pub trait ElectrumApi {
}

/// Executes the requested API call returning the raw answer.
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error>;
fn raw_call(
&self,
method_name: &str,
params: impl IntoIterator<Item = Param>,
) -> Result<serde_json::Value, Error>;

/// Execute a queue of calls stored in a [`Batch`](../batch/struct.Batch.html) struct. Returns
/// `Ok()` **only if** all of the calls are successful. The order of the JSON `Value`s returned
Expand Down
14 changes: 12 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,18 @@ impl Client {

impl ElectrumApi for Client {
#[inline]
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
impl_inner_call!(self, raw_call, call)
fn raw_call(
&self,
method_name: &str,
params: impl IntoIterator<Item = Param>,
) -> Result<serde_json::Value, Error> {
// We can't passthrough this method to the inner client because it would require the
// `params` argument to also be `Copy` (because it's used multiple times for multiple
// retries). To avoid adding this extra trait bound we instead re-direct this call to the internal
// `RawClient::internal_raw_call_with_vec` method.

let vec = params.into_iter().collect::<Vec<Param>>();
impl_inner_call!(self, internal_raw_call_with_vec, method_name, vec.clone());
}

#[inline]
Expand Down
38 changes: 26 additions & 12 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! This module contains the definition of the raw client that wraps the transport method

use std::collections::{BTreeMap, BTreeSet, HashMap, VecDeque};
use std::convert::TryFrom;
use std::io::{BufRead, BufReader, Read, Write};
use std::mem::drop;
use std::net::{TcpStream, ToSocketAddrs};
Expand Down Expand Up @@ -348,6 +347,8 @@ impl RawClient<ElectrumSslStream> {
validate_domain: bool,
tcp_stream: TcpStream,
) -> Result<Self, Error> {
use std::convert::TryFrom;

let builder = ClientConfig::builder().with_safe_defaults();

let config = if validate_domain {
Expand Down Expand Up @@ -658,6 +659,21 @@ impl<S: Read + Write> RawClient<S> {
Ok(())
}

pub(crate) fn internal_raw_call_with_vec(
&self,
method_name: &str,
params: Vec<Param>,
) -> Result<serde_json::Value, Error> {
let req = Request::new_id(
self.last_id.fetch_add(1, Ordering::SeqCst),
&method_name,
params,
);
let result = self.call(req)?;

Ok(result)
}

#[inline]
#[cfg(feature = "debug-calls")]
fn increment_calls(&self) {
Expand All @@ -670,15 +686,12 @@ impl<S: Read + Write> RawClient<S> {
}

impl<T: Read + Write> ElectrumApi for RawClient<T> {
fn raw_call(&self, call: &Call) -> Result<serde_json::Value, Error> {
let req = Request::new_id(
self.last_id.fetch_add(1, Ordering::SeqCst),
&call.0,
call.1.to_vec(),
);
let result = self.call(req)?;

Ok(result)
fn raw_call(
&self,
method_name: &str,
params: impl IntoIterator<Item = Param>,
) -> Result<serde_json::Value, Error> {
self.internal_raw_call_with_vec(method_name, params.into_iter().collect())
}

fn batch_call(&self, batch: &Batch) -> Result<Vec<serde_json::Value>, Error> {
Expand Down Expand Up @@ -1312,9 +1325,10 @@ mod test {
),
Param::Bool(false),
];
let call = ("blockchain.transaction.get".to_string(), params);

let resp = client.raw_call(&call).unwrap();
let resp = client
.raw_call("blockchain.transaction.get", params)
.unwrap();

assert_eq!(
resp,
Expand Down