Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup stuff, and better error handling #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/ipns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl IpfsApi {
/// ```
pub fn name_resolve(&self, name: &str) -> Result<String> {
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() {
Expand All @@ -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(())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod ipns;
mod object;
mod version;

#[derive(Clone, PartialEq, Hash, Debug)]
pub struct IpfsApi {
server: String,
port: u16
Expand Down
16 changes: 12 additions & 4 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ 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 {
/// Get stats for an IPFS hash. It can be used to get the recursive size
/// of a hash.
pub fn object_stats(&self, hash: &str) -> Result<ObjectStats> {
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)?)
}
}
Expand All @@ -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);
}
}
}
28 changes: 11 additions & 17 deletions src/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ error_chain! {
#[derive(Deserialize, Debug, PartialEq, Hash)]
#[serde(rename_all="PascalCase")]
pub struct PinAddResponse {
pins: Vec<String>,
progress: Option<u64>
pub pins: Vec<String>,
pub progress: Option<u64>
}

#[derive(Deserialize, Debug, PartialEq, Hash)]
#[serde(rename_all="PascalCase")]
pub struct PinRmResponse {
pins: Vec<String>
pub pins: Vec<String>
}

#[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<String>
keys: HashMap<String, PinType>
pub keys: HashMap<String, PinType>
}

impl IpfsApi {
Expand All @@ -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)?)
}

Expand All @@ -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)?)
}

Expand All @@ -71,18 +71,14 @@ impl IpfsApi {
pub fn pin_ls(&self) -> Result<PinList> {
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)?)
}
}


#[cfg(test)]
mod tests {
use std::collections::HashMap;
use IpfsApi;
use super::*;

Expand All @@ -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();
Expand All @@ -132,4 +126,4 @@ mod tests {
};
assert_eq!(resp.unwrap(), desired);
}
}
}
10 changes: 5 additions & 5 deletions src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ struct JsonPubSubMessage {

#[derive(Debug)]
pub struct PubSubMessage {
data: Option<Vec<u8>>,
from: Option<Vec<u8>>,
seqno: Option<Vec<u8>>
pub data: Option<Vec<u8>>,
pub from: Option<Vec<u8>>,
pub seqno: Option<Vec<u8>>
}

impl PubSubMessage {
Expand Down Expand Up @@ -56,7 +56,7 @@ impl IpfsApi {
/// ```
pub fn pubsub_subscribe(&self, channel: &str) -> Result<impl Iterator<Item=PubSubMessage>> {
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())
Expand All @@ -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(())
}
}
12 changes: 6 additions & 6 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IpfsVersion> {
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)?)
}
}