Batch processing with Google Calendar V3 API

徘徊边缘 提交于 2019-12-23 04:37:26

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19019530/batch-processing-with-google-calendar-v3-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!