59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""Google Calendar API client for VoIdea.
|
|
|
|
Uses OAuth access token to create events via Google Calendar API v3.
|
|
"""
|
|
|
|
import logging
|
|
|
|
import httpx
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
GOOGLE_CALENDAR_URL = "https://www.googleapis.com/calendar/v3"
|
|
|
|
|
|
class GoogleCalendarClient:
|
|
def __init__(self, access_token: str):
|
|
self._access_token = access_token
|
|
|
|
async def create_event(
|
|
self,
|
|
summary: str,
|
|
description: str,
|
|
start_iso: str,
|
|
end_iso: str,
|
|
**kwargs,
|
|
) -> bool:
|
|
async with httpx.AsyncClient() as client:
|
|
resp = await client.post(
|
|
f"{GOOGLE_CALENDAR_URL}/calendars/primary/events",
|
|
headers={
|
|
"Authorization": f"Bearer {self._access_token}",
|
|
"Content-Type": "application/json",
|
|
},
|
|
json={
|
|
"summary": summary,
|
|
"description": description,
|
|
"start": {"dateTime": start_iso, "timeZone": "UTC"},
|
|
"end": {"dateTime": end_iso, "timeZone": "UTC"},
|
|
},
|
|
)
|
|
if resp.status_code == 200:
|
|
return True
|
|
logger.error("Google Calendar create_event failed: %s %s", resp.status_code, resp.text)
|
|
return False
|
|
|
|
async def list_events(self, max_results: int = 10) -> list[dict]:
|
|
async with httpx.AsyncClient() as client:
|
|
resp = await client.get(
|
|
f"{GOOGLE_CALENDAR_URL}/calendars/primary/events",
|
|
headers={"Authorization": f"Bearer {self._access_token}"},
|
|
params={"maxResults": max_results, "orderBy": "startTime", "singleEvents": "true"},
|
|
)
|
|
if resp.status_code == 200:
|
|
return resp.json().get("items", [])
|
|
return []
|
|
|
|
async def health_check(self) -> bool:
|
|
return bool(self._access_token)
|