Add httplib2 15s timeout + cache_discovery=False to prevent hangs

This commit is contained in:
daniel
2026-05-08 10:53:19 -05:00
parent 25cba295b0
commit 6d0464ea07

View File

@@ -1,12 +1,16 @@
"""Direct Google Calendar and Gmail tools using google-api-python-client.""" """Direct Google Calendar and Gmail tools using google-api-python-client."""
import json import json
import boto3 import boto3
import httplib2
from strands import tool from strands import tool
from google.oauth2.credentials import Credentials from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request from google.auth.transport.requests import Request
from googleapiclient.discovery import build from googleapiclient.discovery import build
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
# Set a 15-second timeout on httplib2 to prevent hangs
_HTTP_TIMEOUT = 15
_sm = None _sm = None
@@ -55,7 +59,8 @@ def list_calendars() -> str:
"""List all Google Calendars for the current user.""" """List all Google Calendars for the current user."""
try: try:
creds = _get_creds(_actor_id()) creds = _get_creds(_actor_id())
svc = build('calendar', 'v3', credentials=creds) http = creds.authorize(httplib2.Http(timeout=_HTTP_TIMEOUT))
svc = build('calendar', 'v3', http=http, credentials=creds, cache_discovery=False)
result = svc.calendarList().list().execute() result = svc.calendarList().list().execute()
items = result.get('items', []) items = result.get('items', [])
if not items: if not items:
@@ -75,7 +80,8 @@ def get_calendar_events(calendar_id: str = 'primary', days_ahead: int = 7) -> st
""" """
try: try:
creds = _get_creds(_actor_id()) creds = _get_creds(_actor_id())
svc = build('calendar', 'v3', credentials=creds) http = creds.authorize(httplib2.Http(timeout=_HTTP_TIMEOUT))
svc = build('calendar', 'v3', http=http, credentials=creds, cache_discovery=False)
now = datetime.now(timezone.utc) now = datetime.now(timezone.utc)
time_max = now + timedelta(days=days_ahead) time_max = now + timedelta(days=days_ahead)
result = svc.events().list( result = svc.events().list(
@@ -108,7 +114,8 @@ def list_gmail_messages(max_results: int = 10, query: str = 'in:inbox') -> str:
""" """
try: try:
creds = _get_creds(_actor_id()) creds = _get_creds(_actor_id())
svc = build('gmail', 'v1', credentials=creds) http = creds.authorize(httplib2.Http(timeout=_HTTP_TIMEOUT))
svc = build('gmail', 'v1', http=http, credentials=creds, cache_discovery=False)
result = svc.users().messages().list( result = svc.users().messages().list(
userId='me', q=query, maxResults=max_results userId='me', q=query, maxResults=max_results
).execute() ).execute()
@@ -139,7 +146,8 @@ def get_gmail_message(message_id: str) -> str:
""" """
try: try:
creds = _get_creds(_actor_id()) creds = _get_creds(_actor_id())
svc = build('gmail', 'v1', credentials=creds) http = creds.authorize(httplib2.Http(timeout=_HTTP_TIMEOUT))
svc = build('gmail', 'v1', http=http, credentials=creds, cache_discovery=False)
msg = svc.users().messages().get( msg = svc.users().messages().get(
userId='me', id=message_id, format='full' userId='me', id=message_id, format='full'
).execute() ).execute()