From b54e6ea5377925f79200018060c04e9bc18cf8fc Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Sat, 16 Dec 2023 19:39:17 +0300 Subject: [PATCH 1/4] impl id_with_options --- ipfs-api-prelude/src/api.rs | 36 +++++++++++++++++++++++++++++- ipfs-api-prelude/src/request/id.rs | 14 +++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index d82ee27..53e4534 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -1603,7 +1603,41 @@ pub trait IpfsApi: Backend { /// ``` /// async fn id(&self, peer: Option<&str>) -> Result { - self.request(request::Id { peer }, None).await + self.request( + request::Id { + peer, + format: None, + peerid_base: None, + }, + None, + ) + .await + } + + /// Returns information about a peer. + /// + /// If `peer` is `None`, returns information about you. + /// + /// ```no_run + /// use ipfs_api::{IpfsApi, IpfsClient}; + /// + /// let client = IpfsClient::default(); + /// #[cfg(feature = "with-builder")] + /// let req = ipfs_api::request::Id::builder() + /// .peer("QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM") + /// .build(); + /// #[cfg(not(feature = "with-builder"))] + /// let req = ipfs_api::request::Id { + /// peer: "QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM", + /// .. Default::default() + /// }; + /// let res = client.id_with_options(req); + /// + async fn id_with_options( + &self, + options: request::Id<'_>, + ) -> Result { + self.request(options, None).await } /// Create a new keypair. diff --git a/ipfs-api-prelude/src/request/id.rs b/ipfs-api-prelude/src/request/id.rs index 5187e97..47ab131 100644 --- a/ipfs-api-prelude/src/request/id.rs +++ b/ipfs-api-prelude/src/request/id.rs @@ -9,10 +9,22 @@ use crate::request::ApiRequest; use serde::Serialize; -#[derive(Serialize)] +#[cfg_attr(feature = "with-builder", derive(TypedBuilder))] +#[derive(Default, Serialize)] +#[serde(rename_all = "kebab-case")] pub struct Id<'a> { + /// Peer.ID of node to look up. #[serde(rename = "arg")] + #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] pub peer: Option<&'a str>, + + /// Optional output format. + #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] + pub format: Option<&'a str>, + + /// Encoding used for peer IDs: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}. Default: b58mh. + #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] + pub peerid_base: Option<&'a str>, } impl<'a> ApiRequest for Id<'a> { From 44dc16e2c1f76e6945798454f9f3f2ad7b70dfde Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Sat, 16 Dec 2023 20:43:55 +0300 Subject: [PATCH 2/4] ignore the format field --- ipfs-api-prelude/src/api.rs | 2 +- ipfs-api-prelude/src/request/id.rs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index 53e4534..a44fba6 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -1606,7 +1606,7 @@ pub trait IpfsApi: Backend { self.request( request::Id { peer, - format: None, + format: (), peerid_base: None, }, None, diff --git a/ipfs-api-prelude/src/request/id.rs b/ipfs-api-prelude/src/request/id.rs index 47ab131..916eec7 100644 --- a/ipfs-api-prelude/src/request/id.rs +++ b/ipfs-api-prelude/src/request/id.rs @@ -18,9 +18,10 @@ pub struct Id<'a> { #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] pub peer: Option<&'a str>, - /// Optional output format. - #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] - pub format: Option<&'a str>, + /// Ignored by go-ipfs in it's REST API. Always returns in JSON. Retained for compatibility. + #[serde(skip)] + #[cfg_attr(feature = "with-builder", builder(default))] + pub format: (), /// Encoding used for peer IDs: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}. Default: b58mh. #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] From 192662b57eb1f960bba64721c584b591302de7c0 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Sat, 16 Dec 2023 20:52:16 +0300 Subject: [PATCH 3/4] use an unconstructible type for Format --- ipfs-api-prelude/src/request/id.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ipfs-api-prelude/src/request/id.rs b/ipfs-api-prelude/src/request/id.rs index 916eec7..17a2c16 100644 --- a/ipfs-api-prelude/src/request/id.rs +++ b/ipfs-api-prelude/src/request/id.rs @@ -19,9 +19,8 @@ pub struct Id<'a> { pub peer: Option<&'a str>, /// Ignored by go-ipfs in it's REST API. Always returns in JSON. Retained for compatibility. - #[serde(skip)] - #[cfg_attr(feature = "with-builder", builder(default))] - pub format: (), + #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] + pub format: Option, /// Encoding used for peer IDs: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}. Default: b58mh. #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] @@ -31,3 +30,5 @@ pub struct Id<'a> { impl<'a> ApiRequest for Id<'a> { const PATH: &'static str = "/id"; } + +pub enum IdFormat {} From 914b47f65bddd94bc2b400cb56f830c2b801b421 Mon Sep 17 00:00:00 2001 From: Miraculous Owonubi Date: Sat, 16 Dec 2023 21:06:35 +0300 Subject: [PATCH 4/4] serde skip format --- ipfs-api-prelude/src/api.rs | 2 +- ipfs-api-prelude/src/request/id.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs index a44fba6..53e4534 100644 --- a/ipfs-api-prelude/src/api.rs +++ b/ipfs-api-prelude/src/api.rs @@ -1606,7 +1606,7 @@ pub trait IpfsApi: Backend { self.request( request::Id { peer, - format: (), + format: None, peerid_base: None, }, None, diff --git a/ipfs-api-prelude/src/request/id.rs b/ipfs-api-prelude/src/request/id.rs index 17a2c16..e96d83f 100644 --- a/ipfs-api-prelude/src/request/id.rs +++ b/ipfs-api-prelude/src/request/id.rs @@ -19,6 +19,7 @@ pub struct Id<'a> { pub peer: Option<&'a str>, /// Ignored by go-ipfs in it's REST API. Always returns in JSON. Retained for compatibility. + #[serde(skip)] #[cfg_attr(feature = "with-builder", builder(default, setter(strip_option)))] pub format: Option,