Zig
Zig
Shippo Validate Global Address
See more Shippo Examples
Demonstrates how to validate a global address.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.
const http = try chilkat.Http.init();
defer http.deinit();
// Implements the following CURL command:
// curl https://api.goshippo.com/addresses/ \
// -H "Authorization: ShippoToken <API_TOKEN>" \
// -d name="Shawn Ippotle" \
// -d company="Shippo" \
// -d street1="Kortrijksesteenweg 1005" \
// -d city="Gent" \
// -d zip=9000 \
// -d country="BE" \
// -d email="shippotle@goshippo.com"\
// -d validate=true
const req = try chilkat.HttpRequest.init();
defer req.deinit();
req.setHttpVerb("POST");
req.setPath("/addresses/");
req.setContentType("application/x-www-form-urlencoded");
req.addParam("name", "Shawn Ippotle");
req.addParam("company", "Shippo");
req.addParam("street1", "Kortrijksesteenweg 1005");
req.addParam("city", "Gent");
req.addParam("zip", "9000");
req.addParam("country", "BE");
req.addParam("email", "shippotle@goshippo.com");
req.addParam("validate", "true");
req.addHeader("Authorization", "ShippoToken <API_TOKEN>");
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpReq("https://api.goshippo.com/addresses/", req, 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)
// {
// "object_created": "2017-07-26T17:52:37.305Z",
// "object_updated": "2017-07-26T17:52:37.351Z",
// "object_id": "b7f9709df3914d1ca6efe4c30e7b0572",
// "is_complete": true,
// "validation_results": {
// "is_valid": true,
// "messages": [
// {
// "source": "Shippo Address Validator",
// "type": "address_correction",
// "code": "administrative_area_change",
// "text": "The administrative area (state or province) was added or changed."
// },
// {
// "source": "Shippo Address Validator",
// "code": "geocoded_rooftop",
// "text": "The record was geocoded down to rooftop level, meaning the point is within the property boundaries (most often the center)."
// },
// {
// "source": "Shippo Address Validator",
// "code": "premises_full",
// "text": "The address has been verified to the Premise (House or Building) Level, which is the highest level possible with the reference data."
// }
// ]
// },
// "object_owner": "hippo@goshippo.com",
// "name": "Hippo Shippo",
// "company": "Shippo",
// "street_no": "2",
// "street1": "Unter den Linden",
// "street2": "",
// "street3": "",
// "city": "Berlin",
// "state": "",
// "zip": "10117",
// "country": "DE",
// "longitude": "13.39751",
// "latitude": "52.51785",
// "phone": "14151234567",
// "email": "hippo@goshippo.com",
// "is_residential": null,
// "metadata": "",
// "test": false
// }
// 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 source: [:0]const u8 = "";
var v_type: [:0]const u8 = "";
var code: [:0]const u8 = "";
var text: [:0]const u8 = "";
_ = try j_resp.stringOf(alloc, "object_created");
_ = try j_resp.stringOf(alloc, "object_updated");
_ = try j_resp.stringOf(alloc, "object_id");
_ = j_resp.boolOf("is_complete");
_ = j_resp.boolOf("validation_results.is_valid");
_ = try j_resp.stringOf(alloc, "object_owner");
_ = try j_resp.stringOf(alloc, "name");
_ = try j_resp.stringOf(alloc, "company");
_ = try j_resp.stringOf(alloc, "street_no");
_ = try j_resp.stringOf(alloc, "street1");
_ = try j_resp.stringOf(alloc, "street2");
_ = try j_resp.stringOf(alloc, "street3");
_ = try j_resp.stringOf(alloc, "city");
_ = try j_resp.stringOf(alloc, "state");
_ = try j_resp.stringOf(alloc, "zip");
_ = try j_resp.stringOf(alloc, "country");
_ = try j_resp.stringOf(alloc, "longitude");
_ = try j_resp.stringOf(alloc, "latitude");
_ = try j_resp.stringOf(alloc, "phone");
_ = try j_resp.stringOf(alloc, "email");
_ = try j_resp.stringOf(alloc, "is_residential");
_ = try j_resp.stringOf(alloc, "metadata");
_ = j_resp.boolOf("test");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("validation_results.messages");
while (i < count_i) {
j_resp.setI(i);
source = try j_resp.stringOf(alloc, "validation_results.messages[i].source");
v_type = try j_resp.stringOf(alloc, "validation_results.messages[i].type");
code = try j_resp.stringOf(alloc, "validation_results.messages[i].code");
text = try j_resp.stringOf(alloc, "validation_results.messages[i].text");
i = i + 1;
}
}