Zig
Zig
SCIS Search
See more SCiS Schools Catalogue Examples
Demonstrates the SCIS (Schools Catalogue Information Service) search API call.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 -i -X GET --url 'https://api.scisdata.com/catalogue/api/search?query=titleSearch%3Adogs%20AND%20publicationYear%3A2015&from=0&size=20&sort=author&order=asc' -H 'Authorization: Basic ****'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// This causes the "Authorization: Basic ****" to be added to each request.
http.setLogin("myLogin");
http.setPassword("myPassword");
http.setBasicAuth(true);
const sb_query = try chilkat.StringBuilder.init();
defer sb_query.deinit();
sb_query.append("titleSearch:dogs AND publicationYear:2015") catch {};
const sb_url = try chilkat.StringBuilder.init();
defer sb_url.deinit();
sb_url.append("https://api.scisdata.com/catalogue/api/search?query=") catch {};
// If non-usascii chars are included in the search, we don't know if utf-8 or windows-1252 is desired by the server. You'll need to find out..
sb_url.append(try sb_query.getEncoded(alloc, "url", "utf-8")) catch {};
sb_url.append("&from=0&size=20&sort=author&order=asc") catch {};
const sb_response_body = try chilkat.StringBuilder.init();
defer sb_response_body.deinit();
http.quickGetSb(try sb_url.getAsString(alloc), 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)
// {
// "data": {
// "hits": {
// "hits": [
// {
// "_index": "scisdata2",
// "_type": "bibdatatype",
// "_source": {
// "isbn": [
// "9781517638160"
// ],
// "languageTermCode": [
// "eng"
// ],
// "mainAuthor": {
// "namePersonalPrimary": [
// "Abbott, Eleanor Hallowell."
// ]
// },
// "title": {
// "titlePrimary": [
// "Peace on Earth, good-will to dogs"
// ],
// "noteStmOfResponsibility": [
// "by Eleanor Hallowell Abbott."
// ]
// },
// "scisType": [
// "Book"
// ],
// "dateIssuedMarc": 2015,
// "languageTermValue": [
// "English"
// ],
// "contributor": {},
// "isbn13": "9781517638160",
// "imageFileName": "9781517638160.jpg",
// "publicationDetails": "United States, Create Space Independent Publishing Platform, 2015",
// "isbn10": "151763816X",
// "id": "1867852"
// },
// "_id": "1867852",
// "sort": [
// "abbott, eleanor hallowell."
// ],
// "_score": null
// },
// {
// "_index": "scisdata2",
// "_type": "bibdatatype",
// "_source": {
// "isbn": [
// "9781780747910"
// ],
// "languageTermCode": [
// "eng"
// ],
// "mainAuthor": {
// "namePersonalPrimary": [
// "Adams, Richard."
// ]
// },
// "title": {
// "titlePrimary": [
// "The plague dogs"
// ],
// "noteStmOfResponsibility": [
// "Richard Adams."
// ]
// },
// "scisType": [
// "Book"
// ],
// "dateIssuedMarc": 2015,
// "languageTermValue": [
// "English"
// ],
// "contributor": {},
// "isbn13": "9781780747910",
// "imageFileName": "9781780747910.jpg",
// "publicationDetails": "New York, Rock the Boat, 2015",
// "isbn10": "1780747918",
// "id": "1749228"
// },
// "_id": "1749228",
// "sort": [
// "adams, richard."
// ],
// "_score": null
// },
// ...
// ],
// "total": 84,
// "max_score": null
// },
// "took": 585,
// "timed_out": false
// },
// "subscriptionStatus": {}
// }
// 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_index: [:0]const u8 = "";
var v_type: [:0]const u8 = "";
var v_source_date_issued_marc: i32 = 0;
var v_source_isbn13: [:0]const u8 = "";
var v_source_image_file_name: [:0]const u8 = "";
var v_source_publication_details: [:0]const u8 = "";
var v_source_isbn10: [:0]const u8 = "";
var v_source_id: [:0]const u8 = "";
var v_id: [:0]const u8 = "";
var v_score: [:0]const u8 = "";
var j: i32 = 0;
var count_j: i32 = 0;
var str_val: [:0]const u8 = "";
_ = j_resp.intOf("data.hits.total");
_ = try j_resp.stringOf(alloc, "data.hits.max_score");
_ = j_resp.intOf("data.took");
_ = j_resp.boolOf("data.timed_out");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("data.hits.hits");
while (i < count_i) {
j_resp.setI(i);
v_index = try j_resp.stringOf(alloc, "data.hits.hits[i]._index");
v_type = try j_resp.stringOf(alloc, "data.hits.hits[i]._type");
v_source_date_issued_marc = j_resp.intOf("data.hits.hits[i]._source.dateIssuedMarc");
v_source_isbn13 = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.isbn13");
v_source_image_file_name = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.imageFileName");
v_source_publication_details = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.publicationDetails");
v_source_isbn10 = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.isbn10");
v_source_id = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.id");
v_id = try j_resp.stringOf(alloc, "data.hits.hits[i]._id");
v_score = try j_resp.stringOf(alloc, "data.hits.hits[i]._score");
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.isbn");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.isbn[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.languageTermCode");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.languageTermCode[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.mainAuthor.namePersonalPrimary");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.mainAuthor.namePersonalPrimary[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.title.titlePrimary");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.title.titlePrimary[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.title.noteStmOfResponsibility");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.title.noteStmOfResponsibility[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.scisType");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.scisType[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.languageTermValue");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.languageTermValue[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i].sort");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i].sort[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.contributor.namePersonalOther");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.contributor.namePersonalOther[j]");
j = j + 1;
}
j = 0;
count_j = j_resp.sizeOfArray("data.hits.hits[i]._source.contributor.nameCorporateOther");
while (j < count_j) {
j_resp.setJ(j);
str_val = try j_resp.stringOf(alloc, "data.hits.hits[i]._source.contributor.nameCorporateOther[j]");
j = j + 1;
}
i = i + 1;
}
}