use serde::Serialize;
use serde_json::json;
use crate::errors::Result;
use crate::transport::Transport;
use super::types::{DriveFile, DriveFileList, DriveListFilesParams, DriveUploadFileParams};
const PROVIDER: &str = "google_drive";
#[derive(Debug, Clone)]
pub struct Drive {
transport: Transport,
}
impl Drive {
pub(crate) fn new(transport: Transport) -> Self {
Self { transport }
}
pub async fn list_files(&self, params: DriveListFilesParams) -> Result<DriveFileList> {
let raw = self
.transport
.integrations_call(PROVIDER, "list-files", ¶ms)
.await?;
decode(raw)
}
pub async fn get_file(&self, file_id: &str) -> Result<DriveFile> {
let raw = self
.transport
.integrations_call(PROVIDER, "get-file", &json!({ "fileId": file_id }))
.await?;
decode(raw)
}
pub async fn download_file(&self, file_id: &str) -> Result<serde_json::Value> {
self.transport
.integrations_call(PROVIDER, "download-file", &json!({ "fileId": file_id }))
.await
}
pub async fn create_folder(&self, name: &str, parent_id: Option<&str>) -> Result<DriveFile> {
#[derive(Serialize)]
struct Body<'a> {
name: &'a str,
#[serde(rename = "parentId", skip_serializing_if = "Option::is_none")]
parent_id: Option<&'a str>,
}
let raw = self
.transport
.integrations_call(PROVIDER, "create-folder", &Body { name, parent_id })
.await?;
decode(raw)
}
pub async fn upload_file(&self, params: DriveUploadFileParams) -> Result<DriveFile> {
let raw = self
.transport
.integrations_call(PROVIDER, "upload-file", ¶ms)
.await?;
decode(raw)
}
pub async fn delete_file(&self, file_id: &str) -> Result<serde_json::Value> {
self.transport
.integrations_call(PROVIDER, "delete-file", &json!({ "fileId": file_id }))
.await
}
pub async fn search_files(
&self,
query: &str,
max_results: Option<u32>,
) -> Result<DriveFileList> {
#[derive(Serialize)]
struct Body<'a> {
query: &'a str,
#[serde(rename = "maxResults", skip_serializing_if = "Option::is_none")]
max_results: Option<u32>,
}
let raw = self
.transport
.integrations_call(PROVIDER, "search-files", &Body { query, max_results })
.await?;
decode(raw)
}
}
fn decode<T: serde::de::DeserializeOwned + Default>(raw: serde_json::Value) -> Result<T> {
if raw.is_null() {
return Ok(T::default());
}
serde_json::from_value(raw).map_err(|e| crate::errors::LeashError::MalformedResponse {
message: format!("Failed to deserialise Drive response: {e}"),
})
}