Zig
Zig
UniPin Game List
See more UniPin Examples
Demonstrates how to send a POST with the hash_hmac(sha256,partnerid+timestamp+path,secretkey) authentication.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 --request POST 'https://dev-api.unipin.com/in-game-topup/list' \
// --header 'partnerid: 587e3675-e0ed-4e9e-9b39-099b11498fdc' \
// --header 'timestamp: 1566552295' \
// --header 'path: in-game-topup/list' \
// --header 'auth: 1d9f5e7aca9f3c14da7c957d6977447739877cebfc10fcf3682bd32da47a2bda' \
// --header 'Content-Type: application/json'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
const dt = try chilkat.DateTime.init();
defer dt.deinit();
dt.setFromCurrentSystemTime() catch {};
const timestamp = try dt.getAsUnixTimeStr(alloc, false);
// Change this to your actual partner ID.
const partner_id = "587e3675-e0ed-4e9e-9b39-099b11498fdc";
http.setRequestHeader("path", "in-game-topup/list");
http.setRequestHeader("timestamp", timestamp);
http.setRequestHeader("partnerid", partner_id);
http.setRequestHeader("Content-Type", "application/json");
// Calculate the auth header using hash_hmac(sha256,partnerid+timestamp+path,secretkey)
const crypt = try chilkat.Crypt2.init();
defer crypt.deinit();
crypt.setMacAlgorithm("hmac");
crypt.setHashAlgorithm("sha256");
crypt.setEncodingMode("hexlower");
// Change this to your actual secret key..
crypt.setMacKeyEncoded("wabc123asljdgadlgd3", "us-ascii") catch {};
const sb_mac_data = try chilkat.StringBuilder.init();
defer sb_mac_data.deinit();
sb_mac_data.append(partner_id) catch {};
sb_mac_data.append(timestamp) catch {};
sb_mac_data.append("in-game-topup/list") catch {};
const auth = try crypt.macStringENC(alloc, try sb_mac_data.getAsString(alloc));
http.setRequestHeader("auth", auth);
const resp = try chilkat.HttpResponse.init();
defer resp.deinit();
http.httpNoBody("POST", "https://dev-api.unipin.com/in-game-topup/list", 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)
// {
// "game_list": [
// {
// "game_category": "MLBB_ID",
// "game_code": "MLBBD_ID",
// "game_name": "Mobile Legends Diamonds",
// "icon_url": "http://dev-backoffice.unipin.com/images/icon_direct_topup_games/1565343343-icon-1548659712-icon-Mobile legend 300x300 px.png",
// "game_status": "active",
// "updated_at": "2019-08-09 16:35:43"
// },
// {
// "game_category": "MLBB_ID",
// "game_code": "MLBBS_ID",
// "game_name": "Mobile Legends Starlight Member",
// "icon_url": "http://dev-backoffice.unipin.com/images/icon_direct_topup_games/1565343258-icon-1548659712-icon-Mobile legend 300x300 px.png",
// "game_status": "active",
// "updated_at": "2019-08-09 16:34:18"
// },
// ...
// ...
// ],
// "status": 1,
// "reason": "Successful"
// }
// 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 game_category: [:0]const u8 = "";
var game_code: [:0]const u8 = "";
var game_name: [:0]const u8 = "";
var icon_url: [:0]const u8 = "";
var game_status: [:0]const u8 = "";
var updated_at: [:0]const u8 = "";
_ = j_resp.intOf("status");
_ = try j_resp.stringOf(alloc, "reason");
var i: i32 = 0;
const count_i = j_resp.sizeOfArray("game_list");
while (i < count_i) {
j_resp.setI(i);
game_category = try j_resp.stringOf(alloc, "game_list[i].game_category");
game_code = try j_resp.stringOf(alloc, "game_list[i].game_code");
game_name = try j_resp.stringOf(alloc, "game_list[i].game_name");
icon_url = try j_resp.stringOf(alloc, "game_list[i].icon_url");
game_status = try j_resp.stringOf(alloc, "game_list[i].game_status");
updated_at = try j_resp.stringOf(alloc, "game_list[i].updated_at");
i = i + 1;
}
}