diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-10-23 23:47:27 -0400 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-10-23 23:47:27 -0400 |
commit | e03ab20304d14c832ddbfeb9124cd34bff4da0be (patch) | |
tree | 55ff88360e2853eaad2946a6ace8339adf70941c | |
parent | dc75dd07409f104110d013c0eea377ce68abc14c (diff) |
add bitswap requests
-rw-r--r-- | ipfs-api/src/client.rs | 85 | ||||
-rw-r--r-- | ipfs-api/src/lib.rs | 3 | ||||
-rw-r--r-- | ipfs-api/src/request/bitswap.rs | 54 | ||||
-rw-r--r-- | ipfs-api/src/request/mod.rs | 2 | ||||
-rw-r--r-- | ipfs-api/src/response/bitswap.rs | 2 |
5 files changed, 129 insertions, 17 deletions
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index 597b4e5..9a7b65d 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -75,23 +75,49 @@ impl IpfsClient { .map_err(From::from) } - /// Processes a response, returning an error or a deserialized json response. + /// Builds an Api error from a response body. /// - fn process_response<Res>(status: StatusCode, chunk: async::Chunk) -> Result<Res, Error> + #[inline] + fn build_error_from_body(chunk: async::Chunk) -> Error { + match serde_json::from_slice(&chunk) { + Ok(e) => Error::Api(e), + Err(_) => { + match String::from_utf8(chunk.to_vec()) { + Ok(s) => Error::Uncategorized(s), + Err(e) => e.into(), + } + } + } + } + + /// Processes a response that expects a json encoded body, returning an + /// error or a deserialized json response. + /// + fn process_json_response<Res>(status: StatusCode, chunk: async::Chunk) -> Result<Res, Error> where for<'de> Res: 'static + Deserialize<'de>, { match status { StatusCode::Ok => serde_json::from_slice(&chunk).map_err(From::from), - _ => { - // For error responses, the error can either be a json error, - // or can just be a string message. - // - match serde_json::from_slice(&chunk) { - Ok(e) => Err(Error::Api(e)), - Err(_) => Err(Error::Uncategorized(String::from_utf8(chunk.to_vec())?)), - } - } + _ => Err(Self::build_error_from_body(chunk)), + } + } + + /// Processes a response that expects an empty body. + /// + fn process_empty_response(status: StatusCode, chunk: async::Chunk) -> Result<(), Error> { + match status { + StatusCode::Ok => Ok(()), + _ => Err(Self::build_error_from_body(chunk)), + } + } + + /// Processes a response that expects a raw String. + /// + fn process_string_response(status: StatusCode, chunk: async::Chunk) -> Result<String, Error> { + match status { + StatusCode::Ok => String::from_utf8(chunk.to_vec()).map_err(From::from), + _ => Err(Self::build_error_from_body(chunk)), } } @@ -122,7 +148,7 @@ impl IpfsClient { for<'de> Res: 'static + Deserialize<'de>, { let res = IpfsClient::send_request(req).into_future().and_then( - move |(status, chunk)| IpfsClient::process_response(status, chunk), + move |(status, chunk)| IpfsClient::process_json_response(status, chunk), ); Box::new(res) @@ -205,6 +231,37 @@ impl IpfsClient { self.request_with_body(data, &request::Add) } + /// Returns the current ledger for a peer. + /// + pub fn bitswap_ledger(&self, peer: &str) -> AsyncResponse<response::BitswapLedgerResponse> { + self.request(&request::BitswapLedger { peer }) + } + + /// Returns some stats about the bitswap agent. + /// + pub fn bitswap_stat(&self) -> AsyncResponse<response::BitswapStatResponse> { + self.request(&request::BitswapStat) + } + + /// Remove a given block from your wantlist. + /// + pub fn bitswap_unwant(&self, key: &str) -> AsyncResponse<response::BitswapUnwantResponse> { + let req = self.request_raw(&request::BitswapUnwant { key }).and_then( + |(code, chunk)| Self::process_empty_response(code, chunk), + ); + + Box::new(req) + } + + /// Shows blocks on the wantlist for you or the specified peer. + /// + pub fn bitswap_wantlist( + &self, + peer: Option<&str>, + ) -> AsyncResponse<response::BitswapWantlistResponse> { + self.request(&request::BitswapWantlist { peer }) + } + /// Add default peers to the bootstrap list. /// pub fn bootstrap_add_default(&self) -> AsyncResponse<response::BootstrapAddDefaultResponse> { @@ -242,8 +299,8 @@ impl IpfsClient { /// pub fn config_show(&self) -> AsyncResponse<response::ConfigShowResponse> { let req = self.request_raw(&request::ConfigShow).and_then( - |(_, chunk)| { - String::from_utf8(chunk.to_vec()).map_err(From::from) + |(status, chunk)| { + Self::process_string_response(status, chunk) }, ); diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs index a8613e5..39db8fa 100644 --- a/ipfs-api/src/lib.rs +++ b/ipfs-api/src/lib.rs @@ -6,11 +6,10 @@ extern crate serde_derive; extern crate serde_json; extern crate serde_urlencoded; extern crate tokio_core; - +extern crate tokio_io; pub use client::IpfsClient; - pub mod request; pub mod response; mod client; diff --git a/ipfs-api/src/request/bitswap.rs b/ipfs-api/src/request/bitswap.rs new file mode 100644 index 0000000..8be4cbe --- /dev/null +++ b/ipfs-api/src/request/bitswap.rs @@ -0,0 +1,54 @@ +use request::ApiRequest; + + +#[derive(Serialize)] +pub struct BitswapLedger<'a> { + #[serde(rename = "arg")] + pub peer: &'a str, +} + +impl<'a> ApiRequest for BitswapLedger<'a> { + #[inline] + fn path() -> &'static str { + "/bitswap/ledger" + } +} + + +pub struct BitswapStat; + +impl_skip_serialize!(BitswapStat); + +impl ApiRequest for BitswapStat { + #[inline] + fn path() -> &'static str { + "/bitswap/stat" + } +} + + +#[derive(Serialize)] +pub struct BitswapUnwant<'a> { + #[serde(rename = "arg")] + pub key: &'a str, +} + +impl<'a> ApiRequest for BitswapUnwant<'a> { + #[inline] + fn path() -> &'static str { + "/bitswap/stat" + } +} + + +#[derive(Serialize)] +pub struct BitswapWantlist<'a> { + pub peer: Option<&'a str>, +} + +impl<'a> ApiRequest for BitswapWantlist<'a> { + #[inline] + fn path() -> &'static str { + "/bitswap/wantlist" + } +} diff --git a/ipfs-api/src/request/mod.rs b/ipfs-api/src/request/mod.rs index 5c372ea..7e41802 100644 --- a/ipfs-api/src/request/mod.rs +++ b/ipfs-api/src/request/mod.rs @@ -1,4 +1,5 @@ pub use self::add::*; +pub use self::bitswap::*; pub use self::bootstrap::*; pub use self::commands::*; pub use self::config::*; @@ -49,6 +50,7 @@ macro_rules! impl_skip_serialize { mod add; +mod bitswap; mod bootstrap; mod commands; mod config; diff --git a/ipfs-api/src/response/bitswap.rs b/ipfs-api/src/response/bitswap.rs index 575decd..c540e0c 100644 --- a/ipfs-api/src/response/bitswap.rs +++ b/ipfs-api/src/response/bitswap.rs @@ -32,7 +32,7 @@ pub struct BitswapStatResponse { } -pub type BitswapUnwantResponse = String; +pub type BitswapUnwantResponse = (); #[derive(Deserialize)] |