use crate::Error;
use crate::client::UnifiedClient;
use log::debug;
#[derive(Debug, Clone)]
pub struct DavAccount {
pub id: String,
pub name: String,
pub uri: Option<String>,
pub server: Option<String>,
pub password: Option<String>,
pub created_at: String,
}
#[derive(Debug)]
pub struct DavListResponse {
pub accounts: Vec<DavAccount>,
}
impl super::CloudreveAPI {
pub async fn list_dav_accounts(&self, page_size: u32) -> Result<DavListResponse, Error> {
debug!("Listing WebDAV accounts with page_size: {}", page_size);
match &self.inner {
UnifiedClient::V3(client) => {
let accounts = client.get_webdav_accounts().await?;
let dav_accounts = accounts
.into_iter()
.map(|acc| DavAccount {
id: acc.id.to_string(),
name: acc.name,
uri: Some(acc.uri),
server: None,
password: Some(acc.password),
created_at: acc.created_at,
})
.collect();
Ok(DavListResponse {
accounts: dav_accounts,
})
}
UnifiedClient::V4(client) => {
let response = client.list_dav_accounts(page_size, None).await?;
let dav_accounts = response
.accounts
.into_iter()
.map(|acc| DavAccount {
id: acc.id.to_string(),
name: acc.name,
uri: Some(acc.uri),
server: None,
password: Some(acc.password),
created_at: acc.created_at,
})
.collect();
Ok(DavListResponse {
accounts: dav_accounts,
})
}
}
}
pub async fn create_dav_account(
&self,
uri: &str,
name: &str,
readonly: bool,
proxy: bool,
) -> Result<(), Error> {
debug!("Creating WebDAV account: {} at {}", name, uri);
match &self.inner {
UnifiedClient::V3(_) => Err(Error::UnsupportedFeature(
"create WebDAV account".to_string(),
"v3".to_string(),
)),
UnifiedClient::V4(client) => {
let request = crate::api::v4::models::CreateDavAccountRequest {
uri: uri.to_string(),
name: name.to_string(),
readonly: Some(readonly),
proxy: Some(proxy),
disable_sys_files: None,
};
client.create_dav_account(&request).await?;
Ok(())
}
}
}
pub async fn update_dav_account(
&self,
id: &str,
uri: Option<&str>,
name: Option<&str>,
readonly: Option<bool>,
proxy: Option<bool>,
) -> Result<(), Error> {
debug!("Updating WebDAV account: {}", id);
match &self.inner {
UnifiedClient::V3(_) => Err(Error::UnsupportedFeature(
"update WebDAV account".to_string(),
"v3".to_string(),
)),
UnifiedClient::V4(client) => {
let current_list = client.list_dav_accounts(100, None).await?;
let current = current_list
.accounts
.iter()
.find(|a| a.id == id)
.ok_or_else(|| {
Error::InvalidResponse(format!("WebDAV account '{}' not found", id))
})?;
let request = crate::api::v4::models::CreateDavAccountRequest {
uri: uri.unwrap_or(¤t.uri).to_string(),
name: name.unwrap_or(¤t.name).to_string(),
readonly,
proxy,
disable_sys_files: None,
};
client.update_dav_account(id, &request).await?;
Ok(())
}
}
}
pub async fn delete_dav_account(&self, id: &str) -> Result<(), Error> {
debug!("Deleting WebDAV account: {}", id);
match &self.inner {
UnifiedClient::V3(_) => Err(Error::UnsupportedFeature(
"delete WebDAV account".to_string(),
"v3".to_string(),
)),
UnifiedClient::V4(client) => {
client.delete_dav_account(id).await?;
Ok(())
}
}
}
}