Sample code for 30+ languages & platforms
Zig

Google People API - Create Contact

See more Google People Examples

Create a new contact.

Chilkat Zig Downloads

Zig
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 -H "Content-Type: application/json" -X POST
    //  -d '{
    //  "names": [{ "givenName": "John", "familyName": "Doe" }]
    //  }'
    //  https://people.googleapis.com/v1/people:createContact

    // 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.

    // {
    //   "names": [
    //     {
    //       "givenName": "John",
    //       "familyName": "Doe"
    //     }
    //   ]
    // }

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("names[0].givenName", "John") catch {};
    json.updateString("names[0].familyName", "Doe") catch {};

    const resp = try chilkat.HttpResponse.init();
    defer resp.deinit();
    http.httpJson("POST", "https://people.googleapis.com/v1/people:createContact", json, "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": "%EigBAj0DBAUGBwgJPgoLPwwNDg8QQBESExQVFhc1GTQ3HyEiIyQlJicuGgQBAgUHIgxleVowR3U1SFZhcz0=",
    //   "metadata": {
    //     "sources": [
    //       {
    //         "type": "CONTACT",
    //         "id": "2645dbf8c902e90",
    //         "etag": "#eyZ0Gu5HVas=",
    //         "updateTime": "2020-08-03T22:46:03.410Z"
    //       }
    //     ],
    //     "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/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/V8BNOaftJmYG2hHugNzYued7G9QFdbZOACLcDEAEiGQoBShD___________8BGNuwiP7______wE/s100/photo.jpg",
    //       "default": true
    //     }
    //   ],
    //   "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 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("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;
    }
}