Skip to content

Commit

Permalink
Add num_retries to api calls
Browse files Browse the repository at this point in the history
With larger periods, the script would regularly hit the quota limit of
the free tier and abort with an HttpError exception and status code 403.

It seems this can be mitigated by setting a `num_retries`, which
includes an exponential backoff mechanism.

From the [google-api-python-client docs](https://github.com/googleapis/google-api-python-client/blob/cbb1f88b82b21f5cb9dcace33ffea3f95a189015/googleapiclient/http.py#L882):

> num_retries: Integer, number of times to retry with randomized
> exponential backoff. If all retries fail, the raised HttpError
> represents the last request. If zero (default), we attempt the
> request only once.
  • Loading branch information
kaeff committed Aug 31, 2021
1 parent debbd0e commit c667ee7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

COPIED_EVENT_LEADING_TEXT = '[CLIENT MEETING]'

NUM_RETRIES = 10

class Calendar:
def __init__(self, calendarId, googleService):
Expand All @@ -23,7 +24,7 @@ def copyAllEventsFrom(self, otherCalendar, period: DatePeriod, copySensibleData)
def getEvents(self, period: DatePeriod):
events_result = self.googleService.events().list(calendarId=self.calendarId, timeMin=period.start,
timeMax=period.end, singleEvents=True,
orderBy='startTime').execute()
orderBy='startTime').execute(num_retries=NUM_RETRIES)
events = events_result.get('items', [])
return events

Expand All @@ -34,7 +35,7 @@ def removeCopiedEvents(self, period: DatePeriod):
self.deleteEvent(event.get("id"))

def deleteEvent(self, eventId):
self.googleService.events().delete(calendarId=self.calendarId, eventId=eventId).execute()
self.googleService.events().delete(calendarId=self.calendarId, eventId=eventId).execute(num_retries=NUM_RETRIES)

def createEventFrom(self, sourceEvent, copySensibleData):
eventBody = {
Expand All @@ -50,7 +51,7 @@ def createEventFrom(self, sourceEvent, copySensibleData):
'reminders': sourceEvent.get('reminders'),
}

createdEvent = self.googleService.events().insert(calendarId=self.calendarId, body=eventBody).execute()
createdEvent = self.googleService.events().insert(calendarId=self.calendarId, body=eventBody).execute(num_retries=NUM_RETRIES)

return createdEvent

Expand Down

0 comments on commit c667ee7

Please sign in to comment.