使用Google Calendar V3 API进行批处理

时间:2013-09-26 04:40:49

标签: google-calendar-api

我一直在使用新的Google Calendar V3 API,我编写了所有类方法来处理添加,更新,检索等,但我想知道是否有办法发送一批添加+更新+一次删除所有请求,而不是单独发送每个请求,并且可能超过trans / sec阈值。我知道.Batch方法已经在V3中折旧了,我找到了另一种使用Web服务的方法,该方法将通知客户端已经准备好了更改,但我正在尝试从.NET Winform应用程序执行此操作,因此需要启动它来自客户,不依赖于在线服务或PUSH方法。

此致

克里

1 个答案:

答案 0 :(得分:3)

我使用BatchRequest对象来实现这一点:

    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()

出于某种原因,如果没有指定Queue方法的回调,则不能有延迟请求,并且该方法需要泛型类型参数。所以我定义了以下内容:

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

有了这个,批量插入工作正常。