Skip to content

Commit

Permalink
add new_from_uri() method to Client
Browse files Browse the repository at this point in the history
version 1.0.1
  • Loading branch information
Danconnolly committed Jul 16, 2024
1 parent 2aebb43 commit f31782d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 1.0.1
- only bitcoinsv-rpc - added new() method for Client which accepts a URI with username and password included

# 1.0.0
- cargo update
- add timeout parameter for Client
Expand Down
69 changes: 68 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bitcoinsv-rpc"
version = "1.0.0"
version = "1.0.1"
authors = [
"Daniel Connolly <[email protected]>"
]
Expand All @@ -26,6 +26,7 @@ serde_json = "1"
hex = "0.4.3"
bitcoinsv = "0.2.5"
async-trait = "0.1.75"
url = "2.5.0"

[dev-dependencies]
tempfile = "3.3.0"
Expand Down
19 changes: 19 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::{BufRead, BufReader, Cursor};
use std::iter::FromIterator;
use std::path::PathBuf;
use std::{fmt, result};
use url::Url;
use async_trait::async_trait;
use hex::{FromHex, ToHex};
use jsonrpc;

Check warning on line 10 in client/src/client.rs

View workflow job for this annotation

GitHub Actions / Internal Tests

this import is redundant

warning: this import is redundant --> client/src/client.rs:10:1 | 10 | use jsonrpc; | ^^^^^^^^^^^^ help: remove it entirely | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports = note: `#[warn(clippy::single_component_path_imports)]` on by default
Expand Down Expand Up @@ -538,6 +539,24 @@ impl Client {
};
Ok(Client {client: jsonrpc::client::Client::with_transport(b.build())})
}

pub fn new_from_uri(uri: &str, timeout: Option<std::time::Duration>) -> Result<Self> {
let url;
let username;
let password;
match Url::parse(&*uri) {
Err(_e) => {
println!("could not parse RPC URI");
return Err(Error::InvalidUri);
}
Ok(result) => {
url = format!("{}://{}:{}/", result.scheme(), result.host_str().unwrap(), result.port().unwrap());
username = String::from(result.username());
password = String::from(result.password().unwrap());
}
}
Client::new(&url, Auth::UserPass(username, password), timeout)
}
}

impl RpcApi for Client {
Expand Down
10 changes: 3 additions & 7 deletions client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum Error {
UnexpectedStructure,
/// The daemon returned an error string.
ReturnedError(String),
/// The URI could not be parsed.
InvalidUri,
MinReqError(jsonrpc::minreq_http::Error),
SVJsonError(bitcoinsv::Error),
}
Expand Down Expand Up @@ -43,12 +45,6 @@ impl From<io::Error> for Error {
}
}

// impl From<bitcoinsv::Error> for Error {
// fn from(e: bitcoinsv::Error) -> Error {
// Error::BitcoinSVError(e)
// }
// }
//
impl From<jsonrpc::minreq_http::Error> for Error {
fn from(e: jsonrpc::minreq_http::Error) -> Error {
Error::MinReqError(e)
Expand All @@ -71,7 +67,7 @@ impl fmt::Display for Error {
Error::InvalidCookieFile => write!(f, "invalid cookie file"),
Error::UnexpectedStructure => write!(f, "the JSON result had an unexpected structure"),
Error::ReturnedError(ref s) => write!(f, "the daemon returned an error string: {}", s),
// Error::BitcoinSVError(ref e) => write!(f, "BSV error: {}", e),
Error::InvalidUri => write!(f, "the URI could not be parsed"),
Error::MinReqError(ref e) => write!(f, "HTTPMinReq: {}", e),
Error::SVJsonError(ref e) => write!(f, "SVJson: {}", e),
}
Expand Down

0 comments on commit f31782d

Please sign in to comment.