Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
api.key
api.key
api.key.*
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ members = [
"blocking",
"examples/event_printer",
"examples/system_info",
"examples/multi_client",
"examples/multi_client_async",
]
21 changes: 20 additions & 1 deletion async/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ use reqwest::Client as HttpClient;
use serde::Serialize;
use serde::de::DeserializeOwned as Deserialize;
use std::collections::HashMap;
use syncthing_types::FolderID;
use syncthing_types::events::{Event, EventType};
use syncthing_types::utils::construct_uri;
use syncthing_types::{API_DEFAULT_AUTHORITY, Timestamp};
use syncthing_types::{API_HEADER_KEY, routes::*};
use syncthing_types::{EMPTY_EVENT_SUBSCRIPTION, system};
use syncthing_types::{cluster, utils};
use syncthing_types::{cluster, config, db, utils};

pub struct Client {
client: HttpClient,
Expand All @@ -37,6 +38,10 @@ impl Client {
}
}

pub fn authority(&self) -> &Authority {
&self.authority
}

pub(crate) async fn get<D: Deserialize, T: AsRef<[u8]> + 'static>(
&self,
path_and_query: T,
Expand Down Expand Up @@ -156,4 +161,18 @@ impl Client {
pub async fn get_cluster_pending_devices(&self) -> Fallible<cluster::PendingDevices> {
self.get(CLUSTER_PENDING_DEVICES).await
}

pub async fn get_config_folders(&self) -> Fallible<Vec<config::Folder>> {
self.get(CONFIG_FOLDERS).await
}

pub async fn get_config_devices(&self) -> Fallible<Vec<config::Device>> {
self.get(CONFIG_DEVICES).await
}

pub async fn get_db_status(&self, folder_id: &FolderID) -> Fallible<db::Status> {
let mut string: String = DB_STATUS.to_string();
string.push_str(folder_id);
self.get(string).await
}
}
1 change: 1 addition & 0 deletions async/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod event_stream;
mod tests;

pub use client::Client;
pub use http::uri::Authority;

pub type Fallible<T> = Result<T, anyhow::Error>;
pub use syncthing_types::*;
18 changes: 16 additions & 2 deletions blocking/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use serde::de::DeserializeOwned as Deserialize;
use std::collections::HashMap;
use syncthing_types::events::{Event, EventType};
use syncthing_types::utils::construct_uri;
use syncthing_types::{API_DEFAULT_AUTHORITY, Timestamp};
use syncthing_types::{API_DEFAULT_AUTHORITY, FolderID, Timestamp};
use syncthing_types::{API_HEADER_KEY, routes::*};
use syncthing_types::{EMPTY_EVENT_SUBSCRIPTION, system};
use syncthing_types::{cluster, utils};
use syncthing_types::{cluster, config, db, utils};
use ureq::Agent;

pub struct Client {
Expand Down Expand Up @@ -149,4 +149,18 @@ impl Client {
pub fn get_cluster_pending_devices(&self) -> Fallible<cluster::PendingDevices> {
self.get(CLUSTER_PENDING_DEVICES)
}

pub fn get_config_folders(&self) -> Fallible<config::Folder> {
self.get(CONFIG_FOLDERS)
}

pub fn get_config_devices(&self) -> Fallible<config::Device> {
self.get(CONFIG_DEVICES)
}

pub fn get_db_status(&self, folder_id: &FolderID) -> Fallible<db::Status> {
let mut string = DB_STATUS.to_string();
string.push_str(folder_id);
self.get(string)
}
}
1 change: 1 addition & 0 deletions blocking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ mod tests;
pub use client::Client;

pub type Fallible<T> = Result<T, anyhow::Error>;
pub use http::uri::Authority;
pub use syncthing_types::*;
8 changes: 8 additions & 0 deletions examples/multi_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "multi_client"
version = "1.0.0"
authors = ["Dylan Sandall <dsandalledu@gmail.com>"]
edition = "2024"

[dependencies]
syncthing = { path = "../../blocking/" }
29 changes: 29 additions & 0 deletions examples/multi_client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use syncthing::{Authority, Client, Fallible};

fn main() -> Fallible<()> {
let mut clients: Vec<Client> = Vec::new();

clients.push(Client::with_authority(
include_str!("../../../api.key"),
Authority::from_static("localhost:8384"),
).unwrap());

clients.push(Client::with_authority(
include_str!("../../../api.key.deb"),
Authority::from_static("debian-server:8384"),
).unwrap());

for client in clients {
println!("client address is {}!", client.authority);

let _p = client.ping().unwrap();

let system = client.get_version_info().unwrap();
println!(
"syncthing {} is running on {:?}!\n>>> ({})!",
system.version, system.os, system.long_version
);
println!("");
}
Ok(())
}
9 changes: 9 additions & 0 deletions examples/multi_client_async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "multi_client_async"
version = "1.0.0"
authors = ["Dylan Sandall <dsandalledu@gmail.com>"]
edition = "2024"

[dependencies]
syncthing-async = { path = "../../async" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
30 changes: 30 additions & 0 deletions examples/multi_client_async/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use syncthing_async::{Authority, Client, Fallible};

#[tokio::main]
async fn main() -> Fallible<()> {
let mut clients: Vec<Client> = Vec::new();

clients.push(Client::with_authority(
include_str!("../../../api.key"),
Authority::from_static("localhost:8384"),
));

clients.push(Client::with_authority(
include_str!("../../../api.key.deb"),
Authority::from_static("debian-server:8384"),
));

for client in clients {
println!("client address is {}!", client.authority);

let _p = client.ping().await?;

let system = client.get_version_info().await?;
println!(
"syncthing {} is running on {:?}!\n>>> ({})!",
system.version, system.os, system.long_version
);
println!("");
}
Ok(())
}
10 changes: 10 additions & 0 deletions types/src/config/devices.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::DeviceID;
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Device {
#[serde(rename = "deviceID")]
pub id: DeviceID,
pub name: String,
pub addresses: Vec<String>,
}
17 changes: 17 additions & 0 deletions types/src/config/folders.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::{DeviceID, FolderID};
use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct Folder {
pub id: FolderID,
pub label: String,
pub path: String,
pub paused: bool,
pub devices: Vec<FolderDevice>,
}

#[derive(Deserialize, Debug)]
struct FolderDevice {
#[serde(rename = "deviceID")]
device_id: DeviceID,
}
5 changes: 5 additions & 0 deletions types/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod devices;
mod folders;

pub use devices::*;
pub use folders::*;
2 changes: 2 additions & 0 deletions types/src/db/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod status;
pub use status::*;
65 changes: 65 additions & 0 deletions types/src/db/status.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use crate::DeviceID;
use serde::Deserialize;
use std::collections::HashMap;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Status {
pub errors: u32,
pub pull_errors: u32,
pub invalid: String,

pub global_files: u64,
pub global_directories: u64,
pub global_symlinks: u64,
pub global_deleted: u64,
pub global_bytes: u64,
pub global_total_items: u64,

pub local_files: u64,
pub local_directories: u64,
pub local_symlinks: u64,
pub local_deleted: u64,
pub local_bytes: u64,
pub local_total_items: u64,

pub need_files: u64,
pub need_directories: u64,
pub need_symlinks: u64,
pub need_deletes: u64,
pub need_bytes: u64,
pub need_total_items: u64,

pub receive_only_changed_files: u64,
pub receive_only_changed_directories: u64,
pub receive_only_changed_symlinks: u64,
pub receive_only_changed_deletes: u64,
pub receive_only_changed_bytes: u64,
pub receive_only_total_items: u64,

pub in_sync_files: u64,
pub in_sync_bytes: u64,

pub state: FolderState,
pub state_changed: String,

pub error: String,

pub version: u64,
pub sequence: u64,

pub remote_sequence: HashMap<DeviceID, u64>,

pub ignore_patterns: bool,
pub watch_error: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum FolderState {
Idle,
Syncing,
Scanning,
Error,
Unknown,
}
5 changes: 2 additions & 3 deletions types/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{DeviceID, Timestamp};
use crate::{DeviceID, FolderID, Timestamp};
use crate::{FileName, Folder, FolderName};
use serde::{Deserialize, Serialize};
use serde_json::value::RawValue;
Expand All @@ -8,7 +8,6 @@ use std::collections::HashMap;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct ConfigSavedEvent {
#[serde(rename = "Version")]
pub version: u64,
}

Expand Down Expand Up @@ -80,7 +79,7 @@ pub struct FolderCompletionEvent {
#[serde(rename = "device")]
pub device_id: DeviceID,
#[serde(rename = "folder")]
pub folder_id: String,
pub folder_id: FolderID,
pub completion: f64,
pub global_bytes: u64,
pub need_bytes: u64,
Expand Down
3 changes: 3 additions & 0 deletions types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub mod cluster;
pub mod config;
pub mod db;
pub mod events;
pub mod routes;
pub mod system;
Expand All @@ -20,6 +22,7 @@ pub static EMPTY_EVENT_SUBSCRIPTION: Vec<crate::events::EventType> = Vec::new();
type FileName = String;
//TODO: use separate type?
type DeviceID = String;
pub type FolderID = String;
type FolderName = String;
type Folder = HashMap<FileName, File>;
pub type Timestamp = DateTime<FixedOffset>;
Expand Down
6 changes: 6 additions & 0 deletions types/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ pub static SYSTEM_UPGRADE: &str = "/rest/system/upgrade";
pub static SYSTEM_VERSION: &str = "/rest/system/version";

pub static CLUSTER_PENDING_DEVICES: &str = "/rest/cluster/pending/devices";

pub static CONFIG_ALL: &str = "/rest/config";
pub static CONFIG_FOLDERS: &str = "/rest/config/folders";
pub static CONFIG_DEVICES: &str = "/rest/config/devices";

pub static DB_STATUS: &str = "/rest/db/status?folder=";