diff --git a/Cargo.lock b/Cargo.lock index 26b4774..b5971f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -2483,7 +2483,7 @@ dependencies = [ "serde_json", "serde_yaml", "sha3", - "solar_client", + "tilde-client", "toml", ] @@ -2575,7 +2575,6 @@ dependencies = [ "peach-stats", "reqwest", "rouille", - "solar_client", "temporary", "vnstat_parse", "xdg", @@ -3445,16 +3444,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "solar_client" -version = "0.1.0" -dependencies = [ - "anyhow", - "jsonrpc_client", - "reqwest", - "serde_json", -] - [[package]] name = "spidev" version = "0.3.0" @@ -3684,6 +3673,17 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "tilde-client" +version = "0.0.1" +dependencies = [ + "anyhow", + "async-std", + "serde 1.0.209", + "serde_json", + "serde_yaml", +] + [[package]] name = "time" version = "0.1.44" diff --git a/Cargo.toml b/Cargo.toml index 0317b43..e7da647 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ members = [ "peach-monitor", "peach-stats", "peach-jsonrpc-server", - "peach-dyndns-updater" + "peach-dyndns-updater", + "tilde-client" ] diff --git a/peach-lib/Cargo.toml b/peach-lib/Cargo.toml index c45253f..3d08aa0 100644 --- a/peach-lib/Cargo.toml +++ b/peach-lib/Cargo.toml @@ -10,7 +10,7 @@ chrono = "0.4" dirs = "4.0" fslock="0.1" kuska-ssb = { git = "https://github.com/Kuska-ssb/ssb" } -solar_client = { path = "../../solarpub/solar_client" } +tilde-client = { path = "../tilde-client" } jsonrpc-client-core = "0.5" jsonrpc-client-http = "0.5" jsonrpc-core = "8.0" diff --git a/peach-lib/src/lib.rs b/peach-lib/src/lib.rs index 1a3afd7..ea5a84d 100644 --- a/peach-lib/src/lib.rs +++ b/peach-lib/src/lib.rs @@ -13,3 +13,4 @@ pub use jsonrpc_client_core; pub use jsonrpc_core; pub use serde_json; pub use serde_yaml; +pub use tilde_client; diff --git a/peach-lib/src/sbot.rs b/peach-lib/src/sbot.rs index a694ce4..a29085f 100644 --- a/peach-lib/src/sbot.rs +++ b/peach-lib/src/sbot.rs @@ -2,7 +2,7 @@ use std::{fs, fs::File, io, io::Write, path::PathBuf, process::Command, str}; use std::os::linux::raw::ino_t; -use solar_client::{Client, SolarClient}; +use tilde_client::{TildeClient, get_sbot_client}; use log::debug; use crate::config_manager; @@ -265,7 +265,7 @@ impl SbotConfig { } /// Initialise an sbot client -pub async fn init_sbot() -> Result { +pub async fn init_sbot() -> Result { // read sbot config from config.toml let sbot_config = SbotConfig::read().ok(); @@ -277,6 +277,6 @@ pub async fn init_sbot() -> Result { ); // TODO: read this from config const SERVER_ADDR: &str = "http://127.0.0.1:3030"; - let sbot_client = Client::new(SERVER_ADDR.to_owned())?; + let sbot_client = get_sbot_client(); Ok(sbot_client) } diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index e6343cf..6880306 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -39,7 +39,6 @@ chrono = "0.4" dirs = "4.0" env_logger = "0.8" futures = "0.3" -solar_client = { path = "../../solarpub/solar_client" } lazy_static = "1.4" log = "0.4" maud = "0.23" diff --git a/peach-web/src/utils/sbot.rs b/peach-web/src/utils/sbot.rs index f5bc26c..55a2fa8 100644 --- a/peach-web/src/utils/sbot.rs +++ b/peach-web/src/utils/sbot.rs @@ -16,10 +16,10 @@ use peach_lib::config_manager; use peach_lib::sbot::SbotConfig; use peach_lib::sbot::init_sbot; use peach_lib::ssb_messages::SsbMessageKVT; -use solar_client::{Client, SolarClient}; use rouille::input::post::BufferedFile; use temporary::Directory; use peach_lib::serde_json::json; +use peach_lib::tilde_client::TildeClient; use crate::{error::PeachWebError, utils::sbot}; // SBOT HELPER FUNCTIONS @@ -64,7 +64,7 @@ pub fn restart_sbot_process() -> (String, String) { } /// Initialise an sbot client with the given configuration parameters. -pub async fn init_sbot_client() -> Result { +pub async fn init_sbot_client() -> Result { debug!("Initialising an sbot client with configuration parameters"); // initialise sbot connection with ip:port and shscap from config file let key_path = format!( @@ -386,7 +386,7 @@ pub fn block_peer(public_key: &str) -> Result { .map_err(|e| e.to_string())?; debug!("Blocking a Scuttlebutt peer"); - match sbot_client.blocks(public_key).await { + match sbot_client.create_block(public_key).await { Ok(_) => Ok("Blocked peer".to_string()), Err(e) => Err(format!("Failed to block peer: {}", e)), } @@ -421,7 +421,7 @@ pub fn get_blocks_list() -> Result>, Box> let self_id = sbot_client.whoami().await?; - let blocks = sbot_client.blocks(&self_id).await?; + let blocks = sbot_client.get_blocks(&self_id).await?; if !blocks.is_empty() { for peer in blocks.iter() { @@ -489,7 +489,7 @@ pub fn get_follows_list() -> Result>, Box let self_id = sbot_client.whoami().await?; - let follows = sbot_client.follows(&self_id).await?; + let follows = sbot_client.get_follows(&self_id).await?; if !follows.is_empty() { for peer in follows.iter() { @@ -520,7 +520,7 @@ pub fn get_friends_list() -> Result>, Box let self_id = sbot_client.whoami().await?; - let friends = sbot_client.friends(&self_id).await?; + let friends = sbot_client.get_friends(&self_id).await?; if !friends.is_empty() { for peer in friends.iter() { diff --git a/tilde-client/.gitignore b/tilde-client/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/tilde-client/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/tilde-client/Cargo.toml b/tilde-client/Cargo.toml new file mode 100644 index 0000000..5c3023f --- /dev/null +++ b/tilde-client/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "tilde-client" +version = "0.0.1" +authors = ["Max Fowler "] +edition = "2018" + +[dependencies] +async-std = "1.10" +anyhow = "1.0.86" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +serde_yaml = "0.8" \ No newline at end of file diff --git a/tilde-client/README.md b/tilde-client/README.md new file mode 100644 index 0000000..7c05a99 --- /dev/null +++ b/tilde-client/README.md @@ -0,0 +1,37 @@ +# peach-lib + +![Generic badge](https://img.shields.io/badge/version-1.2.9-.svg) + +JSON-RPC client library for the PeachCloud ecosystem. + +`peach-lib` offers the ability to programmatically interact with the `peach-network`, `peach-oled` and `peach-stats` microservices. + +## Overview + +The `peach-lib` crate bundles JSON-RPC client code for making requests to the three PeachCloud microservices which expose JSON-RPC servers (`peach-network`, `peach-oled` and `peach-menu`). The full list of available RPC APIs can be found in the READMEs of the respective microservices ([peach-network](https://github.com/peachcloud/peach-network), [peach-oled](https://github.com/peachcloud/peach-oled), [peach-menu](https://github.com/peachcloud/peach-menu)), or in the [developer documentation for PeachCloud](http://docs.peachcloud.org/software/microservices/index.html). + +The library also includes a custom error type, `PeachError`, which bundles the underlying error types into three variants: `JsonRpcHttp`, `JsonRpcCore` and `Serde`. When used as the returned error type in a `Result` function response, this allows convenient use of the `?` operator (as illustrated in the example usage code below). + +## Usage + +Define the dependency in your `Cargo.toml` file: + +`peach-lib = { git = "https://github.com/peachcloud/peach-lib", branch = "main" }` + +Import the required client from the library: + +```rust +use peach_lib::network_client; +``` + +Call one of the exposed methods: + +```rust +network_client::ip("wlan0")?; +``` + +Further example usage can be found in the [`peach-menu`](https://github.com/peachcloud/peach-menu) code (see `src/states.rs`). + +## Licensing + +AGPL-3.0 diff --git a/tilde-client/src/error.rs b/tilde-client/src/error.rs new file mode 100644 index 0000000..14cadaf --- /dev/null +++ b/tilde-client/src/error.rs @@ -0,0 +1,18 @@ +#![warn(missing_docs)] + +use std::error::Error; +use std::fmt; + +/// all tilde client errors +#[derive(Debug)] +pub struct TildeError { + message: String, +} + +impl fmt::Display for TildeError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.message) + } +} + +impl Error for TildeError {} \ No newline at end of file diff --git a/tilde-client/src/lib.rs b/tilde-client/src/lib.rs new file mode 100644 index 0000000..38dee82 --- /dev/null +++ b/tilde-client/src/lib.rs @@ -0,0 +1,56 @@ +// methods for interacting with tilde sbot + +use crate::error::TildeError; + +use serde_json::Value; + +mod error; + +pub struct TildeClient { + name: String, + port: String +} +pub fn init_sbot() { + println!("++ init sbot!"); +} + +pub fn get_sbot_client() -> TildeClient { + TildeClient { + name: "name".to_string(), + port: "8009".to_string() + } +} + + +impl TildeClient { + pub async fn latest_description(&self, key: &str) -> Result { + todo!(); + } + + pub async fn whoami(&self) -> Result { + todo!(); + } + + pub async fn is_following(&self, from_id: &str, to_id: &str) -> Result { + todo!(); + } + + pub async fn create_block(&self, key: &str) -> Result { + todo!(); + } + pub async fn get_blocks(&self, key: &str) -> Result, TildeError> { + todo!(); + } + + pub async fn get_follows(&self, key: &str) -> Result, TildeError> { + todo!(); + } + + pub async fn get_friends(&self, key: &str) -> Result, TildeError> { + todo!(); + } + + pub async fn publish(&self, post: Value) -> Result, TildeError> { + todo!(); + } +} \ No newline at end of file