diff --git a/src/ipns.rs b/src/ipns.rs index fb2eb3d..74b493c 100644 --- a/src/ipns.rs +++ b/src/ipns.rs @@ -23,7 +23,7 @@ impl IpfsApi { /// ``` pub fn name_resolve(&self, name: &str) -> Result { let url = format!("http://{}:{}/api/v0/name/resolve?arg={}", self.server, self.port, name); - let resp = reqwest::get(&url)?; + let resp = reqwest::get(&url)?.error_for_status()?; let resp: Value = serde_json::from_reader(resp)?; if resp["Path"].is_string() { @@ -36,7 +36,7 @@ impl IpfsApi { /// Publish an IPFS hash in IPNS. pub fn name_publish(&self, hash: &str) -> Result<()> { let url = format!("http://{}:{}/api/v0/name/publish?arg={}", self.server, self.port, hash); - let _resp = reqwest::get(&url)?; + let _resp = reqwest::get(&url)?.error_for_status()?; Ok(()) } } diff --git a/src/lib.rs b/src/lib.rs index 96c0c14..b9b4e4e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,6 +19,7 @@ mod ipns; mod object; mod version; +#[derive(Clone, PartialEq, Hash, Debug)] pub struct IpfsApi { server: String, port: u16 diff --git a/src/object.rs b/src/object.rs index 9c952a0..d2dafa8 100644 --- a/src/object.rs +++ b/src/object.rs @@ -13,8 +13,12 @@ error_chain! { #[derive(Deserialize, Debug, PartialEq, Hash)] #[serde(rename_all="PascalCase")] pub struct ObjectStats { - hash: String, - cumulative_size: u64 + pub hash: String, + pub num_links: u64, + pub block_size: u64, + pub links_size: u64, + pub data_size: u64, + pub cumulative_size: u64 } impl IpfsApi { @@ -22,7 +26,7 @@ impl IpfsApi { /// of a hash. pub fn object_stats(&self, hash: &str) -> Result { let url = format!("http://{}:{}/api/v0/object/stat?arg={}", self.server, self.port, hash); - let resp = reqwest::get(&url)?; + let resp = reqwest::get(&url)?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } } @@ -39,9 +43,13 @@ mod tests { let stats = api.object_stats("QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u").unwrap(); let desired = ObjectStats { hash: "QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u".to_string(), + num_links: 0, + block_size: 20, + links_size: 2, + data_size: 18, cumulative_size: 20, }; assert_eq!(stats, desired); } -} \ No newline at end of file +} diff --git a/src/pin.rs b/src/pin.rs index 1490758..7493e5d 100644 --- a/src/pin.rs +++ b/src/pin.rs @@ -15,27 +15,27 @@ error_chain! { #[derive(Deserialize, Debug, PartialEq, Hash)] #[serde(rename_all="PascalCase")] pub struct PinAddResponse { - pins: Vec, - progress: Option + pub pins: Vec, + pub progress: Option } #[derive(Deserialize, Debug, PartialEq, Hash)] #[serde(rename_all="PascalCase")] pub struct PinRmResponse { - pins: Vec + pub pins: Vec } #[derive(Deserialize, Debug, PartialEq, Hash)] pub struct PinType { #[serde(rename = "Type")] - objtype: String, + pub objtype: String, } #[derive(Deserialize, Debug, PartialEq)] #[serde(rename_all="PascalCase")] pub struct PinList { // keys: Vec - keys: HashMap + pub keys: HashMap } impl IpfsApi { @@ -51,7 +51,7 @@ impl IpfsApi { .append_pair("arg", hash) .append_pair("recursive", &recursive.to_string()) .append_pair("progress", &progress.to_string()); - let resp = reqwest::get(url)?; + let resp = reqwest::get(url)?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } @@ -62,7 +62,7 @@ impl IpfsApi { url.query_pairs_mut() .append_pair("arg", hash) .append_pair("recursive", &recursive.to_string()); - let resp = reqwest::get(url)?; + let resp = reqwest::get(url)?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } @@ -71,10 +71,7 @@ impl IpfsApi { pub fn pin_ls(&self) -> Result { let mut url = self.get_url()?; url.set_path("api/v0/pin/ls"); - // url.query_pairs_mut() - // .append_pair("arg", hash); - // .append_pair("recursive", &recursive.to_string()); - let resp = reqwest::get(url)?; + let resp = reqwest::get(url)?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } } @@ -82,7 +79,6 @@ impl IpfsApi { #[cfg(test)] mod tests { - use std::collections::HashMap; use IpfsApi; use super::*; @@ -106,16 +102,14 @@ mod tests { // Add pin let api = IpfsApi::new("127.0.0.1", 5001); - let resp = api.pin_add(obj, true, true); - // println!("Add response: {:#?}", resp); + let resp = api.pin_add(obj, true, false); + println!("Add response: {:#?}", resp); let desired = PinAddResponse { pins: vec![obj.into()], progress: None, }; assert_eq!(resp.unwrap(), desired); - - // List pin to make sure it's present. let api = IpfsApi::new("127.0.0.1", 5001); let resp = api.pin_ls(); @@ -132,4 +126,4 @@ mod tests { }; assert_eq!(resp.unwrap(), desired); } -} \ No newline at end of file +} diff --git a/src/pubsub.rs b/src/pubsub.rs index f10bfe2..3e97376 100644 --- a/src/pubsub.rs +++ b/src/pubsub.rs @@ -22,9 +22,9 @@ struct JsonPubSubMessage { #[derive(Debug)] pub struct PubSubMessage { - data: Option>, - from: Option>, - seqno: Option> + pub data: Option>, + pub from: Option>, + pub seqno: Option> } impl PubSubMessage { @@ -56,7 +56,7 @@ impl IpfsApi { /// ``` pub fn pubsub_subscribe(&self, channel: &str) -> Result> { let url = format!("http://{}:{}/api/v0/pubsub/sub?arg={}&discover=true", self.server, self.port, channel); - let resp = reqwest::get(&url)?; + let resp = reqwest::get(&url)?.error_for_status()?; let messages = BufReader::new(resp).lines() .filter(|x|x.is_ok()) @@ -80,7 +80,7 @@ impl IpfsApi { /// for peer-to-peer communication and dynamic apps over IPFS. pub fn pubsub_publish(&self, channel: &str, data: &str) -> Result<()> { let url = format!("http://{}:{}/api/v0/pubsub/pub?arg={}&arg={}", self.server, self.port, channel, data); - let _resp = reqwest::get(&url)?; + let _resp = reqwest::get(&url)?.error_for_status()?; Ok(()) } } diff --git a/src/version.rs b/src/version.rs index c8deee8..4c732b4 100644 --- a/src/version.rs +++ b/src/version.rs @@ -13,18 +13,18 @@ error_chain! { #[derive(Deserialize, Debug)] #[serde(rename_all="PascalCase")] pub struct IpfsVersion { - version: String, - commit: String, - repo: String, - system: String, - golang: String + pub version: String, + pub commit: String, + pub repo: String, + pub system: String, + pub golang: String } impl IpfsApi { /// Get the version from the IPFS daemon. pub fn version(&self) -> Result { let url = format!("http://{}:{}/api/v0/version", self.server, self.port); - let resp = reqwest::get(&url)?; + let resp = reqwest::get(&url)?.error_for_status()?; Ok(serde_json::from_reader(resp)?) } }