♻️ refactor(message_list): use gmail client for label retrieval
- removes credential file and Labels struct from add_labels function - uses GmailClient to get label id - simplifies label adding process
This commit is contained in:
committed by
Jeremiah Russell
parent
ebd5d56603
commit
ceb4548649
@@ -1,23 +1,17 @@
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
use crate::{Labels, Result};
|
use crate::{GmailClient, Result};
|
||||||
|
|
||||||
use google_gmail1::{
|
use google_gmail1::{
|
||||||
Gmail,
|
Gmail, api::ListMessagesResponse, hyper_rustls::HttpsConnector,
|
||||||
api::ListMessagesResponse,
|
hyper_util::client::legacy::connect::HttpConnector,
|
||||||
hyper_rustls::{HttpsConnector, HttpsConnectorBuilder},
|
|
||||||
hyper_util::{
|
|
||||||
client::legacy::{Client, connect::HttpConnector},
|
|
||||||
rt::TokioExecutor,
|
|
||||||
},
|
|
||||||
yup_oauth2::{ApplicationSecret, InstalledFlowAuthenticator, InstalledFlowReturnMethod},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
mod message_summary;
|
mod message_summary;
|
||||||
|
|
||||||
use message_summary::MessageSummary;
|
use message_summary::MessageSummary;
|
||||||
|
|
||||||
use crate::{Credential, utils::Elide};
|
use crate::utils::Elide;
|
||||||
|
|
||||||
/// Default for the maximum number of results to return on a page
|
/// Default for the maximum number of results to return on a page
|
||||||
pub const DEFAULT_MAX_RESULTS: &str = "200";
|
pub const DEFAULT_MAX_RESULTS: &str = "200";
|
||||||
@@ -44,36 +38,9 @@ impl Debug for MessageList {
|
|||||||
|
|
||||||
impl MessageList {
|
impl MessageList {
|
||||||
/// Create a new List struct and add the Gmail api connection.
|
/// Create a new List struct and add the Gmail api connection.
|
||||||
pub async fn new(credential: &str) -> Result<Self> {
|
pub async fn new(client: &GmailClient) -> Result<Self> {
|
||||||
let (config_dir, secret) = {
|
|
||||||
let config_dir = crate::utils::assure_config_dir_exists("~/.cull-gmail")?;
|
|
||||||
|
|
||||||
let secret: ApplicationSecret = Credential::load_json_file(credential).into();
|
|
||||||
(config_dir, secret)
|
|
||||||
};
|
|
||||||
|
|
||||||
let executor = TokioExecutor::new();
|
|
||||||
let connector = HttpsConnectorBuilder::new()
|
|
||||||
.with_native_roots()
|
|
||||||
.unwrap()
|
|
||||||
.https_or_http()
|
|
||||||
.enable_http1()
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let client = Client::builder(executor.clone()).build(connector.clone());
|
|
||||||
|
|
||||||
let auth = InstalledFlowAuthenticator::with_client(
|
|
||||||
secret,
|
|
||||||
InstalledFlowReturnMethod::HTTPRedirect,
|
|
||||||
Client::builder(executor).build(connector),
|
|
||||||
)
|
|
||||||
.persist_tokens_to_disk(format!("{config_dir}/gmail1"))
|
|
||||||
.build()
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
Ok(MessageList {
|
Ok(MessageList {
|
||||||
hub: Gmail::new(client, auth),
|
hub: client.hub(),
|
||||||
max_results: DEFAULT_MAX_RESULTS.parse::<u32>().unwrap(),
|
max_results: DEFAULT_MAX_RESULTS.parse::<u32>().unwrap(),
|
||||||
label_ids: Vec::new(),
|
label_ids: Vec::new(),
|
||||||
messages: Vec::new(),
|
messages: Vec::new(),
|
||||||
@@ -92,15 +59,11 @@ impl MessageList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add label to the labels collection
|
/// Add label to the labels collection
|
||||||
pub async fn add_labels(&mut self, credential_file: &str, labels: &[String]) -> Result<()> {
|
pub async fn add_labels(&mut self, client: GmailClient, labels: &[String]) -> Result<()> {
|
||||||
// add labels if any specified
|
|
||||||
let label_list = Labels::new(credential_file, false).await?;
|
|
||||||
|
|
||||||
log::trace!("labels found and setup {label_list:#?}");
|
|
||||||
log::debug!("labels from command line: {labels:?}");
|
log::debug!("labels from command line: {labels:?}");
|
||||||
let mut label_ids = Vec::new();
|
let mut label_ids = Vec::new();
|
||||||
for label in labels {
|
for label in labels {
|
||||||
if let Some(id) = label_list.get_label_id(label) {
|
if let Some(id) = client.get_label_id(label) {
|
||||||
label_ids.push(id)
|
label_ids.push(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user