use reqwest::Method;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::client::{CallOptions, Client, Payload};
use crate::error::Error;
use crate::page::{Deleted, ListParams, Page};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FileMetadata {
pub id: String,
#[serde(default)]
pub filename: String,
#[serde(default)]
pub mime_type: String,
#[serde(default)]
pub size_bytes: u64,
#[serde(default)]
pub created_at: String,
#[serde(default)]
pub downloadable: bool,
#[serde(flatten)]
pub extra: Map<String, Value>,
}
impl Client {
pub async fn upload_file(&self, filename: &str, mime_type: &str, data: &[u8]) -> Result<FileMetadata, Error> {
self.call(Method::POST, &["v1", "files"], &[], Payload::File { filename, mime_type, data }, &[], CallOptions::default()).await
}
pub async fn list_files(&self, params: &ListParams) -> Result<Page<FileMetadata>, Error> {
self.call(Method::GET, &["v1", "files"], ¶ms.query(), Payload::Empty, &[], CallOptions::default()).await
}
pub async fn file(&self, id: &str) -> Result<FileMetadata, Error> {
self.call(Method::GET, &["v1", "files", id], &[], Payload::Empty, &[], CallOptions::default()).await
}
pub async fn download_file(&self, id: &str) -> Result<Vec<u8>, Error> {
self.call_bytes(Method::GET, &["v1", "files", id, "content"], &[], Payload::Empty, &[], CallOptions::default()).await
}
pub async fn delete_file(&self, id: &str) -> Result<Deleted, Error> {
self.call(Method::DELETE, &["v1", "files", id], &[], Payload::Empty, &[], CallOptions::default()).await
}
}