Search code examples
pythongoogle-apigoogle-calendar-apigoogle-api-python-clientbatching

Google Calendar API v3 batch update in python


I'm having trouble using the new Google Calendar API v3 python library. The documentation seems a bit sparse. I can authenticate and get events on a specific calendar. However, I would like to perform batch update as was possible with the gdata library:

# example from gdata
# feed that holds all the batch rquest entries
  request_feed = gdata.calendar.data.CalendarEventFeed()
# add the update entries to the batch feed
  request_feed.AddUpdate(entry=updateEntry1)
  request_feed.AddUpdate(entry=updateEntry2)
# submit the batch request to the server
  response_feed = self.cal_client.ExecuteBatch(request_feed, gdata.calendar.client.DEFAULT_BATCH_URL)

There is an example here https://developers.google.com/google-apps/calendar/batch#example in html. But can I do it using the python library?


Solution

  • There's generic Google API Python library batch instructions here. Try something like:

    from apiclient.http import BatchHttpRequest
    
    def insert_event(request_id, response, exception):
      if exception is not None:
        # Do something with the exception
         pass
      else:
        # Do something with the response
        pass
    
    service = build('calendar', 'v3')
    
    batch = BatchHttpRequest(callback=insert_event)
    
    batch.add(service.events().quickAdd(calendarId="[email protected]",
      text="Lunch with Jim on Friday"))
    batch.add(service.events().quickAdd(calendarId="[email protected]",
      text="Dinner with Amy on Saturday"))
    batch.add(service.events().quickAdd(calendarId="[email protected]",
      text="Breakfast with John on Sunday"))
    batch.execute(http=http)