Zig
Zig
SMSAPI - List Contacts
See more SMSAPI Examples
List ContactsChilkat 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.
const http = try chilkat.Http.init();
defer http.deinit();
// Implements the following CURL command:
// curl -X GET -H "Authorization: Bearer token_api_oauth" https://api.smsapi.com/contacts?phone_number=48500000000
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Adds the "Authorization: Bearer token_api_oauth" header.
http.setAuthToken("token_api_oauth");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://api.smsapi.com/contacts?phone_number=48500000000", sb_response_body) catch {
std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
return;
};
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 = http.getLastStatus();
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 http.getLastHeader(alloc)});
std.debug.print("Failed.\n", .{});
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "size": 1,
// "collection": [
// {
// "id": "5b83ba81a788494a0469490f",
// "first_name": "name",
// "last_name": "surname",
// "phone_number": "48500000000",
// "email": "bok@smsapi.com",
// "gender": "male",
// "city": "City",
// "country": "Poland",
// "source": "source",
// "date_created": "2018-08-27T10:46:57+02:00",
// "date_updated": "2018-08-27T10:46:57+02:00",
// "groups": [
// {
// "id": "59a3ca1fa78849062837cd0c",
// "name": "default",
// "date_created": "2017-08-28T09:45:35+02:00",
// "date_updated": "2017-08-28T09:45:35+02:00",
// "description": "",
// "created_by": "login",
// "idx": null,
// "contact_expire_after": null,
// "contacts_count": null
// }
// ]
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
const date_created = try chilkat.DtObj.init();
defer date_created.deinit();
const date_updated = try chilkat.DtObj.init();
defer date_updated.deinit();
var id: [:0]const u8 = "";
var first_name: [:0]const u8 = "";
var last_name: [:0]const u8 = "";
var phone_number: [:0]const u8 = "";
var email: [:0]const u8 = "";
var gender: [:0]const u8 = "";
var city: [:0]const u8 = "";
var country: [:0]const u8 = "";
var source: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var name: [:0]const u8 = "";
var description: [:0]const u8 = "";
var created_by: [:0]const u8 = "";
var idx: [:0]const u8 = "";
var contact_expire_after: [:0]const u8 = "";
var contacts_count: [:0]const u8 = "";
_ = j_resp.intOf("size");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("collection");
while (i < count_i) {
j_resp.setI(i);
id = try j_resp.stringOf(alloc, "collection[i].id");
first_name = try j_resp.stringOf(alloc, "collection[i].first_name");
last_name = try j_resp.stringOf(alloc, "collection[i].last_name");
phone_number = try j_resp.stringOf(alloc, "collection[i].phone_number");
email = try j_resp.stringOf(alloc, "collection[i].email");
gender = try j_resp.stringOf(alloc, "collection[i].gender");
city = try j_resp.stringOf(alloc, "collection[i].city");
country = try j_resp.stringOf(alloc, "collection[i].country");
source = try j_resp.stringOf(alloc, "collection[i].source");
j_resp.dtOf("collection[i].date_created", false, date_created) catch {};
j_resp.dtOf("collection[i].date_updated", false, date_updated) catch {};
j = 0;
count_j = j_resp.sizeOfArray("collection[i].groups");
while (j < count_j) {
j_resp.setJ(j);
id = try j_resp.stringOf(alloc, "collection[i].groups[j].id");
name = try j_resp.stringOf(alloc, "collection[i].groups[j].name");
j_resp.dtOf("collection[i].groups[j].date_created", false, date_created) catch {};
j_resp.dtOf("collection[i].groups[j].date_updated", false, date_updated) catch {};
description = try j_resp.stringOf(alloc, "collection[i].groups[j].description");
created_by = try j_resp.stringOf(alloc, "collection[i].groups[j].created_by");
idx = try j_resp.stringOf(alloc, "collection[i].groups[j].idx");
contact_expire_after = try j_resp.stringOf(alloc, "collection[i].groups[j].contact_expire_after");
contacts_count = try j_resp.stringOf(alloc, "collection[i].groups[j].contacts_count");
j = j + 1;
}
i = i + 1;
}
}