Fix URL Compatibility with 2.0.0 (#236)

* fix url scheme for consistency with 2.0.0

* rustfmt

* remove URL lib and go back to parsing strings for the dest url

* rustmft
This commit is contained in:
Yeastplume 2019-10-09 15:19:37 +01:00 committed by GitHub
parent e02b5d9b20
commit 8c1d326a26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 23 deletions

1
Cargo.lock generated
View file

@ -912,7 +912,6 @@ dependencies = [
"tokio 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-retry 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-retry 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

@ -26,7 +26,6 @@ tokio-core = "0.1"
tokio-retry = "0.1" tokio-retry = "0.1"
uuid = { version = "0.7", features = ["serde", "v4"] } uuid = { version = "0.7", features = ["serde", "v4"] }
chrono = { version = "0.4.4", features = ["serde"] } chrono = { version = "0.4.4", features = ["serde"] }
url = "1.7.2"
grin_wallet_util = { path = "../util", version = "3.0.0-alpha.1" } grin_wallet_util = { path = "../util", version = "3.0.0-alpha.1" }
grin_wallet_config = { path = "../config", version = "3.0.0-alpha.1" } grin_wallet_config = { path = "../config", version = "3.0.0-alpha.1" }

View file

@ -18,25 +18,26 @@ use crate::libwallet::{Error, ErrorKind, Slate};
use crate::SlateSender; use crate::SlateSender;
use serde::Serialize; use serde::Serialize;
use serde_json::{json, Value}; use serde_json::{json, Value};
use url::Url;
#[derive(Clone)] #[derive(Clone)]
pub struct HttpSlateSender { pub struct HttpSlateSender {
base_url: Url, base_url: String,
} }
impl HttpSlateSender { impl HttpSlateSender {
/// Create, return Err if scheme is not "http" /// Create, return Err if scheme is not "http"
pub fn new(base_url: Url) -> Result<HttpSlateSender, SchemeNotHttp> { pub fn new(base_url: &str) -> Result<HttpSlateSender, SchemeNotHttp> {
if base_url.scheme() != "http" && base_url.scheme() != "https" { if !base_url.starts_with("http") && !base_url.starts_with("https") {
Err(SchemeNotHttp) Err(SchemeNotHttp)
} else { } else {
Ok(HttpSlateSender { base_url }) Ok(HttpSlateSender {
base_url: base_url.to_owned(),
})
} }
} }
/// Check version of the listening wallet /// Check version of the listening wallet
fn check_other_version(&self, url: &Url) -> Result<(), Error> { fn check_other_version(&self, url: &str) -> Result<(), Error> {
let req = json!({ let req = json!({
"jsonrpc": "2.0", "jsonrpc": "2.0",
"method": "check_version", "method": "check_version",
@ -95,13 +96,15 @@ impl HttpSlateSender {
impl SlateSender for HttpSlateSender { impl SlateSender for HttpSlateSender {
fn send_tx(&self, slate: &Slate) -> Result<Slate, Error> { fn send_tx(&self, slate: &Slate) -> Result<Slate, Error> {
let url: Url = self let trailing = match self.base_url.ends_with('/') {
.base_url true => "",
.join("/v2/foreign") false => "/",
.expect("/v2/foreign is an invalid url path"); };
debug!("Posting transaction slate to {}", url); let url_str = format!("{}{}v2/foreign", self.base_url, trailing);
self.check_other_version(&url)?; debug!("Posting transaction slate to {}", url_str);
self.check_other_version(&url_str)?;
// Note: not using easy-jsonrpc as don't want the dependencies in this crate // Note: not using easy-jsonrpc as don't want the dependencies in this crate
let req = json!({ let req = json!({
@ -116,7 +119,7 @@ impl SlateSender for HttpSlateSender {
}); });
trace!("Sending receive_tx request: {}", req); trace!("Sending receive_tx request: {}", req);
let res: String = post(&url, None, &req).map_err(|e| { let res: String = post(&url_str, None, &req).map_err(|e| {
let report = format!("Posting transaction slate (is recipient listening?): {}", e); let report = format!("Posting transaction slate (is recipient listening?): {}", e);
error!("{}", report); error!("{}", report);
ErrorKind::ClientCallback(report) ErrorKind::ClientCallback(report)
@ -152,12 +155,12 @@ impl Into<Error> for SchemeNotHttp {
} }
} }
pub fn post<IN>(url: &Url, api_secret: Option<String>, input: &IN) -> Result<String, api::Error> pub fn post<IN>(url: &str, api_secret: Option<String>, input: &IN) -> Result<String, api::Error>
where where
IN: Serialize, IN: Serialize,
{ {
// TODO: change create_post_request to accept a url instead of a &str // TODO: change create_post_request to accept a url instead of a &str
let req = api::client::create_post_request(url.as_str(), api_secret, input)?; let req = api::client::create_post_request(url, api_secret, input)?;
let res = api::client::send_request(req)?; let res = api::client::send_request(req)?;
Ok(res) Ok(res)
} }

View file

@ -58,8 +58,6 @@ pub trait SlateGetter {
/// select a SlateSender based on method and dest fields from, e.g., SendArgs /// select a SlateSender based on method and dest fields from, e.g., SendArgs
pub fn create_sender(method: &str, dest: &str) -> Result<Box<dyn SlateSender>, Error> { pub fn create_sender(method: &str, dest: &str) -> Result<Box<dyn SlateSender>, Error> {
use url::Url;
let invalid = || { let invalid = || {
ErrorKind::WalletComms(format!( ErrorKind::WalletComms(format!(
"Invalid wallet comm type and destination. method: {}, dest: {}", "Invalid wallet comm type and destination. method: {}, dest: {}",
@ -67,10 +65,7 @@ pub fn create_sender(method: &str, dest: &str) -> Result<Box<dyn SlateSender>, E
)) ))
}; };
Ok(match method { Ok(match method {
"http" => { "http" => Box::new(HttpSlateSender::new(dest).map_err(|_| invalid())?),
let url: Url = dest.parse().map_err(|_| invalid())?;
Box::new(HttpSlateSender::new(url).map_err(|_| invalid())?)
}
"keybase" => Box::new(KeybaseChannel::new(dest.to_owned())?), "keybase" => Box::new(KeybaseChannel::new(dest.to_owned())?),
"self" => { "self" => {
return Err(ErrorKind::WalletComms( return Err(ErrorKind::WalletComms(