Skip to content

Commit 2c561eb

Browse files
feat: Add optional creds argument to GoogleCalendarToolSpec (#19826)
1 parent 8bba0ef commit 2c561eb

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

llama-index-integrations/tools/llama-index-tools-google/llama_index/tools/google/calendar/base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ class GoogleCalendarToolSpec(BaseToolSpec):
3535

3636
spec_functions = ["load_data", "create_event", "get_date"]
3737

38+
def __init__(self, creds: Optional[Any] = None):
39+
"""
40+
Initialize the GoogleCalendarToolSpec.
41+
42+
Args:
43+
creds (Optional[Any]): Pre-configured credentials to use for authentication.
44+
If provided, these will be used instead of the OAuth flow.
45+
46+
"""
47+
self.creds = creds
48+
3849
def load_data(
3950
self,
4051
number_of_results: Optional[int] = 100,
@@ -119,6 +130,9 @@ def _get_credentials(self) -> Any:
119130
Credentials, the obtained credential.
120131
121132
"""
133+
if self.creds is not None:
134+
return self.creds
135+
122136
from google_auth_oauthlib.flow import InstalledAppFlow
123137

124138
from google.auth.transport.requests import Request

llama-index-integrations/tools/llama-index-tools-google/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ dev = [
2626

2727
[project]
2828
name = "llama-index-tools-google"
29-
version = "0.6.1"
29+
version = "0.6.2"
3030
description = "llama-index tools google integration"
3131
authors = [{name = "Your Name", email = "[email protected]"}]
3232
requires-python = ">=3.9,<4.0"

llama-index-integrations/tools/llama-index-tools-google/tests/test_tools_google.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest.mock import Mock, patch
12
from llama_index.core.tools.tool_spec.base import BaseToolSpec
23
from llama_index.tools.google import (
34
GmailToolSpec,
@@ -15,3 +16,45 @@ def test_class():
1516

1617
names_of_base_classes = [b.__name__ for b in GoogleSearchToolSpec.__mro__]
1718
assert BaseToolSpec.__name__ in names_of_base_classes
19+
20+
21+
def test_google_calendar_tool_spec_init_without_creds():
22+
"""Test GoogleCalendarToolSpec initialization without credentials."""
23+
tool = GoogleCalendarToolSpec()
24+
assert tool.creds is None
25+
26+
27+
def test_google_calendar_tool_spec_init_with_creds():
28+
"""Test GoogleCalendarToolSpec initialization with credentials."""
29+
mock_creds = Mock()
30+
tool = GoogleCalendarToolSpec(creds=mock_creds)
31+
assert tool.creds is mock_creds
32+
33+
34+
def test_google_calendar_tool_spec_get_credentials_with_provided_creds():
35+
"""Test _get_credentials returns provided credentials when available."""
36+
mock_creds = Mock()
37+
tool = GoogleCalendarToolSpec(creds=mock_creds)
38+
39+
credentials = tool._get_credentials()
40+
assert credentials is mock_creds
41+
42+
43+
@patch("os.path.exists")
44+
@patch("google.oauth2.credentials.Credentials.from_authorized_user_file")
45+
def test_google_calendar_tool_spec_get_credentials_oauth_flow(
46+
mock_from_file, mock_exists
47+
):
48+
"""Test _get_credentials falls back to OAuth flow when no creds provided."""
49+
mock_exists.return_value = True
50+
mock_creds = Mock()
51+
mock_creds.valid = True
52+
mock_from_file.return_value = mock_creds
53+
54+
tool = GoogleCalendarToolSpec() # No creds provided
55+
56+
credentials = tool._get_credentials()
57+
assert credentials is mock_creds
58+
mock_from_file.assert_called_once_with(
59+
"token.json", ["https://www.googleapis.com/auth/calendar"]
60+
)

0 commit comments

Comments
 (0)