Zig
Zig
Shippo List All Orders
See more Shippo Examples
Demonstrates how to list all orders created by sending a GET request to the orders endpoint.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/orders/ \
// -H "Authorization: ShippoToken <API_Token>"
http.setRequestHeader("Authorization", "ShippoToken <API_Token>");
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb("https://api.goshippo.com/orders/", 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)
// {
// "count": 1382,
// "next": "https://api.goshippo.com/orders/?page=2",
// "previous": null,
// "results": [
// {
// "object_id": "4f2bc588e4e5446cb3f9fdb7cd5e190b",
// "object_owner": "shippotle@goshippo.com",
// "order_number": "#1068",
// "order_status": "PAID",
// "placed_at": "2016-09-23T01:28:12Z",
// "to_address": {
// "object_created": "2016-09-23T01:38:56Z",
// "object_updated": "2016-09-23T01:38:56Z",
// "object_id": "d799c2679e644279b59fe661ac8fa488",
// "object_owner": "shippotle@goshippo.com",
// "is_complete": true,
// "validation_results": {},
// "name": "Mr Hippo",
// "company": "Shippo",
// "street1": "215 Clayton St.",
// "street2": "",
// "city": "San Francisco",
// "state": "CA",
// "zip": "94117",
// "country": "US",
// "phone": "15553419393",
// "email": "shippotle@goshippo.com",
// "is_residential": null,
// "metadata": ""
// },
// "from_address": null,
// "line_items": [
// {
// "object_id": "abf7d5675d744b6ea9fdb6f796b28f28",
// "title": "Hippo Magazines",
// "variant_title": "",
// "sku": "HM-123",
// "quantity": 1,
// "total_price": "12.10",
// "currency": "USD",
// "weight": "0.40",
// "weight_unit": "lb",
// "manufacture_country": null,
// "max_ship_time": null,
// "max_delivery_time": null,
// "description": null
// }
// ],
// "shipping_cost": "12.83",
// "shipping_cost_currency": "USD",
// "shipping_method": "USPS First Class Package",
// "shop_app": "Shippo",
// "subtotal_price": "12.10",
// "total_price": "24.93",
// "total_tax": "0.00",
// "currency": "USD",
// "transactions": [
// ],
// "weight": "0.40",
// "weight_unit": "lb",
// "notes": 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
var int_val: i32 = 0;
var j: i32 = 0;
var count_j: i32 = 0;
var object_id: [:0]const u8 = "";
var title: [:0]const u8 = "";
var variant_title: [:0]const u8 = "";
var sku: [:0]const u8 = "";
var quantity: i32 = 0;
var total_price: [:0]const u8 = "";
var currency: [:0]const u8 = "";
var weight: [:0]const u8 = "";
var weight_unit: [:0]const u8 = "";
var manufacture_country: [:0]const u8 = "";
var max_ship_time: [:0]const u8 = "";
var max_delivery_time: [:0]const u8 = "";
var description: [:0]const u8 = "";
_ = j_resp.intOf("count");
_ = try j_resp.stringOf(alloc, "next");
_ = try j_resp.stringOf(alloc, "previous");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("results");
while (i < count_i) {
j_resp.setI(i);
int_val = j_resp.intOf("results[i]");
j = 0;
count_j = j_resp.sizeOfArray("results[i].line_items");
while (j < count_j) {
j_resp.setJ(j);
object_id = try j_resp.stringOf(alloc, "results[i].line_items[j].object_id");
title = try j_resp.stringOf(alloc, "results[i].line_items[j].title");
variant_title = try j_resp.stringOf(alloc, "results[i].line_items[j].variant_title");
sku = try j_resp.stringOf(alloc, "results[i].line_items[j].sku");
quantity = j_resp.intOf("results[i].line_items[j].quantity");
total_price = try j_resp.stringOf(alloc, "results[i].line_items[j].total_price");
currency = try j_resp.stringOf(alloc, "results[i].line_items[j].currency");
weight = try j_resp.stringOf(alloc, "results[i].line_items[j].weight");
weight_unit = try j_resp.stringOf(alloc, "results[i].line_items[j].weight_unit");
manufacture_country = try j_resp.stringOf(alloc, "results[i].line_items[j].manufacture_country");
max_ship_time = try j_resp.stringOf(alloc, "results[i].line_items[j].max_ship_time");
max_delivery_time = try j_resp.stringOf(alloc, "results[i].line_items[j].max_delivery_time");
description = try j_resp.stringOf(alloc, "results[i].line_items[j].description");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("results[i].transactions");
while (j < count_j) {
j_resp.setJ(j);
j = j + 1;
}
i = i + 1;
}
}