use crate::client::Result;
use crate::http_client::HttpClient;
use reduct_base::msg::replication_api::ReplicationSettings;
use reqwest::Method;
use std::sync::Arc;
pub struct ReplicationBuilder {
name: String,
settings: ReplicationSettings,
http_client: Arc<HttpClient>,
}
impl ReplicationBuilder {
pub(super) fn new(name: String, http_client: Arc<HttpClient>) -> Self {
Self {
name,
settings: ReplicationSettings {
src_bucket: "".to_string(),
dst_bucket: "".to_string(),
dst_host: "".to_string(),
dst_token: Some("".to_string()), entries: vec![],
include: Default::default(),
exclude: Default::default(),
each_s: None,
each_n: None,
when: None,
},
http_client,
}
}
pub fn src_bucket(mut self, bucket: &str) -> Self {
self.settings.src_bucket = bucket.to_string();
self
}
pub fn dst_bucket(mut self, bucket: &str) -> Self {
self.settings.dst_bucket = bucket.to_string();
self
}
pub fn dst_host(mut self, host: &str) -> Self {
self.settings.dst_host = host.to_string();
self
}
pub fn dst_token(mut self, token: &str) -> Self {
self.settings.dst_token = Some(token.to_string());
self
}
pub fn entries(mut self, entries: Vec<String>) -> Self {
self.settings.entries = entries;
self
}
#[deprecated(
since = "1.15.0",
note = "Use `$each_t` operator in `when` condition. It will be removed in v1.18.0."
)]
pub fn each_s(mut self, each_s: f64) -> Self {
self.settings.each_s = Some(each_s);
self
}
#[deprecated(
since = "1.15.0",
note = "Use `$each_n` operator in `when` condition. It will be removed in v1.18.0."
)]
pub fn each_n(mut self, each_n: u64) -> Self {
self.settings.each_n = Some(each_n);
self
}
pub fn when(mut self, when: serde_json::Value) -> Self {
self.settings.when = Some(when);
self
}
pub fn set_settings(mut self, settings: ReplicationSettings) -> Self {
self.settings = settings;
self
}
pub async fn send(self) -> Result<()> {
self.http_client
.send_json(
Method::POST,
&format!("/replications/{}", self.name),
self.settings,
)
.await
}
}