aipype-g - Google API Integrations
The aipype-g package provides seamless integration with Google APIs, enabling aipype pipelines to work with Gmail, Google Sheets, and other Google services.
Note
This package requires Google API credentials and is designed for workflows that need to interact with Google services.
Package Overview
aipype-g: Google API integrations for the aipype framework.
- class GoogleOAuthTask[source]
Bases:
BaseTaskTask that handles Google OAuth2 authentication for specified services.
- __init__(name, config, dependencies=None)[source]
Initialize Google OAuth task.
- Parameters:
name (
str) – Task nameconfig (
Dict[str,Any]) – Configuration dictionary containing: - scopes: List of Google API scopes (optional, defaults to Gmail+Sheets) - service_types: List of service types [“gmail”, “sheets”, “drive”] (alternative to scopes) - credentials_file: Path to OAuth2 credentials file (optional) - token_file: Path to OAuth2 token file (optional)dependencies (
Optional[List[TaskDependency]]) – List of task dependencies
- static create_combined_auth_config(credentials_file=None, token_file=None)[source]
Helper to create configuration for Gmail + Sheets authentication.
- static create_custom_scopes_config(scopes, credentials_file=None, token_file=None)[source]
Helper to create configuration with custom scopes.
- static create_gmail_auth_config(credentials_file=None, token_file=None)[source]
Helper to create configuration for Gmail-only authentication.
- static create_sheets_auth_config(credentials_file=None, token_file=None)[source]
Helper to create configuration for Sheets-only authentication.
- class GmailListEmailsTask[source]
Bases:
BaseTaskTask that lists Gmail messages with optional filters and search queries.
- __init__(name, config, dependencies=None)[source]
Initialize Gmail list emails task.
- Parameters:
name (
str) – Task nameconfig (
Dict[str,Any]) – Configuration dictionary containing: - query: Gmail search query (optional, can be resolved from dependencies) - max_results: Maximum number of messages to return (default: 10) - include_spam_trash: Include spam and trash messages (default: False) - label_ids: List of label IDs to filter by (optional) - credentials_file: Path to OAuth2 credentials file (optional) - token_file: Path to OAuth2 token file (optional) - parse_messages: Whether to parse full message content (default: True)dependencies (
Optional[List[TaskDependency]]) – List of task dependencies
- static create_search_query(**kwargs)[source]
Helper method to create Gmail search queries using common patterns.
- Supported kwargs:
from_sender: str - Filter by sender email
to_recipient: str - Filter by recipient email
subject: str - Filter by subject
newer_than_days: int - Messages newer than X days
older_than_days: int - Messages older than X days
has_attachment: bool - Messages with attachments
is_unread: bool - Unread messages
is_important: bool - Important messages
is_starred: bool - Starred messages
label: str - Messages with specific label
custom_query: str - Additional custom query terms
- get_dependencies()[source]
Get the list of task dependencies.
- Return type:
- Returns:
List of TaskDependency objects
- run()[source]
List Gmail messages based on query and filters.
- Returns:
messages: List of GmailMessage objects (if parse_messages=True)
message_ids: List of message IDs (if parse_messages=False)
search_result: GmailSearchResult object with metadata
query_used: The Gmail query that was executed
total_found: Total number of messages found
retrieved_count: Number of messages actually retrieved
- Return type:
TaskResult containing
- class GmailReadEmailTask[source]
Bases:
BaseTaskTask that reads specific Gmail message content by ID.
- __init__(name, config, dependencies=None)[source]
Initialize Gmail read email task.
- Parameters:
name (
str) – Task nameconfig (
Dict[str,Any]) – Configuration dictionary containing: - message_id: Gmail message ID to read (can be resolved from dependencies) - message_ids: List of message IDs to read (alternative to message_id) - format: Message format (“full”, “minimal”, “raw”, “metadata”) (default: “full”) - include_attachments: Whether to include attachment info (default: True) - credentials_file: Path to OAuth2 credentials file (optional) - token_file: Path to OAuth2 token file (optional)dependencies (
Optional[List[TaskDependency]]) – List of task dependencies
- static create_batch_read_config(message_ids, format='full', include_attachments=True, credentials_file=None, token_file=None)[source]
Helper method to create configuration for reading multiple messages.
- Parameters:
- Return type:
- Returns:
Configuration dictionary for GmailReadEmailTask
- static extract_message_ids_from_list_result(list_result_data)[source]
Helper method to extract message IDs from GmailListEmailsTask result.
- get_dependencies()[source]
Get the list of task dependencies.
- Return type:
- Returns:
List of TaskDependency objects
- run()[source]
Read Gmail message(s) content.
- Returns:
message: GmailMessage object (if single message_id)
messages: List of GmailMessage objects (if multiple message_ids)
message_count: Number of messages read
failed_reads: List of failed message IDs with error details
success_count: Number of successfully read messages
format_used: Message format that was used
- Return type:
TaskResult containing
- class ReadGoogleSheetTask[source]
Bases:
BaseTaskTask that reads data from Google Sheets and returns as 2D arrays.
- __init__(name, config, dependencies=None)[source]
Initialize Read Google Sheet task.
- Parameters:
name (
str) – Task nameConfiguration dictionary containing:
spreadsheet_id: Google Spreadsheet ID (required)
range: Range in A1 notation (e.g., ‘Sheet1!A1:C10’) (optional)
sheet_name: Name of sheet to read (alternative to range) (optional)
start_row: Starting row number (1-based) (optional, default: 1)
end_row: Ending row number (1-based) (optional)
start_col: Starting column number (1-based) (optional, default: 1)
end_col: Ending column number (1-based) (optional)
include_headers: Whether first row contains headers (default: True)
credentials: Pre-authenticated credentials (can be resolved from dependencies)
credentials_file: Path to OAuth2 credentials file (optional)
token_file: Path to OAuth2 token file (optional)
timeout: Request timeout in seconds (default: 30)
dependencies (
Optional[List[TaskDependency]]) – List of task dependencies
- static create_full_sheet_config(spreadsheet_id, sheet_name, include_headers=True, credentials_file=None, token_file=None)[source]
Helper to create configuration for reading an entire sheet.
- Parameters:
- Return type:
- Returns:
Configuration dictionary for ReadGoogleSheetTask
- static create_range_config(spreadsheet_id, range_a1, include_headers=True, credentials_file=None, token_file=None)[source]
Helper to create configuration for reading a specific range.
- Parameters:
- Return type:
- Returns:
Configuration dictionary for ReadGoogleSheetTask
- static create_sheet_config(spreadsheet_id, sheet_name, start_row=1, end_row=None, start_col=1, end_col=None, include_headers=True, credentials_file=None, token_file=None)[source]
Helper to create configuration for reading a sheet with bounds.
- Parameters:
- Return type:
- Returns:
Configuration dictionary for ReadGoogleSheetTask
- get_dependencies()[source]
Get the list of task dependencies.
- Return type:
- Returns:
List of TaskDependency objects
- run()[source]
Read data from Google Sheet.
- Returns:
sheet_data: SheetData object (serialized)
values: 2D array of cell values
headers: List of header names (if include_headers=True)
num_rows: Number of data rows
num_cols: Number of columns
shape: Tuple of (rows, cols)
spreadsheet_info: Metadata about the spreadsheet
- Return type:
TaskResult containing
- class GmailService[source]
Bases:
objectGmail API service with OAuth2 authentication and common operations.
- Parameters:
- __init__(credentials=None, credentials_file=None, token_file=None, scopes=None, timeout=30)[source]
Initialize Gmail service.
- Parameters:
credentials (
Optional[Credentials]) – Pre-authenticated Google credentials (from GoogleOAuthTask)credentials_file (
Optional[str]) – Path to OAuth2 credentials JSON filetoken_file (
Optional[str]) – Path to store/load OAuth2 tokensscopes (
Optional[List[str]]) – List of Gmail API scopes to requesttimeout (
int) – Request timeout in seconds (default: 30)
- create_label(name, message_list_visibility='show', label_list_visibility='labelShow')[source]
Create a new Gmail label.
- Parameters:
- Return type:
- Returns:
Created GmailLabel object
- Raises:
GmailServiceError – If creation fails
- get_message(message_id, format='full', progress_callback=None)[source]
Get a specific Gmail message by ID.
- Parameters:
- Return type:
- Returns:
Message data dictionary
- Raises:
GmailServiceError – If API call fails
- list_labels()[source]
List all Gmail labels.
- Return type:
- Returns:
List of GmailLabel objects
- Raises:
GmailServiceError – If API call fails
- list_messages(query='', max_results=10, label_ids=None, include_spam_trash=False, progress_callback=None)[source]
List Gmail messages with optional query and filters.
- Parameters:
query (
str) – Gmail search query (same syntax as Gmail search)max_results (
int) – Maximum number of messages to returnlabel_ids (
Optional[List[str]]) – List of label IDs to filter byinclude_spam_trash (
bool) – Whether to include spam and trashprogress_callback (
Optional[Callable[[str],None]]) – Optional callback to report progress
- Return type:
- Returns:
List of message metadata dictionaries
- Raises:
GmailServiceError – If API call fails
- modify_message_labels(message_id, add_label_ids=None, remove_label_ids=None)[source]
Add or remove labels from a message.
- parse_message(message_data)[source]
Parse Gmail API message data into GmailMessage object.
- send_message(to, subject, body, html_body=None, cc=None, bcc=None, reply_to_message_id=None)[source]
Send an email message.
- class GoogleSheetsService[source]
Bases:
objectGoogle Sheets API service with OAuth2 authentication and read operations.
- Parameters:
- __init__(credentials=None, credentials_file=None, token_file=None, timeout=30)[source]
Initialize Google Sheets service.
- Parameters:
credentials (
Optional[Credentials]) – Pre-authenticated Google credentials (from GoogleOAuthTask)credentials_file (
Optional[str]) – Path to OAuth2 credentials JSON file (fallback)token_file (
Optional[str]) – Path to store/load OAuth2 tokens (fallback)timeout (
int) – Request timeout in seconds (default: 30)
- get_sheet_names(spreadsheet_id, progress_callback=None)[source]
Get list of sheet names in a spreadsheet.
- get_spreadsheet_info(spreadsheet_id, progress_callback=None)[source]
Get information about a spreadsheet.
- read_all_data(spreadsheet_id, sheet_name, include_headers=True, progress_callback=None)[source]
Read all data from a sheet.
- Parameters:
- Return type:
- Returns:
SheetData object with all data from the sheet
- Raises:
GoogleSheetsError – If API call fails
- read_range(spreadsheet_id, range_a1, include_headers=True, progress_callback=None)[source]
Read data from a specific range in a spreadsheet.
- Parameters:
- Return type:
- Returns:
SheetData object with the read data
- Raises:
GoogleSheetsError – If API call fails or range is invalid
- read_sheet(spreadsheet_id, sheet_name, start_row=1, end_row=None, start_col=1, end_col=None, include_headers=True, progress_callback=None)[source]
Read data from a sheet with row/column bounds.
- Parameters:
spreadsheet_id (
str) – ID of the Google Spreadsheetsheet_name (
str) – Name of the sheet to readstart_row (
int) – Starting row (1-based, default: 1)start_col (
int) – Starting column (1-based, default: 1)include_headers (
bool) – Whether first row should be treated as headersprogress_callback (
Optional[Callable[[str],None]]) – Optional callback for progress updates
- Return type:
- Returns:
SheetData object with the read data
- Raises:
GoogleSheetsError – If API call fails
- class GoogleAuthService[source]
Bases:
objectUnified Google OAuth2 authentication service for multiple Google APIs.
- Parameters:
- __init__(credentials_file=None, token_file=None, scopes=None)[source]
Initialize Google authentication service.
- authenticate()[source]
Authenticate with Google APIs using OAuth2.
- Return type:
Credentials- Returns:
Authenticated Google credentials object
- Raises:
GoogleAuthError – If authentication fails
- static create_service_with_scopes(service_types, credentials_file=None, token_file=None)[source]
Create authentication service with scopes for specific service types.
- get_credentials()[source]
Get authenticated credentials, authenticating if necessary.
- Return type:
Credentials- Returns:
Authenticated Google credentials object
- has_gmail_access()[source]
Check if Gmail scopes are included.
- Return type:
- Returns:
True if any Gmail scope is included
- has_sheets_access()[source]
Check if Sheets scopes are included.
- Return type:
- Returns:
True if any Sheets scope is included
- class GmailMessage[source]
Bases:
objectRepresents a Gmail message with parsed content.
- Parameters:
- __init__(message_id, thread_id, label_ids, snippet, subject, sender, recipient, date, body, history_id=None, internal_date=None, size_estimate=0, cc='', bcc='', html_body='', attachments=<factory>, headers=<factory>)
-
attachments:
List[GmailAttachment]
- class GmailThread[source]
Bases:
objectRepresents a Gmail conversation thread.
- Parameters:
thread_id (
str)history_id (
str)messages (
List[GmailMessage])
- __init__(thread_id, history_id, messages=<factory>)
- Parameters:
thread_id (
str)history_id (
str)messages (
List[GmailMessage])
- property latest_message: GmailMessage | None
Get the latest message in the thread.
-
messages:
List[GmailMessage]
- class GmailLabel[source]
Bases:
objectRepresents a Gmail label.
- Parameters:
- __init__(label_id, name, message_list_visibility=None, label_list_visibility=None, label_type=None, messages_total=None, messages_unread=None, threads_total=None, threads_unread=None)
- class GmailAttachment[source]
Bases:
objectRepresents a Gmail message attachment.
- __init__(attachment_id, filename, mime_type, size)
- class SheetData[source]
Bases:
objectRepresents data read from a Google Sheet.
- Parameters:
- __init__(spreadsheet_id, sheet_name, range_read, values=<factory>, headers=<factory>, metadata=<factory>)
- to_dict_list()[source]
Convert to list of dictionaries using headers as keys.
- Return type:
- Returns:
List of dictionaries, one per data row
- Raises:
ValueError – If no headers are defined
- class SheetRange[source]
Bases:
objectRepresents a range within a Google Sheet.
- Parameters:
- __init__(sheet_name, start_row, start_col, end_row=None, end_col=None)