2
votes

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?

2

2 Answers

2
votes

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)
0
votes

Another way, using the new_batch_http_request method in the Calendar API:

event = {
        'summary': 'Google I/O 2015',
        'description': 'A chance to hear more about google.',
        'start': {
            'dateTime': '2021-05-28T09:00:00-07:00',
            'timeZone': 'America/Los_Angeles',
        },
        'end': {
            'dateTime': '2021-05-28T17:00:00-07:00',
            'timeZone': 'America/Los_Angeles',
        },
}

event2 = {
        'summary': 'Rocky Balboa',
        'description': 'more products.',
        'start': {
            'dateTime': '2021-05-22T09:00:00-07:00',
            'timeZone': 'America/Los_Angeles',
        },
        'end': {
            'dateTime': '2021-05-22T17:00:00-07:00',
            'timeZone': 'America/Los_Angeles',
        },

}

from googleapiclient.http import BatchHttpRequest
import httplib2
from googleapiclient import discovery



batch = service_calendar.new_batch_http_request()
batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event2))
batch.add(service_calendar.events().insert(calendarId=calendar_id, body=event))
batch.execute()