Files

59 lines
1.8 KiB
Python

"""Yandex Calendar API client for VoIdea.
Uses OAuth access token to create events via Yandex Calendar API.
"""
import logging
import httpx
logger = logging.getLogger(__name__)
YANDEX_CALENDAR_URL = "https://calendar.yandex.ru/api"
class YandexCalendarClient:
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"{YANDEX_CALENDAR_URL}/events",
headers={
"Authorization": f"OAuth {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 in (200, 201):
return True
logger.error("Yandex 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"{YANDEX_CALENDAR_URL}/events",
headers={"Authorization": f"OAuth {self._access_token}"},
params={"maxResults": max_results},
)
if resp.status_code == 200:
return resp.json().get("events", [])
return []
async def health_check(self) -> bool:
return bool(self._access_token)