Zig
Zig
Google People API - Update an Existing Contact
See more Google People Examples
To update an existing contact, you must include the person.metadata.sources.etag field in the person for the contact to be updated to make sure the contact has not changed since your last read.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// It is assumed we previously obtained an OAuth2 access token.
// This example loads the JSON access token file
// originally obtained by this example: Get Google People API OAuth2 Access Token
// or refreshed by this example: Refresh Google People API OAuth2 Access Token
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
json_token.loadFile("qa_data/tokens/googlePeople.json") catch {
std.debug.print("Failed to load googleContacts.json\n", .{});
return;
};
const http = try chilkat.Http.init();
defer http.deinit();
http.setAuthToken(try json_token.stringOf(alloc, "access_token"));
// Implements the following CURL command:
// curl -X PATCH https://people.googleapis.com/v1/resource_name:updateContact?updatePersonFields=emailAddresses \
// -H "Content-Type: application/json" \
// -d '{
// "resourceName": "resource_name",
// "etag": "#eyZ0Gu5HVas=",
// "emailAddresses": [{ "value": "john.doe@gmail.com" }],
// }'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "resourceName": "resource_name",
// "etag": "#eyZ0Gu5HVas=",
// "emailAddresses": [
// {
// "value": "john.doe@gmail.com"
// }
// ]
// }
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.updateString("resourceName", "people/c172365763025317520") catch {};
json.updateString("etag", "%EigBAj0DBAUGBwgJPgoLPwwNDg8QQBESExQVFhc1GTQ3HyEiIyQlJicuGgQBAgUHIgxleVowR3U1SFZhcz0=") catch {};
json.updateString("emailAddresses[0].value", "john.doe@gmail.com") catch {};
const sb_request_body = try chilkat.StringBuilder.init();
defer sb_request_body.deinit();
json.emitSb(sb_request_body) catch {};
// Notice that the resource name is part of the URL...
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpSb("PATCH", "https://people.googleapis.com/v1/people/c172365763025317520:updateContact?updatePersonFields=emailAddresses", sb_request_body, "utf-8", "application/json", resp) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
resp.getBodySb(sb_response_body) catch {};
const j_resp = try chilkat.JsonObject.init();
defer j_resp.deinit();
j_resp.loadSb(sb_response_body) catch {};
j_resp.setEmitCompact(false);
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
const resp_status_code = resp.getStatusCode();
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
if (resp_status_code >= 400) {
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try resp.getHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "resourceName": "people/c172365763025317520",
// "etag": "%EigBAj0DBAUGBwgJPgoLPwwNDg8QQBESExQVFhc1GTQ3HyEiIyQlJicuGgQBAgUHIgxtQnhyR2MzMGU4Yz0=",
// "metadata": {
// "sources": [
// {
// "type": "CONTACT",
// "id": "2645dbf8c902e90",
// "etag": "#mBxrGc30e8c=",
// "updateTime": "2020-08-03T23:01:23.539Z"
// }
// ],
// "objectType": "PERSON"
// },
// "names": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "2645dbf8c902e90"
// }
// },
// "displayName": "John Doe",
// "familyName": "Doe",
// "givenName": "John",
// "displayNameLastFirst": "Doe, John",
// "unstructuredName": "John Doe"
// }
// ],
// "photos": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "2645dbf8c902e90"
// }
// },
// "url": "https://lh3.googleusercontent.com/-XdUIqeMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/V8BNOaftJmYG2hHugNzYued7G9QFdeZOACLcDEAEiGQoBShD___________8BGNvwiP7______wE/s100/photo.jpg",
// "default": true
// }
// ],
// "emailAddresses": [
// {
// "metadata": {
// "primary": true,
// "source": {
// "type": "CONTACT",
// "id": "2645def8c902e90"
// }
// },
// "value": "john.doe@gmail.com"
// }
// ],
// "memberships": [
// {
// "metadata": {
// "source": {
// "type": "CONTACT",
// "id": "2645dbf8c902e90"
// }
// },
// "contactGroupMembership": {
// "contactGroupId": "myContacts",
// "contactGroupResourceName": "contactGroups/myContacts"
// }
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
var v_type: [:0]const u8 = "";
var id: [:0]const u8 = "";
var update_time: [:0]const u8 = "";
var metadata_primary: bool = false;
var metadata_source_type: [:0]const u8 = "";
var metadata_source_id: [:0]const u8 = "";
var display_name: [:0]const u8 = "";
var family_name: [:0]const u8 = "";
var given_name: [:0]const u8 = "";
var display_name_last_first: [:0]const u8 = "";
var unstructured_name: [:0]const u8 = "";
var url: [:0]const u8 = "";
var default: bool = false;
var value: [:0]const u8 = "";
var contact_group_membership_contact_group_id: [:0]const u8 = "";
var contact_group_membership_contact_group_resource_name: [:0]const u8 = "";
_ = try j_resp.stringOf(alloc, "resourceName");
var etag: [:0]const u8 = try j_resp.stringOf(alloc, "etag");
_ = try j_resp.stringOf(alloc, "metadata.objectType");
var i: i32 = 0;
var count_i: i32 = j_resp.sizeOfArray("metadata.sources");
while (i < count_i) {
j_resp.setI(i);
v_type = try j_resp.stringOf(alloc, "metadata.sources[i].type");
id = try j_resp.stringOf(alloc, "metadata.sources[i].id");
etag = try j_resp.stringOf(alloc, "metadata.sources[i].etag");
update_time = try j_resp.stringOf(alloc, "metadata.sources[i].updateTime");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("names");
while (i < count_i) {
j_resp.setI(i);
metadata_primary = j_resp.boolOf("names[i].metadata.primary");
metadata_source_type = try j_resp.stringOf(alloc, "names[i].metadata.source.type");
metadata_source_id = try j_resp.stringOf(alloc, "names[i].metadata.source.id");
display_name = try j_resp.stringOf(alloc, "names[i].displayName");
family_name = try j_resp.stringOf(alloc, "names[i].familyName");
given_name = try j_resp.stringOf(alloc, "names[i].givenName");
display_name_last_first = try j_resp.stringOf(alloc, "names[i].displayNameLastFirst");
unstructured_name = try j_resp.stringOf(alloc, "names[i].unstructuredName");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("photos");
while (i < count_i) {
j_resp.setI(i);
metadata_primary = j_resp.boolOf("photos[i].metadata.primary");
metadata_source_type = try j_resp.stringOf(alloc, "photos[i].metadata.source.type");
metadata_source_id = try j_resp.stringOf(alloc, "photos[i].metadata.source.id");
url = try j_resp.stringOf(alloc, "photos[i].url");
default = j_resp.boolOf("photos[i].default");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("emailAddresses");
while (i < count_i) {
j_resp.setI(i);
metadata_primary = j_resp.boolOf("emailAddresses[i].metadata.primary");
metadata_source_type = try j_resp.stringOf(alloc, "emailAddresses[i].metadata.source.type");
metadata_source_id = try j_resp.stringOf(alloc, "emailAddresses[i].metadata.source.id");
value = try j_resp.stringOf(alloc, "emailAddresses[i].value");
i = i + 1;
}
i = 0;
count_i = j_resp.sizeOfArray("memberships");
while (i < count_i) {
j_resp.setI(i);
metadata_source_type = try j_resp.stringOf(alloc, "memberships[i].metadata.source.type");
metadata_source_id = try j_resp.stringOf(alloc, "memberships[i].metadata.source.id");
contact_group_membership_contact_group_id = try j_resp.stringOf(alloc, "memberships[i].contactGroupMembership.contactGroupId");
contact_group_membership_contact_group_resource_name = try j_resp.stringOf(alloc, "memberships[i].contactGroupMembership.contactGroupResourceName");
i = i + 1;
}
}