K Cassanova
K Cassanova

Reputation: 1

Batch processing with Google Calendar V3 API

I've been working with the new google Calendar V3 API and I've coded all my class methods to process Adds, Updates, retrievals etc but I was wondering if there is a way to send a batch of adds + updates + deletes all at once rather than sending each request separately and possible exceeding the trans/sec threshold. I understand the .Batch method has been depreciated in V3 and I found another methodology that uses web services that will notify a client that changes are ready but I'm trying to do this from a .NET Winform application so it needs to be something initiated from the client and not dependent upon online services or a PUSH methodology.

Regards,

Kerry

Upvotes: 0

Views: 2205

Answers (1)

Joshua Frank
Joshua Frank

Reputation: 13848

I got this to work using the BatchRequest object:

    Dim initializer As New BaseClientService.Initializer()
    initializer.HttpClientInitializer = credential
    initializer.ApplicationName = "My App"

    Dim service = New CalendarService(initializer)

    'fetch the calendars
    Dim list = service.CalendarList.List().Execute().Items()
    'get the calendar you want to work with
    Dim calendar = list.First(Function(x) x.Summary = "{Calendar Name}")

    Dim br As New Google.Apis.Requests.BatchRequest(service)

    'make 5 events
    For i = 1 To 5
        'create a new event
        Dim e As New [Event]

        'set the event properties
        e.Summary = "Test Event"
        e.Description = "Test Description"
        e.Location = "Test Location"
        ...
        'make a request to insert the event
        Dim ins As New InsertRequest(service, e, calendar.Id)
        'queue the request
        br.Queue(Of Dummy)(ins, AddressOf OnResponse)
    Next

    'execute the batch request
    Dim t = br.ExecuteAsync()
    'wait for completion
    t.Wait()

For some reason, you can't have a deferred request without specifying a callback to the Queue method, and that method requires a generic type parameter. So I defined the following:

Class Dummy
End Class

Sub OnResponse(content As Dummy, err As Google.Apis.Requests.RequestError, index As Integer, message As System.Net.Http.HttpResponseMessage)
End Sub

With this in place, the batch inserts worked fine.

Upvotes: 3

Related Questions