diff options
author | SionoiS <SionoiS@users.noreply.github.com> | 2020-08-04 11:06:23 -0400 |
---|---|---|
committer | SionoiS <SionoiS@users.noreply.github.com> | 2020-08-04 11:06:23 -0400 |
commit | 6af9cfd1664edf4ad78277d4079700ec53f2206e (patch) | |
tree | 0a1b955dd690b83c883f3f42476774b003ddade2 | |
parent | 8c999d104f403b34d9d9b8a63d1ed41c4aa871ee (diff) |
Config API
-rw-r--r-- | ipfs-api/examples/config.rs | 44 | ||||
-rw-r--r-- | ipfs-api/src/client/internal.rs | 29 | ||||
-rw-r--r-- | ipfs-api/src/request/config.rs | 20 | ||||
-rw-r--r-- | ipfs-api/src/response/config.rs | 10 |
4 files changed, 103 insertions, 0 deletions
diff --git a/ipfs-api/examples/config.rs b/ipfs-api/examples/config.rs new file mode 100644 index 0000000..d2eb8ca --- /dev/null +++ b/ipfs-api/examples/config.rs @@ -0,0 +1,44 @@ +// 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 value + let response = client + .config("Identity.PeerID", None, None, None) + .await + .expect("Config read failed"); + + println!("Config: {}={}", response.key, response.value); + + //set a boolean value + let response = client + .config("Pubsub.DisableSigning", Some("false"), Some(true), None) + .await + .expect("Config write failed"); + + println!("Config: {}={}", response.key, response.value); + + //set a integer value + let response = client + .config("Datastore.StorageGCWatermark", Some("90"), None, Some(true)) + .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..60acf2e 100644 --- a/ipfs-api/src/client/internal.rs +++ b/ipfs-api/src/client/internal.rs @@ -789,6 +789,35 @@ impl IpfsClient { self.request(request::Commands, None).await } + /// Get and set ipfs config values. + /// + /// ```no_run + /// use ipfs_api::IpfsClient; + /// + /// let client = IpfsClient::default(); + /// let res = client.config("Identity.PeerID", None, None, None); + /// ``` + /// + #[inline] + pub async fn config( + &self, + key: &str, + value: Option<&str>, + boolean: Option<bool>, + stringified_json: Option<bool>, + ) -> Result<response::ConfigResponse, Error> { + self.request( + request::Config { + key, + value, + boolean, + stringified_json, + }, + 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 = (); |