如何使用 Google API Java 客户端对联系人进行部分更新?

发布于 2024-10-11 20:31:06 字数 918 浏览 5 评论 0原文

我正在使用轻量级 Google API Java 客户端来修改联系人。

API 有一些示例展示了如何进行部分更新(即通过 HTTP PATCH),但 Google Contacts 似乎不支持 PATCH 。该文档还指定通过 PUT 完成的联系人更新需要包含有关联系人的完整信息,因为 PUT 请求中省略的任何现有数据都将从联系人中删除。

那么读取联系人、修改其数据并更新它的最简单的过程是什么?客户端 API 有一个很好的 AtomParser,它从 HTTP GET 请求获取响应并填充我的类中的成员,但除非我的类包含联系人可能拥有的所有可能的字段,否则我可能无法全部读取数据并因此在 HTTP PUT 期间删除数据。

我唯一的猜测是,我必须跳过所有很酷的 AtomParser 魔法,只使用普通的旧 XML 解析器来读取传入的提要,提取我需要的信息,然后对其执行 HTTP PUT。但考虑到 API 客户端的所有功能,这似乎有点荒谬。有更好的办法吗?

我暗自希望有一个与此等效的东西:

HttpRequest request = transport.buildPatchRequest();
request.setUrl(getEditLink());
AtomPatchRelativeToOriginalContent content =
    new AtomPatchRelativeToOriginalContent();

content.namespaceDictionary = Namespace.DICTIONARY;
content.originalEntry = originalEntry;
content.patchedEntry = modifiedEntry;
request.content = content;

return RedirectHandler.execute(request).parseAs(getClass());

I'm using the lightweight Google API Java client to modify contacts.

There are a few samples for the API showing how to do partial updates (i.e. via HTTP PATCH), but Google Contacts specifically doesn't seem to support PATCH . The docs also specify that contacts updates done via PUT need to include the full information about the contact since any existing data omitted in the PUT request will be removed from the contact.

So what's the easiest procedure to read a contact, modify its data and update it? The client API has this nice AtomParser that takes the response from the HTTP GET request and populates members in my class, but unless my class contains every possible field that a contact could have, I might not read in all the data and as such delete data during the HTTP PUT.

My only guess is that I'll have to skip all the cool AtomParser witchcraft and just use a plain old XML parser to read the incoming feed, extract the information I need and then do an HTTP PUT on it. But that seems a bit absurd, given all the functionality int he API client. Is there a better way?

I'm secretly hoping there is an equivalent to this:

HttpRequest request = transport.buildPatchRequest();
request.setUrl(getEditLink());
AtomPatchRelativeToOriginalContent content =
    new AtomPatchRelativeToOriginalContent();

content.namespaceDictionary = Namespace.DICTIONARY;
content.originalEntry = originalEntry;
content.patchedEntry = modifiedEntry;
request.content = content;

return RedirectHandler.execute(request).parseAs(getClass());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2024-10-18 20:31:06

这正是 GenericXml 的设计目的是:它存储您在应用程序中未使用的任意 XML,以便您可以安全地使用 PUT 方法。只需在您的数据模型类中扩展它即可。可以在 Content API 示例中找到 GenericXml 的一个很好的示例购物。他们还提供了XML 模型的详细指南,其中包括讨论GenericXml 的。如果它不适合您,请告诉我。

全面披露:我是 google-api-java-client项目。

That's exactly what GenericXml was designed for: it stores arbitrary XML that you are not using in your application so you can safely use the PUT method. Simply extend it in your data model classes. A good example of GenericXml can be found in the sample for the Content API for Shopping. They also have a detailed guide of the XML model including a discussion of GenericXml. Let me know if it doesn't work for you.

Full disclosure: I am an owner of the google-api-java-client project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文