use std::collections::HashMap; use std::fmt::{Debug, Formatter}; use chrono::{DateTime, Utc}; use google_drive3::hyper_util::client::legacy::connect::HttpConnector; use google_drive3::{hyper_util, DriveHub}; use yup_oauth2::hyper_rustls; use yup_oauth2::hyper_rustls::HttpsConnector; use crate::prelude::*; #[derive(Clone)] pub struct GoogleDrive { pub hub: DriveHub>, } impl Debug for GoogleDrive { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "GoogleDrive") } } impl GoogleDrive { pub async fn new() -> Result { //TODO3: maybe change the path where the auth tokens get stored let auth = yup_oauth2::read_application_secret("auth/client_secret.json").await?; let auth = yup_oauth2::InstalledFlowAuthenticator::builder( auth, yup_oauth2::InstalledFlowReturnMethod::HTTPRedirect, ) .persist_tokens_to_disk("auth/tokens.json") .build() .await?; let client = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()) .build( hyper_rustls::HttpsConnectorBuilder::new() .with_native_roots()? .https_or_http() .enable_http1() .enable_http2() .build(), ); Ok(Self { hub: DriveHub::new(client, auth), }) } } #[derive(Debug)] pub struct Drive { tracked_files: HashMap>, pub hub: GoogleDrive, } impl Drive { pub async fn new() -> Result { let hub = GoogleDrive::new().await?; Ok(Self { tracked_files: HashMap::new(), hub, }) } pub async fn check_connection(&self) -> bool { self.hub .hub .about() .get() .param("fields", "user") .doit() .await .inspect(|x| { dbg!(x); }) .is_ok() } pub fn get_file_tracking_state(&self, id: &DriveId) -> TrackingState { let file = self.tracked_files.get(id); match file { Some(date) => TrackingState::Tracked(*date), None => TrackingState::Untracked, } } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum TrackingState { Untracked, Tracked(DateTime), }