|
44 | 44 | build_event_set_create, |
45 | 45 | build_event_set_destroy, |
46 | 46 | build_event_set_update, |
| 47 | + parse_event_get, |
47 | 48 | parse_event_set, |
48 | 49 | ) |
| 50 | +from calendaring_jmap._methods.principal import build_get_availability, parse_get_availability |
49 | 51 | from calendaring_jmap._methods.task import ( |
50 | 52 | build_task_get, |
51 | 53 | build_task_list_get, |
|
54 | 56 | build_task_set_update, |
55 | 57 | parse_task_set, |
56 | 58 | ) |
57 | | -from calendaring_jmap.client import _DEFAULT_USING, _TASK_USING, _JMAPClientBase |
| 59 | +from calendaring_jmap.client import _DEFAULT_USING, _PRINCIPALS_USING, _TASK_USING, _JMAPClientBase |
58 | 60 | from calendaring_jmap.constants import ( |
59 | 61 | PARTICIPATION_STATUS_ACCEPTED, |
60 | 62 | PARTICIPATION_STATUS_DECLINED, |
61 | 63 | PARTICIPATION_STATUS_TENTATIVE, |
62 | 64 | ) |
63 | 65 | from calendaring_jmap.convert import ical_to_jscal |
64 | | -from calendaring_jmap.error import JMAPAuthError, JMAPMethodError |
| 66 | +from calendaring_jmap.error import _DEFAULT_ERROR_TYPE, JMAPAuthError, JMAPMethodError |
| 67 | +from calendaring_jmap.objects.busy_interval import BusyInterval |
65 | 68 | from calendaring_jmap.objects.calendar import JMAPCalendar |
66 | 69 | from calendaring_jmap.objects.calendar_object import JMAPCalendarObject |
67 | 70 | from calendaring_jmap.session import Session, async_fetch_session |
@@ -198,7 +201,7 @@ async def _request(self, method_calls: list[tuple], using: list[str] | None = No |
198 | 201 | for resp in method_responses: |
199 | 202 | method_name, resp_args, call_id = resp |
200 | 203 | if method_name == "error": |
201 | | - error_type = resp_args.get("type", "serverError") |
| 204 | + error_type = resp_args.get("type", _DEFAULT_ERROR_TYPE) |
202 | 205 | raise JMAPMethodError( |
203 | 206 | url=session.api_url, |
204 | 207 | reason=f"Method call failed: {resp_args}", |
@@ -664,6 +667,60 @@ async def search_events( |
664 | 667 | calendar_id=calendar_id, start=start, end=end, text=text, account_id=account_id |
665 | 668 | ) |
666 | 669 |
|
| 670 | + async def get_availability( |
| 671 | + self, |
| 672 | + account_ids: list[str], |
| 673 | + start: str, |
| 674 | + end: str, |
| 675 | + show_details: bool = False, |
| 676 | + ) -> dict[str, list[BusyInterval]]: |
| 677 | + """Return busy intervals for each of your own accounts over a time period. |
| 678 | +
|
| 679 | + See :meth:`JMAPClient.get_availability` for the full semantics. |
| 680 | + """ |
| 681 | + session = await self._get_session() |
| 682 | + result: dict[str, list[BusyInterval]] = {} |
| 683 | + for account_id in account_ids: |
| 684 | + if self._can_use_principal_availability(session, account_id): |
| 685 | + principal_id = self._current_user_principal_id(session) |
| 686 | + if principal_id is not None: |
| 687 | + try: |
| 688 | + result[account_id] = await self._get_availability_via_principal( |
| 689 | + principal_id, start, end, show_details |
| 690 | + ) |
| 691 | + continue |
| 692 | + except JMAPMethodError as e: |
| 693 | + if not self._should_fall_back_to_query(e.error_type): |
| 694 | + raise |
| 695 | + result[account_id] = await self._get_availability_via_fallback(account_id, start, end) |
| 696 | + return result |
| 697 | + |
| 698 | + async def _get_availability_via_principal( |
| 699 | + self, principal_id: str, start: str, end: str, show_details: bool |
| 700 | + ) -> list[BusyInterval]: |
| 701 | + session = await self._get_session() |
| 702 | + call = build_get_availability( |
| 703 | + principal_id, |
| 704 | + self._as_utc_datetime(start), |
| 705 | + self._as_utc_datetime(end), |
| 706 | + show_details=show_details, |
| 707 | + ) |
| 708 | + responses = await self._request([call], using=_PRINCIPALS_USING) |
| 709 | + for method_name, resp_args, _ in responses: |
| 710 | + if method_name == "Principal/getAvailability": |
| 711 | + return [BusyInterval.from_jmap(bp) for bp in parse_get_availability(resp_args)] |
| 712 | + raise JMAPMethodError(url=session.api_url, reason="No Principal/getAvailability response") |
| 713 | + |
| 714 | + async def _get_availability_via_fallback( |
| 715 | + self, account_id: str, start: str, end: str |
| 716 | + ) -> list[BusyInterval]: |
| 717 | + calls = self._build_availability_fallback_calls(account_id, start, end) |
| 718 | + responses = await self._request(calls) |
| 719 | + for method_name, resp_args, _ in responses: |
| 720 | + if method_name == "CalendarEvent/get": |
| 721 | + return self._busy_intervals_from_events(parse_event_get(resp_args)) |
| 722 | + return [] |
| 723 | + |
667 | 724 | async def get_sync_token(self) -> str: |
668 | 725 | """Return the current CalendarEvent state string for use as a sync token. |
669 | 726 |
|
|
0 commit comments