From 6af9cfd1664edf4ad78277d4079700ec53f2206e Mon Sep 17 00:00:00 2001 From: SionoiS Date: Tue, 4 Aug 2020 11:06:23 -0400 Subject: Config API --- ipfs-api/examples/config.rs | 44 +++++++++++++++++++++++++++++++++++++++++ ipfs-api/src/client/internal.rs | 29 +++++++++++++++++++++++++++ ipfs-api/src/request/config.rs | 20 +++++++++++++++++++ ipfs-api/src/response/config.rs | 10 ++++++++++ 4 files changed, 103 insertions(+) create mode 100644 ipfs-api/examples/config.rs 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, or the MIT license , 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, + stringified_json: Option, + ) -> Result { + 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, + + #[serde(rename = "json")] + pub stringified_json: Option, +} + +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 = (); -- cgit v1.2.3