How to perform batch insertion for list of events using Google Calendar API in Dart?

192

If you check the Google calendar api documentation for events.insert you will notice that it states.

Creates an event.

There is however the option to use the batching endpoint You should be aware that the only thing batching is going to save you is the HTTP Calls back and forth. Each call within the batch still counts against your quota it does not count as a single request against your quota. You're limited to 50 calls in a single batch request.

Question: As per the official Google Blog, there's no way to perform a batch operation in dart.

If you check the batching guide it shows how to build up a batching request using HTTP. I would be surprised to find that you could not do this with flutter as well as flutter is capable of a making a HTTP request. I was also unable to find anyone that had gotten it to work. This may just mean that no one has bothered to try or that they just haven't posted the results.

Actually the Batching blog post states that the Dart Client library does not support batching

The Google API Dart Client Library does not support these features.

That does by no means that its not possible at all it just means that your going to have to code it from scratch yourself.

is it worth it.

Again as there is little or no advantage to using batching aside from the HTTP request send limit. You will need to decide if its worth your time trying to get it to work. IMO its not, i have been using these apis for more then 11 years and i have never found batching to be very stable or worth the aggravation it causes.

With batching you tend to get more quota flooding errors as the server things you are sending to many requests at once even sending 5 at once has resulted in flooding errors in my experience and then you have to go back and check which ones made it though to send them again.

Share:
192
Zujaj Misbah Khan
Author by

Zujaj Misbah Khan

Name : Zujaj Misbah Khan Main Skills: Dart, Flutter, C# Extra Skills: Video Editing,Photoshop Country: Pakistan

Updated on December 31, 2022

Comments

  • Zujaj Misbah Khan
    Zujaj Misbah Khan over 1 year

    Problem Statement

    As a developer, my requirements are to insert a list of events in the Google Calendar with a single call.

    Why Batch?

    As mentioned here, it helps to reduce the network overheads and increases performance.

    Current Scenario

    Using the googleapis package, I can only perform a single insert operation as shown in the below snippet :

    var eventResponse = await _calendarApi.events
              .insert(event, calendarId, sendUpdates: 'all');
    

    From a development perspective, it's not an efficient approach to call this method multiple times. Also, it would be a bad idea to create an array of the insert method wrapped in Future and use Future.wait() to wait for all the insertion calls to be executed, see below snippet.

     Future<List<Event> insertEvents(List<Event> events) async {
        
        var _calendarApi = CalendarApi(_authClient);
    
        List<Future<Event>> _futureEvents = [];
    
        for (int i = 0; i < events.length; i++) {
          _futureEvents.add(_calendarApi.events.insert(events[i], calendarId));
        }
    
        var _eventResponse =
            await Future.wait(_futureEvents).catchError((e) => print(e));
        
        return _eventResponse;
      }
    

    As per the official Google Blog, there's no way to perform a batch operation in dart.

    Does anyone know a better optimistic solution for this problem?

  • Zujaj Misbah Khan
    Zujaj Misbah Khan over 2 years
    @DalmTo As per your experience, your advice is not to use batch? Can you please provide some code to carry out the insert operation through a better or optimistic approach?
  • DaImTo
    DaImTo over 2 years
    Sorry i cant help with flutter, I haven't touched dart since it first came out. The only thing i would consider with your code though is to check the response from the insert to make sure it doesn't fail. You could potentially get a quota error if you do you would want to resend the request. Also check out the fields parameter on the insert. You can set it so that it only returns the id and none of the rest of the response fields will be populated this may get you a slight performance increase.