use super::*;
impl ApiClient {
pub async fn list_files(
&self,
service_environment_id: &str,
path: Option<&str>,
) -> Result<Answer<DirectoryListing>> {
let query = path.map_or_else(String::new, |path| {
format!("?path={}", utf8_percent_encode(path, NON_ALPHANUMERIC))
});
self.get_answer(&format!(
"/api/infra/services/{service_environment_id}/files{query}"
))
.await
}
pub async fn preview_file(
&self,
service_environment_id: &str,
path: &str,
) -> Result<Answer<FilePreview>> {
self.get_answer(&format!(
"/api/infra/services/{service_environment_id}/files/preview?path={}",
utf8_percent_encode(path, NON_ALPHANUMERIC)
))
.await
}
pub async fn download_file(
&self,
service_environment_id: &str,
path: &str,
into: &std::path::Path,
) -> Result<u64> {
let response = self
.request(
Method::GET,
&format!(
"/api/infra/services/{service_environment_id}/files/download?path={}",
utf8_percent_encode(path, NON_ALPHANUMERIC)
),
None::<&()>,
)
.send()
.await?;
if !response.status().is_success() {
Self::check(response).await?;
bail!("the server refused the file");
}
let bytes = response.bytes().await.context("could not read the file")?;
std::fs::write(into, &bytes)
.with_context(|| format!("could not write {}", into.display()))?;
Ok(bytes.len() as u64)
}
}