Zig
Zig
Quickbooks Query an Invoice
See more QuickBooks Examples
Demonstrates how to query for invoices matching a SELECT statement via the Quickbooks REST API.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 requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// First get our previously obtained OAuth2 access token.
const json_token = try chilkat.JsonObject.init();
defer json_token.deinit();
try json_token.loadFile("qa_data/tokens/qb-access-token.json");
const rest = try chilkat.Rest.init();
defer rest.deinit();
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("sandbox-quickbooks.api.intuit.com", port, b_tls, b_auto_reconnect);
const sb_auth = try chilkat.StringBuilder.init();
defer sb_auth.deinit();
sb_auth.append("Bearer ") catch {};
sb_auth.append(try json_token.stringOf(alloc, "access_token")) catch {};
rest.setAuthorization(try sb_auth.getAsString(alloc));
// --------------------------------------------------------------------------
// Note: The above REST connection and setup of the AWS credentials
// can be done once. After connecting, any number of REST calls can be made.
// The "auto reconnect" property passed to rest.Connect indicates that if
// the connection is lost, a REST method call will automatically reconnect
// if needed.
// --------------------------------------------------------------------------
// This is a GET request, so there should be no Content-Type
// This line of code is just to make sure..
rest.removeHeader("Content-Type") catch {};
rest.addHeader("Accept", "application/json") catch {};
rest.setAllowHeaderFolding(false);
// Add a SELECT statement
rest.addQueryParam("query", "select * from Invoice where id = '239'") catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
rest.fullRequestNoBodySb("GET", "/v3/company/<realmID>/invoice", sb_response_body) catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
const resp_status_code = rest.getResponseStatusCode();
if (resp_status_code >= 400) {
std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
std.debug.print("Response Header:\n", .{});
std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("Response Body:\n", .{});
std.debug.print("{s}\n", .{try sb_response_body.getAsString(alloc)});
return;
}
// Success is indicated by a 200 response status.
std.debug.print("response status code = {d}\n", .{resp_status_code});
const json = try chilkat.JsonObject.init();
defer json.deinit();
json.loadSb(sb_response_body) catch {};
json.setEmitCompact(false);
std.debug.print("{s}\n", .{try json.emit(alloc)});
// Sample output:
// Use the this online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// {
// "QueryResponse": {
// "startPosition": 1,
// "totalCount": 1,
// "maxResults": 1,
// "Invoice": [
// {
// "DocNumber": "1070",
// "SyncToken": "0",
// "domain": "QBO",
// "Balance": 150.0,
// "BillAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "2"
// },
// "TxnDate": "2015-07-24",
// "TotalAmt": 150.0,
// "CustomerRef": {
// "name": "Amy's Bird Sanctuary",
// "value": "1"
// },
// "ShipAddr": {
// "City": "Bayshore",
// "Line1": "4581 Finch St.",
// "PostalCode": "94326",
// "Lat": "INVALID",
// "Long": "INVALID",
// "CountrySubDivisionCode": "CA",
// "Id": "109"
// },
// "LinkedTxn": [],
// "DueDate": "2015-08-23",
// "PrintStatus": "NeedToPrint",
// "Deposit": 0,
// "sparse": false,
// "EmailStatus": "NotSet",
// "Line": [
// {
// "LineNum": 1,
// "Amount": 150.0,
// "SalesItemLineDetail": {
// "TaxCodeRef": {
// "value": "NON"
// },
// "ItemRef": {
// "name": "Services",
// "value": "1"
// }
// },
// "Id": "1",
// "DetailType": "SalesItemLineDetail"
// },
// {
// "DetailType": "SubTotalLineDetail",
// "Amount": 150.0,
// "SubTotalLineDetail": {}
// }
// ],
// "ApplyTaxAfterDiscount": false,
// "CustomField": [
// {
// "DefinitionId": "1",
// "Type": "StringType",
// "Name": "Crew #"
// }
// ],
// "Id": "239",
// "TxnTaxDetail": {
// "TotalTax": 0
// },
// "MetaData": {
// "CreateTime": "2015-07-24T10:35:08-07:00",
// "LastUpdatedTime": "2015-07-24T10:35:08-07:00"
// }
// }
// ]
// },
// "time": "2015-07-24T10:38:50.01-07:00"
// }
var doc_number: [:0]const u8 = "";
var sync_token: [:0]const u8 = "";
var domain: [:0]const u8 = "";
var balance: [:0]const u8 = "";
var bill_addr_city: [:0]const u8 = "";
var bill_addr_line1: [:0]const u8 = "";
var bill_addr_postal_code: [:0]const u8 = "";
var bill_addr_lat: [:0]const u8 = "";
var bill_addr_long: [:0]const u8 = "";
var bill_addr_country_sub_division_code: [:0]const u8 = "";
var bill_addr_id: [:0]const u8 = "";
var txn_date: [:0]const u8 = "";
var total_amt: [:0]const u8 = "";
var customer_ref_name: [:0]const u8 = "";
var customer_ref_value: [:0]const u8 = "";
var ship_addr_city: [:0]const u8 = "";
var ship_addr_line1: [:0]const u8 = "";
var ship_addr_postal_code: [:0]const u8 = "";
var ship_addr_lat: [:0]const u8 = "";
var ship_addr_long: [:0]const u8 = "";
var ship_addr_country_sub_division_code: [:0]const u8 = "";
var ship_addr_id: [:0]const u8 = "";
var due_date: [:0]const u8 = "";
var print_status: [:0]const u8 = "";
var deposit: i32 = 0;
var sparse: bool = false;
var email_status: [:0]const u8 = "";
var apply_tax_after_discount: bool = false;
var id: [:0]const u8 = "";
var txn_tax_detail_total_tax: i32 = 0;
var meta_data_create_time: [:0]const u8 = "";
var meta_data_last_updated_time: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var line_num: i32 = 0;
var amount: [:0]const u8 = "";
var sales_item_line_detail_tax_code_ref_value: [:0]const u8 = "";
var sales_item_line_detail_item_ref_name: [:0]const u8 = "";
var sales_item_line_detail_item_ref_value: [:0]const u8 = "";
var detail_type: [:0]const u8 = "";
var definition_id: [:0]const u8 = "";
var inv_type: [:0]const u8 = "";
var name: [:0]const u8 = "";
_ = json.intOf("QueryResponse.startPosition");
_ = json.intOf("QueryResponse.totalCount");
_ = json.intOf("QueryResponse.maxResults");
_ = try json.stringOf(alloc, "time");
var i: i32 = 0;
const count_i = json.sizeOfArray("QueryResponse.Invoice");
while (i < count_i) {
json.setI(i);
doc_number = try json.stringOf(alloc, "QueryResponse.Invoice[i].DocNumber");
sync_token = try json.stringOf(alloc, "QueryResponse.Invoice[i].SyncToken");
domain = try json.stringOf(alloc, "QueryResponse.Invoice[i].domain");
balance = try json.stringOf(alloc, "QueryResponse.Invoice[i].Balance");
bill_addr_city = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.City");
bill_addr_line1 = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.Line1");
bill_addr_postal_code = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.PostalCode");
bill_addr_lat = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.Lat");
bill_addr_long = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.Long");
bill_addr_country_sub_division_code = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.CountrySubDivisionCode");
bill_addr_id = try json.stringOf(alloc, "QueryResponse.Invoice[i].BillAddr.Id");
txn_date = try json.stringOf(alloc, "QueryResponse.Invoice[i].TxnDate");
total_amt = try json.stringOf(alloc, "QueryResponse.Invoice[i].TotalAmt");
customer_ref_name = try json.stringOf(alloc, "QueryResponse.Invoice[i].CustomerRef.name");
customer_ref_value = try json.stringOf(alloc, "QueryResponse.Invoice[i].CustomerRef.value");
ship_addr_city = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.City");
ship_addr_line1 = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.Line1");
ship_addr_postal_code = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.PostalCode");
ship_addr_lat = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.Lat");
ship_addr_long = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.Long");
ship_addr_country_sub_division_code = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.CountrySubDivisionCode");
ship_addr_id = try json.stringOf(alloc, "QueryResponse.Invoice[i].ShipAddr.Id");
due_date = try json.stringOf(alloc, "QueryResponse.Invoice[i].DueDate");
print_status = try json.stringOf(alloc, "QueryResponse.Invoice[i].PrintStatus");
deposit = json.intOf("QueryResponse.Invoice[i].Deposit");
sparse = json.boolOf("QueryResponse.Invoice[i].sparse");
email_status = try json.stringOf(alloc, "QueryResponse.Invoice[i].EmailStatus");
apply_tax_after_discount = json.boolOf("QueryResponse.Invoice[i].ApplyTaxAfterDiscount");
id = try json.stringOf(alloc, "QueryResponse.Invoice[i].Id");
txn_tax_detail_total_tax = json.intOf("QueryResponse.Invoice[i].TxnTaxDetail.TotalTax");
meta_data_create_time = try json.stringOf(alloc, "QueryResponse.Invoice[i].MetaData.CreateTime");
meta_data_last_updated_time = try json.stringOf(alloc, "QueryResponse.Invoice[i].MetaData.LastUpdatedTime");
j = 0;
count_j = json.sizeOfArray("QueryResponse.Invoice[i].LinkedTxn");
while (j < count_j) {
json.setJ(j);
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("QueryResponse.Invoice[i].Line");
while (j < count_j) {
json.setJ(j);
line_num = json.intOf("QueryResponse.Invoice[i].Line[j].LineNum");
amount = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].Amount");
sales_item_line_detail_tax_code_ref_value = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.TaxCodeRef.value");
sales_item_line_detail_item_ref_name = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.name");
sales_item_line_detail_item_ref_value = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].SalesItemLineDetail.ItemRef.value");
id = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].Id");
detail_type = try json.stringOf(alloc, "QueryResponse.Invoice[i].Line[j].DetailType");
j = j + 1;
}
j = 0;
count_j = json.sizeOfArray("QueryResponse.Invoice[i].CustomField");
while (j < count_j) {
json.setJ(j);
definition_id = try json.stringOf(alloc, "QueryResponse.Invoice[i].CustomField[j].DefinitionId");
inv_type = try json.stringOf(alloc, "QueryResponse.Invoice[i].CustomField[j].Type");
name = try json.stringOf(alloc, "QueryResponse.Invoice[i].CustomField[j].Name");
j = j + 1;
}
i = i + 1;
}
}