use reqwest::Method;
use std::sync::Arc;
use crate::client::ClientInner;
use crate::error::Result;
use crate::types::FileInfo;
pub struct FileService {
client: Arc<ClientInner>,
}
impl FileService {
pub(crate) fn new(client: Arc<ClientInner>) -> Self {
Self { client }
}
pub async fn list(&self, container_id: &str, path: &str) -> Result<Vec<FileInfo>> {
let encoded_path = urlencoding::encode(path);
self.client
.request(
Method::GET,
&format!("/api/containers/{}/files/list?path={}", container_id, encoded_path),
)
.await
}
pub async fn download(&self, container_id: &str, path: &str) -> Result<Vec<u8>> {
let encoded_path = urlencoding::encode(path);
let response = self
.client
.raw_request(
Method::GET,
&format!("/api/containers/{}/files?path={}", container_id, encoded_path),
None::<&()>,
)
.await?;
Ok(response.bytes().await?.to_vec())
}
pub async fn mkdir(&self, container_id: &str, path: &str) -> Result<()> {
#[derive(serde::Serialize)]
struct MkdirRequest<'a> {
path: &'a str,
}
self.client
.request_with_body::<serde_json::Value, _>(
Method::POST,
&format!("/api/containers/{}/files/mkdir", container_id),
&MkdirRequest { path },
)
.await?;
Ok(())
}
pub async fn delete(&self, container_id: &str, path: &str) -> Result<()> {
let encoded_path = urlencoding::encode(path);
self.client
.request_empty(
Method::DELETE,
&format!("/api/containers/{}/files?path={}", container_id, encoded_path),
)
.await
}
}