From 248340a3a999366e7f603a1eeb7b314e601af407 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Jan 2018 18:30:31 -0500 Subject: add /key/rename and /key/rm --- ipfs-api/src/client.rs | 82 +++++++++++++++++------- ipfs-api/src/request/key.rs | 31 +++++++-- ipfs-api/src/response/key.rs | 22 +++++-- ipfs-api/src/response/tests/v0_key_rename_0.json | 6 ++ ipfs-api/src/response/tests/v0_key_rm_0.json | 8 +++ 5 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 ipfs-api/src/response/tests/v0_key_rename_0.json create mode 100644 ipfs-api/src/response/tests/v0_key_rm_0.json diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index 4cfcaf1..64faed0 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -12,7 +12,7 @@ use header::Trailer; use read::{JsonLineDecoder, LineDecoder, StreamReader}; use request::{self, ApiRequest}; use response::{self, Error, ErrorKind}; -use hyper::{self, Chunk, Request, Response, Uri, Method, StatusCode}; +use hyper::{self, Chunk, Method, Request, Response, StatusCode, Uri}; use hyper::client::{Client, Config, HttpConnector}; use hyper_multipart::client::multipart; use serde::{Deserialize, Serialize}; @@ -21,17 +21,14 @@ use std::io::Read; use tokio_core::reactor::Handle; use tokio_io::codec::{Decoder, FramedRead}; - /// A response returned by the HTTP client. /// type AsyncResponse = Box>; - /// A future that returns a stream of responses. /// type AsyncStreamResponse = Box>; - /// Asynchronous Ipfs client. /// pub struct IpfsClient { @@ -107,12 +104,10 @@ impl IpfsClient { fn build_error_from_body(chunk: Chunk) -> Error { match serde_json::from_slice(&chunk) { Ok(e) => ErrorKind::Api(e).into(), - Err(_) => { - match String::from_utf8(chunk.to_vec()) { - Ok(s) => ErrorKind::Uncategorized(s).into(), - Err(e) => e.into(), - } - } + Err(_) => match String::from_utf8(chunk.to_vec()) { + Ok(s) => ErrorKind::Uncategorized(s).into(), + Err(e) => e.into(), + }, } } @@ -226,9 +221,8 @@ impl IpfsClient { Req: ApiRequest + Serialize, for<'de> Res: 'static + Deserialize<'de>, { - let res = self.request_raw(req, form).and_then(|(status, chunk)| { - IpfsClient::process_json_response(status, chunk) - }); + let res = self.request_raw(req, form) + .and_then(|(status, chunk)| IpfsClient::process_json_response(status, chunk)); Box::new(res) } @@ -240,12 +234,11 @@ impl IpfsClient { where Req: ApiRequest + Serialize, { - let res = self.request_raw(req, form).and_then( - |(status, chunk)| match status { + let res = self.request_raw(req, form) + .and_then(|(status, chunk)| match status { StatusCode::Ok => Ok(()), _ => Err(Self::build_error_from_body(chunk)), - }, - ); + }); Box::new(res) } @@ -257,17 +250,15 @@ impl IpfsClient { where Req: ApiRequest + Serialize, { - let res = self.request_raw(req, form).and_then( - |(status, chunk)| match status { + let res = self.request_raw(req, form) + .and_then(|(status, chunk)| match status { StatusCode::Ok => String::from_utf8(chunk.to_vec()).map_err(From::from), _ => Err(Self::build_error_from_body(chunk)), - }, - ); + }); Box::new(res) } - /// Generic method for making a request to the Ipfs server, and getting /// back a raw stream of bytes. /// @@ -1414,6 +1405,53 @@ impl IpfsClient { self.request(&request::KeyList, None) } + /// Rename a keypair. + /// + /// ```no_run + /// # extern crate ipfs_api; + /// # extern crate tokio_core; + /// # + /// use ipfs_api::IpfsClient; + /// use tokio_core::reactor::Core; + /// + /// # fn main() { + /// let mut core = Core::new().unwrap(); + /// let client = IpfsClient::default(&core.handle()); + /// let req = client.key_rename("key_0", "new_name", false); + /// # } + /// ``` + /// + #[inline] + pub fn key_rename( + &self, + name: &str, + new: &str, + force: bool, + ) -> AsyncResponse { + self.request(&request::KeyRename { name, new, force }, None) + } + + /// Remove a keypair. + /// + /// ```no_run + /// # extern crate ipfs_api; + /// # extern crate tokio_core; + /// # + /// use ipfs_api::IpfsClient; + /// use tokio_core::reactor::Core; + /// + /// # fn main() { + /// let mut core = Core::new().unwrap(); + /// let client = IpfsClient::default(&core.handle()); + /// let req = client.key_rm("key_0"); + /// # } + /// ``` + /// + #[inline] + pub fn key_rm(&self, name: &str) -> AsyncResponse { + self.request(&request::KeyRm { name }, None) + } + /// Change the logging level for a logger. /// /// ```no_run diff --git a/ipfs-api/src/request/key.rs b/ipfs-api/src/request/key.rs index e656f60..763285d 100644 --- a/ipfs-api/src/request/key.rs +++ b/ipfs-api/src/request/key.rs @@ -9,7 +9,6 @@ use request::ApiRequest; use serde::ser::{Serialize, Serializer}; - #[derive(Copy, Clone)] pub enum KeyType { Rsa, @@ -30,14 +29,11 @@ impl Serialize for KeyType { } } - #[derive(Serialize)] pub struct KeyGen<'a, 'b> { - #[serde(rename = "arg")] - pub name: &'a str, + #[serde(rename = "arg")] pub name: &'a str, - #[serde(rename = "type")] - pub kind: KeyType, + #[serde(rename = "type")] pub kind: KeyType, pub size: &'b Option, } @@ -46,7 +42,6 @@ impl<'a, 'b> ApiRequest for KeyGen<'a, 'b> { const PATH: &'static str = "/key/gen"; } - pub struct KeyList; impl_skip_serialize!(KeyList); @@ -54,3 +49,25 @@ impl_skip_serialize!(KeyList); impl ApiRequest for KeyList { const PATH: &'static str = "/key/list"; } + +#[derive(Serialize)] +pub struct KeyRename<'a, 'b> { + #[serde(rename = "arg")] pub name: &'a str, + + #[serde(rename = "arg")] pub new: &'b str, + + pub force: bool, +} + +impl<'a, 'b> ApiRequest for KeyRename<'a, 'b> { + const PATH: &'static str = "/key/rename"; +} + +#[derive(Serialize)] +pub struct KeyRm<'a> { + #[serde(rename = "arg")] pub name: &'a str, +} + +impl<'a> ApiRequest for KeyRm<'a> { + const PATH: &'static str = "/key/rm"; +} diff --git a/ipfs-api/src/response/key.rs b/ipfs-api/src/response/key.rs index 2eb7de7..2e3cf67 100644 --- a/ipfs-api/src/response/key.rs +++ b/ipfs-api/src/response/key.rs @@ -8,25 +8,37 @@ use response::serde; - #[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] -pub struct KeyGenResponse { +pub struct KeyPair { pub name: String, pub id: String, } +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct KeyPairList { + #[serde(deserialize_with = "serde::deserialize_vec")] pub keys: Vec, +} + +pub type KeyGenResponse = KeyPair; + +pub type KeyListResponse = KeyPairList; #[derive(Debug, Deserialize)] #[serde(rename_all = "PascalCase")] -pub struct KeyListResponse { - #[serde(deserialize_with = "serde::deserialize_vec")] - pub keys: Vec, +pub struct KeyRenameResponse { + pub was: String, + pub now: String, + pub id: String, + pub overwrite: bool, } +pub type KeyRmResponse = KeyPairList; #[cfg(test)] mod tests { deserialize_test!(v0_key_gen_0, KeyGenResponse); deserialize_test!(v0_key_list_0, KeyListResponse); + deserialize_test!(v0_key_rename_0, KeyRenameResponse); } diff --git a/ipfs-api/src/response/tests/v0_key_rename_0.json b/ipfs-api/src/response/tests/v0_key_rename_0.json new file mode 100644 index 0000000..303128b --- /dev/null +++ b/ipfs-api/src/response/tests/v0_key_rename_0.json @@ -0,0 +1,6 @@ +{ + "Was":"test0", + "Now":"test", + "Id":"QmPLabRUDjgGUmLTQwbBzbFtRGH3jaC33jbHKBtyQ7TfrC", + "Overwrite":false +} diff --git a/ipfs-api/src/response/tests/v0_key_rm_0.json b/ipfs-api/src/response/tests/v0_key_rm_0.json new file mode 100644 index 0000000..cd89710 --- /dev/null +++ b/ipfs-api/src/response/tests/v0_key_rm_0.json @@ -0,0 +1,8 @@ +{ + "Keys": [ + { + "Name":"test", + "Id":"QmPLabRUDjgGUmLTQwbBzbFtRGH3jaC33jbHKBtyQ7TfrC" + } + ] +} -- cgit v1.2.3