diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2020-09-04 10:16:16 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-04 10:16:16 -0400 |
commit | cbf189810f8a918b02536c792e490dec71d9eb5c (patch) | |
tree | a8cb09bba913a190b7b469d49b1603650bb6143e | |
parent | 8c999d104f403b34d9d9b8a63d1ed41c4aa871ee (diff) | |
parent | 2dd4058bf8002401f8e3ecd6e555883cea709bfd (diff) |
Merge pull request #59 from SionoiS/config
Config API
-rw-r--r-- | ipfs-api/examples/config.rs | 68 | ||||
-rw-r--r-- | ipfs-api/src/client/internal.rs | 150 | ||||
-rw-r--r-- | ipfs-api/src/request/config.rs | 20 | ||||
-rw-r--r-- | ipfs-api/src/response/config.rs | 10 |
4 files changed, 248 insertions, 0 deletions
diff --git a/ipfs-api/examples/config.rs b/ipfs-api/examples/config.rs new file mode 100644 index 0000000..884ecb4 --- /dev/null +++ b/ipfs-api/examples/config.rs @@ -0,0 +1,68 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or +// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or +// http://opensource.org/licenses/MIT>, at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use ipfs_api::IpfsClient; + +// Creates an Ipfs client, read & set config values. +// +#[cfg_attr(feature = "actix", actix_rt::main)] +#[cfg_attr(feature = "hyper", tokio::main)] +async fn main() { + eprintln!("note: this must be run in the root of the project repository"); + eprintln!("connecting to localhost:5001..."); + + let client = IpfsClient::default(); + + //read a string + let response = client + .config_get_string("Identity.PeerID") + .await + .expect("Config read failed"); + + println!("Config: {}={}", response.key, response.value); + + //read a bool + let response = client + .config_get_bool("Datastore.HashOnRead") + .await + .expect("Config read failed"); + + println!("Config: {}={}", response.key, response.value); + + //read a stringified json + let response = client + .config_get_json("Mounts") + .await + .expect("Config read failed"); + + println!("Config: {}={}", response.key, response.value); + + //set a string value + let response = client + .config_set_string("Routing.Type", "dht") + .await + .expect("Config write failed"); + + println!("Config: {}={}", response.key, response.value); + + //set a boolean value + let response = client + .config_set_bool("Pubsub.DisableSigning", false) + .await + .expect("Config write failed"); + + println!("Config: {}={}", response.key, response.value); + + //set a json value + let response = client + .config_set_json("Discovery", r#"{"MDNS":{"Enabled":true,"Interval":10}}"#) + .await + .expect("Config write failed"); + + println!("Config: {}={}", response.key, response.value); +} diff --git a/ipfs-api/src/client/internal.rs b/ipfs-api/src/client/internal.rs index 17059fd..974d3c0 100644 --- a/ipfs-api/src/client/internal.rs +++ b/ipfs-api/src/client/internal.rs @@ -789,6 +789,156 @@ impl IpfsClient { self.request(request::Commands, None).await } + /// Get ipfs config strings. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config_get_string("Identity.PeerID"); + /// ``` + /// + #[inline] + pub async fn config_get_string(&self, key: &str) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: None, + boolean: None, + stringified_json: None, + }, + None, + ) + .await + } + + /// Get ipfs config booleans. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config_get_bool("Datastore.HashOnRead"); + /// ``` + /// + #[inline] + pub async fn config_get_bool(&self, key: &str) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: None, + boolean: None, + stringified_json: None, + }, + None, + ) + .await + } + + /// Get ipfs config json. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config_get_json("Mounts"); + /// ``` + /// + #[inline] + pub async fn config_get_json(&self, key: &str) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: None, + boolean: None, + stringified_json: None, + }, + None, + ) + .await + } + + /// Set ipfs config string. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config_set_string("Routing.Type", "dht"); + /// ``` + /// + #[inline] + pub async fn config_set_string( + &self, + key: &str, + value: &str, + ) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: Some(value), + boolean: None, + stringified_json: None, + }, + None, + ) + .await + } + + /// Set ipfs config boolean. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config_set_bool("Pubsub.DisableSigning", "false"); + /// ``` + /// + #[inline] + pub async fn config_set_bool( + &self, + key: &str, + value: bool, + ) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: Some(&value.to_string()), + boolean: Some(true), + stringified_json: None, + }, + None, + ) + .await + } + + /// Set ipfs config json. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = config_set_json("Discovery", r#"{"MDNS":{"Enabled":true,"Interval":10}}"#); + /// ``` + /// + #[inline] + pub async fn config_set_json( + &self, + key: &str, + value: &str, + ) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value: Some(value), + boolean: None, + stringified_json: Some(true), + }, + None, + ) + .await + } + /// Opens the config file for editing (on the server). /// /// ```no_run diff --git a/ipfs-api/src/request/config.rs b/ipfs-api/src/request/config.rs index a6d5c58..9ea5250 100644 --- a/ipfs-api/src/request/config.rs +++ b/ipfs-api/src/request/config.rs @@ -7,6 +7,26 @@ // use crate::request::ApiRequest; +use crate::serde::Serialize; + +#[derive(Serialize)] +pub struct Config<'a> { + #[serde(rename = "arg")] + pub key: &'a str, + + #[serde(rename = "arg")] + pub value: Option<&'a str>, + + #[serde(rename = "bool")] + pub boolean: Option<bool>, + + #[serde(rename = "json")] + pub stringified_json: Option<bool>, +} + +impl<'a> ApiRequest for Config<'a> { + const PATH: &'static str = "/config"; +} pub struct ConfigEdit; diff --git a/ipfs-api/src/response/config.rs b/ipfs-api/src/response/config.rs index 81e8caf..0bacf98 100644 --- a/ipfs-api/src/response/config.rs +++ b/ipfs-api/src/response/config.rs @@ -6,6 +6,16 @@ // copied, modified, or distributed except according to those terms. // +use crate::serde::Deserialize; + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "PascalCase")] +pub struct ConfigResponse { + pub key: String, + + pub value: serde_json::value::Value, +} + pub type ConfigEditResponse = (); pub type ConfigReplaceResponse = (); |