Question

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());
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top