Skip to content

Commit

Permalink
First part
Browse files Browse the repository at this point in the history
  • Loading branch information
squeaky-pl committed Feb 27, 2024
1 parent e84a084 commit 1f2b2c4
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 196 deletions.
19 changes: 9 additions & 10 deletions inbox/events/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import random
import urllib.parse
import uuid
from typing import Any, Dict, List, Optional
from typing import Any, Dict, Iterable, List, Optional

import arrow
import attrs
Expand Down Expand Up @@ -104,13 +104,13 @@ def sync_events(

return updates

def _get_raw_calendars(self) -> List[Dict[str, Any]]:
def _get_raw_calendars(self) -> Iterable[Dict[str, Any]]:
"""Gets raw data for the user's calendars."""
return self._get_resource_list(CALENDARS_URL)

def _get_raw_events(
self, calendar_uid: str, sync_from_time: Optional[datetime.datetime] = None
) -> List[Dict[str, Any]]:
) -> Iterable[Dict[str, Any]]:
"""Gets raw event data for the given calendar.
Parameters
Expand All @@ -123,7 +123,7 @@ def _get_raw_events(
Returns
-------
list of dictionaries representing JSON.
generator of dictionaries representing JSON.
"""
if sync_from_time is not None:
# Note explicit offset is required by Google calendar API.
Expand All @@ -135,7 +135,7 @@ def _get_raw_events(
urllib.parse.quote(calendar_uid)
)
try:
return self._get_resource_list(
yield from self._get_resource_list(
url, updatedMin=sync_from_time_str, eventTypes="default"
)
except requests.exceptions.HTTPError as exc:
Expand All @@ -144,14 +144,13 @@ def _get_raw_events(
# The calendar API may return 410 if you pass a value for
# updatedMin that's too far in the past. In that case, refetch
# all events.
return self._get_resource_list(url)
yield from self._get_resource_list(url)
else:
raise

def _get_resource_list(self, url: str, **params) -> List[Dict[str, Any]]:
def _get_resource_list(self, url: str, **params) -> Iterable[Dict[str, Any]]:
"""Handles response pagination."""
token = self._get_access_token()
items = []
next_page_token: Optional[str] = None
params["showDeleted"] = True
while True:
Expand All @@ -161,10 +160,10 @@ def _get_resource_list(self, url: str, **params) -> List[Dict[str, Any]]:
r = requests.get(url, params=params, auth=OAuthRequestsWrapper(token))
r.raise_for_status()
data = r.json()
items += data["items"]
yield from data["items"]
next_page_token = data.get("nextPageToken")
if next_page_token is None:
return items
return

except requests.exceptions.SSLError:
self.log.warning(
Expand Down
Loading

0 comments on commit 1f2b2c4

Please sign in to comment.