diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2017-10-11 12:57:52 -0400 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2017-10-11 12:57:52 -0400 |
commit | 06c7a6c911a97ff7f566a35fa6ab24a74aa719c2 (patch) | |
tree | b29c2334d6c247f4ce9ef528e59063a9f7a7941e | |
parent | 710e86da290ec7f8944f85f89fbc02400bab25a5 (diff) |
modify api to return a Boxed future instead of a result
-rw-r--r-- | ipfs-api/examples/add_file.rs | 2 | ||||
-rw-r--r-- | ipfs-api/examples/bootstrap_default.rs | 8 | ||||
-rw-r--r-- | ipfs-api/examples/get_commands.rs | 2 | ||||
-rw-r--r-- | ipfs-api/examples/get_stats.rs | 6 | ||||
-rw-r--r-- | ipfs-api/examples/get_swarm.rs | 6 | ||||
-rw-r--r-- | ipfs-api/examples/get_version.rs | 2 | ||||
-rw-r--r-- | ipfs-api/src/client.rs | 108 |
7 files changed, 65 insertions, 69 deletions
diff --git a/ipfs-api/examples/add_file.rs b/ipfs-api/examples/add_file.rs index 163027e..2dccfa9 100644 --- a/ipfs-api/examples/add_file.rs +++ b/ipfs-api/examples/add_file.rs @@ -16,7 +16,7 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); let file = File::open(file!()).expect("could not read source file"); - let req = client.add(file).expect("expected a valid request"); + let req = client.add(file); let add = core.run(req).expect("expected a valid response"); println!("added file: {:?}", add); diff --git a/ipfs-api/examples/bootstrap_default.rs b/ipfs-api/examples/bootstrap_default.rs index 97df32e..d845d88 100644 --- a/ipfs-api/examples/bootstrap_default.rs +++ b/ipfs-api/examples/bootstrap_default.rs @@ -14,7 +14,7 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); - let bootstrap = client.bootstrap_list().expect("expected a valid request"); + let bootstrap = client.bootstrap_list(); let bootstrap = core.run(bootstrap).expect("expected a valid response"); println!("current bootstrap peers:"); @@ -25,7 +25,7 @@ fn main() { println!(""); println!("dropping all bootstrap peers..."); - let drop = client.bootstrap_rm_all().expect("expected a valid request"); + let drop = client.bootstrap_rm_all(); let drop = core.run(drop).expect("expected a valid response"); println!("dropped:"); @@ -36,9 +36,7 @@ fn main() { println!(""); println!("adding default peers..."); - let add = client.bootstrap_add_default().expect( - "expected a valid request", - ); + let add = client.bootstrap_add_default(); let add = core.run(add).expect("expected a valid response"); println!("added:"); diff --git a/ipfs-api/examples/get_commands.rs b/ipfs-api/examples/get_commands.rs index 2b4f04d..1d849f0 100644 --- a/ipfs-api/examples/get_commands.rs +++ b/ipfs-api/examples/get_commands.rs @@ -36,7 +36,7 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); - let req = client.commands().expect("expected a valid request"); + let req = client.commands(); print_recursive(0, &core.run(req).expect("expected a valid response")); } else { diff --git a/ipfs-api/examples/get_stats.rs b/ipfs-api/examples/get_stats.rs index b7bd293..a0cde97 100644 --- a/ipfs-api/examples/get_stats.rs +++ b/ipfs-api/examples/get_stats.rs @@ -14,13 +14,13 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); - let bitswap_stats = client.stats_bitswap().expect("expected a valid request"); + let bitswap_stats = client.stats_bitswap(); let bitswap_stats = core.run(bitswap_stats).expect("expected a valid response"); - let bw_stats = client.stats_bw().expect("expected a valid request"); + let bw_stats = client.stats_bw(); let bw_stats = core.run(bw_stats).expect("expected a valid response"); - let repo_stats = client.stats_repo().expect("expected a valid request"); + let repo_stats = client.stats_repo(); let repo_stats = core.run(repo_stats).expect("expected a valid response"); println!("bitswap stats:"); diff --git a/ipfs-api/examples/get_swarm.rs b/ipfs-api/examples/get_swarm.rs index 37bad39..6ee2d1a 100644 --- a/ipfs-api/examples/get_swarm.rs +++ b/ipfs-api/examples/get_swarm.rs @@ -15,9 +15,7 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); - let local = client.swarm_addrs_local().expect( - "expected a valid request", - ); + let local = client.swarm_addrs_local(); let local = core.run(local).expect("expected a valid response"); println!("your addrs:"); @@ -26,7 +24,7 @@ fn main() { } println!(""); - let connected = client.swarm_peers().expect("expected a valid request"); + let connected = client.swarm_peers(); let connected = core.run(connected).expect("expected a valid response"); println!("connected:"); diff --git a/ipfs-api/examples/get_version.rs b/ipfs-api/examples/get_version.rs index e605da3..8f743ab 100644 --- a/ipfs-api/examples/get_version.rs +++ b/ipfs-api/examples/get_version.rs @@ -13,7 +13,7 @@ fn main() { let client = IpfsClient::new(&core.handle(), "localhost", 5001).expect("expected a valid url"); - let req = client.version().expect("expected a valid request"); + let req = client.version(); let version = core.run(req).expect("expected a valid response"); println!("version: {:?}", version.version); diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index 790fb83..5bf95a9 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -1,5 +1,5 @@ use futures::Stream; -use futures::future::Future; +use futures::future::{Future, IntoFuture}; use request::{self, ApiRequest}; use reqwest::{self, multipart, Method, StatusCode, Url}; use reqwest::unstable::async::{self, Client, ClientBuilder}; @@ -15,11 +15,6 @@ use tokio_core::reactor::Handle; type AsyncResponse<T> = Box<Future<Item = T, Error = Error>>; -/// A result returned by a public facing call to the Ipfs api. -/// -type ApiResult<T> = Result<AsyncResponse<T>, Error>; - - /// Asynchronous Ipfs client. /// pub struct IpfsClient { @@ -91,10 +86,7 @@ impl IpfsClient { /// Methods prefixed with `send_` work on a raw reqwest `RequestBuilder` /// instance. /// - fn send_request( - &self, - mut req: async::RequestBuilder, - ) -> ApiResult<(StatusCode, async::Chunk)> { + fn send_request(mut req: async::RequestBuilder) -> AsyncResponse<(StatusCode, async::Chunk)> { let res = req.send() .and_then(|res| { let status = res.status(); @@ -103,7 +95,7 @@ impl IpfsClient { }) .from_err(); - Ok(Box::new(res)) + Box::new(res) } /// Sends a request and deserializes the response into Json. @@ -111,57 +103,65 @@ impl IpfsClient { /// Methods prefixed with `send_` work on a raw reqwest `RequestBuilder` /// instance. /// - fn send_request_json<Res>(&self, req: async::RequestBuilder) -> ApiResult<Res> + fn send_request_json<Res>(req: async::RequestBuilder) -> AsyncResponse<Res> where for<'de> Res: 'static + Deserialize<'de>, { - let res = self.send_request(req)?.and_then(move |(status, chunk)| { - IpfsClient::process_response(status, chunk) - }); + let res = IpfsClient::send_request(req).into_future().and_then( + move |(status, chunk)| IpfsClient::process_response(status, chunk), + ); - Ok(Box::new(res)) + Box::new(res) } /// Generates a request, and returns the unprocessed response future. /// - fn request_raw<Req>(&self, req: &Req) -> ApiResult<(StatusCode, async::Chunk)> + fn request_raw<Req>(&self, req: &Req) -> AsyncResponse<(StatusCode, async::Chunk)> where Req: ApiRequest + Serialize, { - let url = self.build_url(req)?; - let req = self.client.request(Method::Get, url); + let res = self.build_url(req) + .map(|url| self.client.request(Method::Get, url)) + .into_future() + .and_then(|req| IpfsClient::send_request(req)); - self.send_request(req) + Box::new(res) } /// Generic method for making a request to the Ipfs server, and getting /// a deserializable response. /// - fn request<Req, Res>(&self, req: &Req) -> ApiResult<Res> + fn request<Req, Res>(&self, req: &Req) -> AsyncResponse<Res> where Req: ApiRequest + Serialize, for<'de> Res: 'static + Deserialize<'de>, { - let url = self.build_url(req)?; - let req = self.client.request(Method::Get, url); + let res = self.build_url(req) + .map(|url| self.client.request(Method::Get, url)) + .into_future() + .and_then(|req| IpfsClient::send_request_json(req)); - self.send_request_json(req) + Box::new(res) } /// Generic method for making a request to the Ipfs server, and getting /// a deserializable response. /// - fn request_with_body<Req, Res, R>(&self, data: R, req: &Req) -> ApiResult<Res> + fn request_with_body<Req, Res, R>(&self, data: R, req: &Req) -> AsyncResponse<Res> where Req: ApiRequest + Serialize, for<'de> Res: 'static + Deserialize<'de>, R: 'static + Read + Send, { - let url = self.build_url(req)?; - let form = multipart::Form::new().part("file", multipart::Part::reader(data)); - let req = self.client.request(Method::Get, url); + let res = self.build_url(req) + .map(|url| self.client.request(Method::Get, url)) + .into_future() + .and_then(move |req| { + let form = multipart::Form::new().part("file", multipart::Part::reader(data)); + IpfsClient::send_request_json(req) + }); - self.send_request_json(req) + Box::new(res) } } @@ -169,7 +169,7 @@ impl IpfsClient { /// Add file to Ipfs. /// #[inline] - pub fn add<R>(&self, data: R) -> ApiResult<response::AddResponse> + pub fn add<R>(&self, data: R) -> AsyncResponse<response::AddResponse> where R: 'static + Read + Send, { @@ -178,32 +178,32 @@ impl IpfsClient { /// Add default peers to the bootstrap list. /// - pub fn bootstrap_add_default(&self) -> ApiResult<response::BootstrapAddDefaultResponse> { + pub fn bootstrap_add_default(&self) -> AsyncResponse<response::BootstrapAddDefaultResponse> { self.request(&request::BootstrapAddDefault) } /// Lists peers in bootstrap list. /// - pub fn bootstrap_list(&self) -> ApiResult<response::BootstrapListResponse> { + pub fn bootstrap_list(&self) -> AsyncResponse<response::BootstrapListResponse> { self.request(&request::BootstrapList) } /// Removes all peers in bootstrap list. /// - pub fn bootstrap_rm_all(&self) -> ApiResult<response::BootstrapRmAllResponse> { + pub fn bootstrap_rm_all(&self) -> AsyncResponse<response::BootstrapRmAllResponse> { self.request(&request::BootstrapRmAll) } /// List available commands that the server accepts. /// #[inline] - pub fn commands(&self) -> ApiResult<response::CommandsResponse> { + pub fn commands(&self) -> AsyncResponse<response::CommandsResponse> { self.request(&request::Commands) } /// Opens the config file for editing (on the server). /// - pub fn config_edit(&self) -> ApiResult<response::ConfigEditResponse> { + pub fn config_edit(&self) -> AsyncResponse<response::ConfigEditResponse> { self.request(&request::ConfigEdit) } @@ -211,50 +211,50 @@ impl IpfsClient { /// /// Returns an unparsed json string, due to an unclear spec. /// - pub fn config_show(&self) -> ApiResult<response::ConfigShowResponse> { - let req = self.request_raw(&request::ConfigShow)?.and_then( + 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) }, ); - Ok(Box::new(req)) + Box::new(req) } /// Returns information about a dag node in Ipfs. /// - pub fn dag_get(&self, path: &str) -> ApiResult<response::DagGetResponse> { + pub fn dag_get(&self, path: &str) -> AsyncResponse<response::DagGetResponse> { self.request(&request::DagGet { path }) } /// List the contents of an Ipfs multihash. /// #[inline] - pub fn ls(&self, path: Option<&str>) -> ApiResult<response::LsResponse> { + pub fn ls(&self, path: Option<&str>) -> AsyncResponse<response::LsResponse> { self.request(&request::Ls { path }) } /// Returns the diff of two Ipfs objects. /// - pub fn object_diff(&self, key0: &str, key1: &str) -> ApiResult<response::ObjectDiffResponse> { + pub fn object_diff(&self, key0: &str, key1: &str) -> AsyncResponse<response::ObjectDiffResponse> { self.request(&request::ObjectDiff { key0, key1 }) } /// Returns the data in an object. /// - pub fn object_get(&self, key: &str) -> ApiResult<response::ObjectGetResponse> { + pub fn object_get(&self, key: &str) -> AsyncResponse<response::ObjectGetResponse> { self.request(&request::ObjectGet { key }) } /// Returns the links that an object points to. /// - pub fn object_links(&self, key: &str) -> ApiResult<response::ObjectLinksResponse> { + pub fn object_links(&self, key: &str) -> AsyncResponse<response::ObjectLinksResponse> { self.request(&request::ObjectLinks { key }) } /// Returns the stats for an object. /// - pub fn object_stat(&self, key: &str) -> ApiResult<response::ObjectStatResponse> { + pub fn object_stat(&self, key: &str) -> AsyncResponse<response::ObjectStatResponse> { self.request(&request::ObjectStat { key }) } @@ -264,62 +264,62 @@ impl IpfsClient { &self, key: Option<&str>, typ: Option<&str>, - ) -> ApiResult<response::PinLsResponse> { + ) -> AsyncResponse<response::PinLsResponse> { self.request(&request::PinLs { key, typ }) } /// Removes a pinned object from local storage. /// - pub fn pin_rm(&self, key: &str, recursive: Option<bool>) -> ApiResult<response::PinRmResponse> { + pub fn pin_rm(&self, key: &str, recursive: Option<bool>) -> AsyncResponse<response::PinRmResponse> { self.request(&request::PinRm { key, recursive }) } /// List subscribed pubsub topics. /// - pub fn pubsub_ls(&self) -> ApiResult<response::PubsubLsResponse> { + pub fn pubsub_ls(&self) -> AsyncResponse<response::PubsubLsResponse> { self.request(&request::PubsubLs) } /// List peers that are being published to. /// - pub fn pubsub_peers(&self, topic: Option<&str>) -> ApiResult<response::PubsubPeersResponse> { + pub fn pubsub_peers(&self, topic: Option<&str>) -> AsyncResponse<response::PubsubPeersResponse> { self.request(&request::PubsubPeers { topic }) } /// Returns bitswap stats. /// - pub fn stats_bitswap(&self) -> ApiResult<response::StatsBitswapResponse> { + pub fn stats_bitswap(&self) -> AsyncResponse<response::StatsBitswapResponse> { self.request(&request::StatsBitswap) } /// Returns bandwidth stats. /// - pub fn stats_bw(&self) -> ApiResult<response::StatsBwResponse> { + pub fn stats_bw(&self) -> AsyncResponse<response::StatsBwResponse> { self.request(&request::StatsBw) } /// Returns repo stats. /// - pub fn stats_repo(&self) -> ApiResult<response::StatsRepoResponse> { + pub fn stats_repo(&self) -> AsyncResponse<response::StatsRepoResponse> { self.request(&request::StatsRepo) } /// Return a list of local addresses. /// - pub fn swarm_addrs_local(&self) -> ApiResult<response::SwarmAddrsLocalResponse> { + pub fn swarm_addrs_local(&self) -> AsyncResponse<response::SwarmAddrsLocalResponse> { self.request(&request::SwarmAddrsLocal) } /// Return a list of peers with open connections. /// - pub fn swarm_peers(&self) -> ApiResult<response::SwarmPeersResponse> { + pub fn swarm_peers(&self) -> AsyncResponse<response::SwarmPeersResponse> { self.request(&request::SwarmPeers) } /// Returns information about the Ipfs server version. /// #[inline] - pub fn version(&self) -> ApiResult<response::VersionResponse> { + pub fn version(&self) -> AsyncResponse<response::VersionResponse> { self.request(&request::Version) } } |