use crate::client::Result;
use crate::http_client::HttpClient;
use reduct_base::msg::replication_api::{ReplicationMode, 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 {
let mut settings = ReplicationSettings::default();
settings.dst_token = Some("".to_string());
Self {
name,
settings,
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
}
pub fn when(mut self, when: serde_json::Value) -> Self {
self.settings.when = Some(when);
self
}
pub fn mode(mut self, mode: ReplicationMode) -> Self {
self.settings.mode = mode;
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
}
}