Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ tokio-util = "0.7"
croner = "3"
tabled = "0.17"
reqwest = { version = "0.13", features = ["json", "multipart", "form", "query"] }
# Pinned to the major reqwest 0.13 re-exports, so `http::Response` round-trips
# through `reqwest::Response` (used by the Gmail retry path to buffer a body).
http = "1"
quick-xml = { version = "0.41", features = ["serialize"] }
tokio-tungstenite = { version = "0.29", features = ["native-tls"] }
futures-util = "0.3"
Expand Down
1 change: 1 addition & 0 deletions crates/void-gmail/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ anyhow = { workspace = true }
thiserror = { workspace = true }
async-trait = { workspace = true }
reqwest = { workspace = true }
http = { workspace = true }
uuid = { workspace = true }
chrono = { workspace = true }
tokio-util = { workspace = true }
Expand Down
41 changes: 23 additions & 18 deletions crates/void-gmail/src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::time::Duration;
use crate::error::GmailError;
use tracing::{debug, info};

use super::retry::{RetryPolicy, SendRetrying};

use super::types::{
AttachmentResponse, DraftListResponse, GmailDraft, GmailMessage, GmailProfile, GmailThread,
HistoryListResponse, HistoryRecord, LabelListResponse, MessageListResponse, SendAsAlias,
Expand All @@ -26,6 +28,7 @@ pub struct GmailApiClient {
http: reqwest::Client,
access_token: String,
base_url: String,
retry: RetryPolicy,
}

impl GmailApiClient {
Expand All @@ -34,6 +37,7 @@ impl GmailApiClient {
http: build_http_client(),
access_token: access_token.to_string(),
base_url: DEFAULT_BASE_URL.to_string(),
retry: RetryPolicy::default(),
}
}

Expand All @@ -43,6 +47,7 @@ impl GmailApiClient {
http: build_http_client(),
access_token: access_token.to_string(),
base_url: base_url.to_string(),
retry: RetryPolicy::fast(),
}
}

Expand All @@ -56,7 +61,7 @@ impl GmailApiClient {
.http
.get(format!("{}/gmail/v1/users/me/profile", self.base_url))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?
.json()
.await?;
Expand Down Expand Up @@ -94,7 +99,7 @@ impl GmailApiClient {
.get(format!("{}/gmail/v1/users/me/messages", self.base_url))
.bearer_auth(&self.access_token)
.query(&params)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?;
let resp: MessageListResponse = resp.json().await?;
Expand All @@ -117,7 +122,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.query(&[("format", "full")])
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?;
let resp: GmailMessage = resp.json().await?;
Expand Down Expand Up @@ -148,7 +153,7 @@ impl GmailApiClient {
.get(format!("{}/gmail/v1/users/me/history", self.base_url))
.bearer_auth(&self.access_token)
.query(&params)
.send()
.send_retrying(&self.retry)
.await?;
// Gmail returns 404 once the startHistoryId is too old (history is
// only kept for a limited window). Surface that distinctly so the
Expand Down Expand Up @@ -210,7 +215,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.json()
.await?;
Expand All @@ -226,7 +231,7 @@ impl GmailApiClient {
.post(format!("{}/gmail/v1/users/me/messages/send", self.base_url))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.json()
.await?;
Expand All @@ -244,7 +249,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.query(&[("format", "full")])
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -267,7 +272,7 @@ impl GmailApiClient {
self.base_url
))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -282,7 +287,7 @@ impl GmailApiClient {
.http
.get(format!("{}/gmail/v1/users/me/labels", self.base_url))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand Down Expand Up @@ -316,7 +321,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand Down Expand Up @@ -349,7 +354,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?;
debug!("gmail: batch_modify ok");
Expand All @@ -363,7 +368,7 @@ impl GmailApiClient {
.get(format!("{}/gmail/v1/users/me/drafts", self.base_url))
.bearer_auth(&self.access_token)
.query(&[("maxResults", max_results.to_string())])
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -383,7 +388,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.query(&[("format", "full")])
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -408,7 +413,7 @@ impl GmailApiClient {
.post(format!("{}/gmail/v1/users/me/drafts", self.base_url))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -430,7 +435,7 @@ impl GmailApiClient {
))
.bearer_auth(&self.access_token)
.json(&body)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?
.json()
Expand All @@ -447,7 +452,7 @@ impl GmailApiClient {
self.base_url
))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?
.error_for_status()?;
debug!(draft_id, "gmail: delete_draft ok");
Expand All @@ -464,7 +469,7 @@ impl GmailApiClient {
self.base_url
))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?;
let resp: SendAsListResponse = Self::json_or_scope_error(resp).await?;
let count = resp.send_as.as_ref().map(|s| s.len()).unwrap_or(0);
Expand All @@ -483,7 +488,7 @@ impl GmailApiClient {
self.base_url
))
.bearer_auth(&self.access_token)
.send()
.send_retrying(&self.retry)
.await?;
let resp: SendAsAlias = Self::json_or_scope_error(resp).await?;
debug!(send_as_email, "gmail: get_send_as ok");
Expand Down
2 changes: 2 additions & 0 deletions crates/void-gmail/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod client;
mod message;
mod retry;
mod types;

#[cfg(test)]
mod tests;

pub use client::{build_http_client, GmailApiClient};
pub use message::decode_attachment_data;
pub use retry::RetryPolicy;
pub use types::*;
Loading