Skip to content

Commit

Permalink
Paginate response when retrieving list of Google calendars.
Browse files Browse the repository at this point in the history
  • Loading branch information
phw198 committed Jan 16, 2021
1 parent 0ccc6a0 commit c6224c7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/OutlookGoogleCalendarSync/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ private void GetMyGoogleCalendars_Click(object sender, EventArgs e) {

log.Debug("Retrieving Google calendar list.");
this.bGetGoogleCalendars.Text = "Cancel retrieval";
List<GoogleCalendarListEntry> calendars = null;
List<GoogleCalendarListEntry> calendars = new List<GoogleCalendarListEntry>();
try {
calendars = GoogleOgcs.Calendar.Instance.GetCalendars();
} catch (AggregateException agex) {
Expand Down Expand Up @@ -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) {
Expand Down
73 changes: 40 additions & 33 deletions src/OutlookGoogleCalendarSync/GoogleOgcs/GoogleCalendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,44 +96,51 @@ public String SubscriptionInvite {

public List<GoogleCalendarListEntry> GetCalendars() {
CalendarList request = null;
String pageToken = null;
List<GoogleCalendarListEntry> result = new List<GoogleCalendarListEntry>();
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<GoogleCalendarListEntry> result = new List<GoogleCalendarListEntry>();
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<Event> GetCalendarEntriesInRecurrence(String recurringEventId) {
Expand Down

0 comments on commit c6224c7

Please sign in to comment.