Python Google Contacts API - 删除联系人

时间:2016-06-16 13:48:08

标签: python gdata google-contacts

阅读开发人员指南我找到了如何删除单个联系人:

def delete_contact(gd_client, contact_url):
  # Retrieving the contact is required in order to get the Etag.
  contact = gd_client.GetContact(contact_url)

  try:
    gd_client.Delete(contact)
  except gdata.client.RequestError, e:
    if e.status == 412:
      # Etags mismatch: handle the exception.
      pass

有没有办法删除所有联系人? 找不到办法。

迭代每个联系人需要几分钟才能完成大批量的

1 个答案:

答案 0 :(得分:0)

如果您要执行大量操作,请使用批处理请求。您可以让服务器使用单个HTTP请求执行多个操作。批量请求一次限制为100个操作。您可以在Google Data APIs Batch Processing documentation中找到有关批处理操作的更多信息。

delete all contacts使用contactsrequest.Batch操作。对于此操作,请创建LIST<type>,为每个联系人项目设置BatchData,然后将列表传递给contactsrequest.Batch操作。

private void DeleteAllContacts()
{
RequestSettings rs = new RequestSettings(this.ApplicationName, this.userName, this.passWord);
rs.AutoPaging = true // this will result in automatic paging for listing and deleting all contacts
ContactsRequest cr = new ContactsRequest(rs);
Feed<Contact> f = cr.GetContacts();
List<Contact> list = new List<Contact>();
int i=0;
foreach (Contact c in f.Entries)
{
c.BatchData = new GDataBatchEntryData();
c..BatchData.Id = i.ToString();
c.BatchData.Type = GDataBatchOperationType.delete;
i++;
list.Add(c);
}
cr.Batch(list, new Uri(f.AtomFeed.Batch), GDataBatchOperationType.insert);
f = cr.GetContacts();
Assert.IsTrue(f.TotalResults == 0, "Feed should be empty now");
}