class GmailClient: """Authenticated Gmail client for one account (API or XOAUTH2 SMTP).""" def __init__(self, account_email: str, access_token: str, refresh_token: str, client_config: dict, sending_method: str = "api", token_expiry=None): self.account_email = account_email self.client_config = client_config self.method = sending_method if sending_method in ("api", "smtp") else "api" creds = gcred.Credentials( token=access_token, refresh_token=refresh_token or None, token_uri=GOOGLE_TOKEN_URL, client_id=client_config["client_id"], client_secret=client_config["client_secret"], scopes=GMAIL_SCOPES, ) self._creds = creds self._service = None self.token_expiry = token_expiry def _ensure_valid(self): request = google.auth.transport.requests.Request() if not self._creds.valid: self._creds.refresh(request) return self._creds.token def _api(self): if self._service is None: