Google-api-dotnet-client: Sample code for .NET Gmail batch requests

Created on 20 Apr 2017  路  4Comments  路  Source: googleapis/google-api-dotnet-client

Is it possible to provide a code sample for using Gmail batch request API for .NET?

Documentation p2+

Most helpful comment

This is what I've managed to put together. Please have a look:

```C#
public IList BatchDownloadEmails(GmailService service, IEnumerable messageIds)
{
// Create a batch request.
var request = new BatchRequest(service);
var messages = new List();

        foreach (var messageId in messageIds)
        {
            var messageBodyRequest = service.Users.Messages.Get("me", messageId);
            messageBodyRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
            request.Queue<Message>(messageBodyRequest,
                (content, error, index, message) =>
                {
                    var msg = content;
                    messages.Add(msg);
                });
        }

        request.ExecuteAsync().Wait();
        return messages;
    }

```

All 4 comments

@LindaLawton is this something you already have a sample of?

Batching is really picky I was never able to come up with a decent way of generating batching example for all of the APIs.

  1. Recommend checking this its calendar but should help with Gmail
    Send Batch Requests
    if you cant get it to work let me know what methods you are sending and i will try and help out.
  2. When testing it DO NOT use your primary email address. If you spam the server google will block you.
  3. Remember batching is not going to help you with the 4xx user rate limit errors (5xx as well sometimes) they are still considered to be separate request. You are going to get user rate limit with batching as well as they are run on separate threads / instances. (To googles servers they look like separate requests, I really cant remember the term.)

This is what I've managed to put together. Please have a look:

```C#
public IList BatchDownloadEmails(GmailService service, IEnumerable messageIds)
{
// Create a batch request.
var request = new BatchRequest(service);
var messages = new List();

        foreach (var messageId in messageIds)
        {
            var messageBodyRequest = service.Users.Messages.Get("me", messageId);
            messageBodyRequest.Format = UsersResource.MessagesResource.GetRequest.FormatEnum.Metadata;
            request.Queue<Message>(messageBodyRequest,
                (content, error, index, message) =>
                {
                    var msg = content;
                    messages.Add(msg);
                });
        }

        request.ExecuteAsync().Wait();
        return messages;
    }

```

Closing as this library is in maintenance mode, so this is unlikely to be done.

Was this page helpful?
0 / 5 - 0 ratings