Skip to content

Commit

Permalink
Fix URL Compatibility with 2.0.0 (#236)
Browse files Browse the repository at this point in the history
* fix url scheme for consistency with 2.0.0

* rustfmt

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

* rustmft
  • Loading branch information
yeastplume authored Oct 9, 2019
1 parent e02b5d9 commit 8c1d326
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion impls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ tokio-core = "0.1"
tokio-retry = "0.1"
uuid = { version = "0.7", features = ["serde", "v4"] }
chrono = { version = "0.4.4", features = ["serde"] }
url = "1.7.2"

grin_wallet_util = { path = "../util", version = "3.0.0-alpha.1" }
grin_wallet_config = { path = "../config", version = "3.0.0-alpha.1" }
Expand Down
33 changes: 18 additions & 15 deletions impls/src/adapters/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,26 @@ use crate::libwallet::{Error, ErrorKind, Slate};
use crate::SlateSender;
use serde::Serialize;
use serde_json::{json, Value};
use url::Url;

#[derive(Clone)]
pub struct HttpSlateSender {
base_url: Url,
base_url: String,
}

impl HttpSlateSender {
/// Create, return Err if scheme is not "http"
pub fn new(base_url: Url) -> Result<HttpSlateSender, SchemeNotHttp> {
if base_url.scheme() != "http" && base_url.scheme() != "https" {
pub fn new(base_url: &str) -> Result<HttpSlateSender, SchemeNotHttp> {
if !base_url.starts_with("http") && !base_url.starts_with("https") {
Err(SchemeNotHttp)
} else {
Ok(HttpSlateSender { base_url })
Ok(HttpSlateSender {
base_url: base_url.to_owned(),
})
}
}

/// 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!({
"jsonrpc": "2.0",
"method": "check_version",
Expand Down Expand Up @@ -95,13 +96,15 @@ impl HttpSlateSender {

impl SlateSender for HttpSlateSender {
fn send_tx(&self, slate: &Slate) -> Result<Slate, Error> {
let url: Url = self
.base_url
.join("/v2/foreign")
.expect("/v2/foreign is an invalid url path");
debug!("Posting transaction slate to {}", url);
let trailing = match self.base_url.ends_with('/') {
true => "",
false => "/",
};
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
let req = json!({
Expand All @@ -116,7 +119,7 @@ impl SlateSender for HttpSlateSender {
});
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);
error!("{}", report);
ErrorKind::ClientCallback(report)
Expand Down Expand Up @@ -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
IN: Serialize,
{
// 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)?;
Ok(res)
}
7 changes: 1 addition & 6 deletions impls/src/adapters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,14 @@ pub trait SlateGetter {

/// 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> {
use url::Url;

let invalid = || {
ErrorKind::WalletComms(format!(
"Invalid wallet comm type and destination. method: {}, dest: {}",
method, dest
))
};
Ok(match method {
"http" => {
let url: Url = dest.parse().map_err(|_| invalid())?;
Box::new(HttpSlateSender::new(url).map_err(|_| invalid())?)
}
"http" => Box::new(HttpSlateSender::new(dest).map_err(|_| invalid())?),
"keybase" => Box::new(KeybaseChannel::new(dest.to_owned())?),
"self" => {
return Err(ErrorKind::WalletComms(
Expand Down

0 comments on commit 8c1d326

Please sign in to comment.