use reqwest::Client;
use crate::types::DriveError;
const FILES_ENDPOINT: &str = "https://www.googleapis.com/drive/v3/files";
pub async fn download_file(
client: &Client,
access_token: &str,
file_id: &str,
) -> Result<Vec<u8>, DriveError> {
let url = format!("{}/{}", FILES_ENDPOINT, file_id);
let resp = client
.get(&url)
.bearer_auth(access_token)
.query(&[("alt", "media")])
.send()
.await?;
let status = resp.status().as_u16();
if !resp.status().is_success() {
let msg = resp.text().await.unwrap_or_default();
return Err(DriveError::Api {
status,
message: msg,
});
}
let bytes = resp.bytes().await?;
Ok(bytes.to_vec())
}
pub async fn export_file(
client: &Client,
access_token: &str,
file_id: &str,
export_mime_type: &str,
) -> Result<Vec<u8>, DriveError> {
let url = format!("{}/{}/export", FILES_ENDPOINT, file_id);
let resp = client
.get(&url)
.bearer_auth(access_token)
.query(&[("mimeType", export_mime_type)])
.send()
.await?;
let status = resp.status().as_u16();
if !resp.status().is_success() {
let msg = resp.text().await.unwrap_or_default();
return Err(DriveError::Api {
status,
message: msg,
});
}
let bytes = resp.bytes().await?;
Ok(bytes.to_vec())
}