From c6224c7ea1cbb16913daf41cbade636263622acb Mon Sep 17 00:00:00 2001 From: Paul Woolcock <11843015+phw198@users.noreply.github.com> Date: Sat, 16 Jan 2021 20:27:18 +0000 Subject: [PATCH] Paginate response when retrieving list of Google calendars. #1151 --- .../Forms/MainForm.cs | 4 +- .../GoogleOgcs/GoogleCalendar.cs | 73 ++++++++++--------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/OutlookGoogleCalendarSync/Forms/MainForm.cs b/src/OutlookGoogleCalendarSync/Forms/MainForm.cs index bdc1b304..db991ba9 100644 --- a/src/OutlookGoogleCalendarSync/Forms/MainForm.cs +++ b/src/OutlookGoogleCalendarSync/Forms/MainForm.cs @@ -1074,7 +1074,7 @@ private void GetMyGoogleCalendars_Click(object sender, EventArgs e) { log.Debug("Retrieving Google calendar list."); this.bGetGoogleCalendars.Text = "Cancel retrieval"; - List calendars = null; + List calendars = new List(); try { calendars = GoogleOgcs.Calendar.Instance.GetCalendars(); } catch (AggregateException agex) { @@ -1105,7 +1105,7 @@ private void GetMyGoogleCalendars_Click(object sender, EventArgs e) { } } } - if (calendars != null) { + if (calendars.Count > 0) { cbGoogleCalendars.Items.Clear(); calendars.Sort((x, y) => (x.Sorted()).CompareTo(y.Sorted())); foreach (GoogleCalendarListEntry mcle in calendars) { diff --git a/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs b/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs index 86b15f29..8299c860 100644 --- a/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs +++ b/src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs @@ -96,44 +96,51 @@ public String SubscriptionInvite { public List GetCalendars() { CalendarList request = null; + String pageToken = null; + List result = new List(); int backoff = 0; - while (backoff < BackoffLimit) { - try { - request = Service.CalendarList.List().Execute(); - break; - } catch (Google.GoogleApiException ex) { - switch (HandleAPIlimits(ref ex, null)) { - case ApiException.throwException: throw; - case ApiException.freeAPIexhausted: - OGCSexception.LogAsFail(ref ex); - OGCSexception.Analyse(ex); - System.ApplicationException aex = new System.ApplicationException(SubscriptionInvite); - OGCSexception.LogAsFail(ref aex); - throw aex; - case ApiException.backoffThenRetry: - backoff++; - if (backoff == BackoffLimit) { - log.Error("API limit backoff was not successful. Retrieve calendar list failed."); - throw; - } else { - log.Warn("API rate limit reached. Backing off " + backoff + "sec before retry."); - System.Threading.Thread.Sleep(backoff * 1000); - } - break; + + do { + while (backoff < BackoffLimit) { + try { + CalendarListResource.ListRequest lr = Service.CalendarList.List(); + lr.PageToken = pageToken; + request = lr.Execute(); + break; + } catch (Google.GoogleApiException ex) { + switch (HandleAPIlimits(ref ex, null)) { + case ApiException.throwException: throw; + case ApiException.freeAPIexhausted: + OGCSexception.LogAsFail(ref ex); + OGCSexception.Analyse(ex); + System.ApplicationException aex = new System.ApplicationException(SubscriptionInvite); + OGCSexception.LogAsFail(ref aex); + throw aex; + case ApiException.backoffThenRetry: + backoff++; + if (backoff == BackoffLimit) { + log.Error("API limit backoff was not successful. Retrieve calendar list failed."); + throw; + } else { + log.Warn("API rate limit reached. Backing off " + backoff + "sec before retry."); + System.Threading.Thread.Sleep(backoff * 1000); + } + break; + } } } - } - if (request != null) { - List result = new List(); - foreach (CalendarListEntry cle in request.Items) { - result.Add(new GoogleCalendarListEntry(cle)); + if (request != null) { + pageToken = request.NextPageToken; + foreach (CalendarListEntry cle in request.Items) { + result.Add(new GoogleCalendarListEntry(cle)); + } + } else { + log.Error("Handshaking with the Google calendar service failed."); } - return result; - } else { - log.Error("Handshaking with the Google calendar service failed."); - } - return null; + } while (pageToken != null); + + return result; } public List GetCalendarEntriesInRecurrence(String recurringEventId) {