Sample code for 30+ languages & platforms
Zig

MercadoLibre - Consultar mis datos personales

See more MercadoLibre Examples

Consultar mis datos personales

Chilkat Zig Downloads

Zig
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();

    // 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/mercadolibre.json");

    // Implements the following CURL command:

    // curl - X GET https://api.mercadolibre.com/users/me?access_token=$ACCESS_TOKEN

    // Use the following online tool to generate HTTP code from a CURL command
    // Convert a cURL Command to HTTP Source Code

    http.setUrlVar("access_token", try json_token.stringOf(alloc, "access_token")) catch {};

    const sb_response_body = try chilkat.StringBuilder.init();
    defer sb_response_body.deinit();
    http.quickGetSb("https://api.mercadolibre.com/users/me?access_token={$access_token}", 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)

    // {
    //   "id": 202593498,
    //   "nickname": "TETE2870021",
    //   "registration_date": "2016-01-06T11: 31: 42.000-04: 00",
    //   "country_id": "AR",
    //   "address": {
    //     "state": "AR-C",
    //     "city": "Palermo"
    //   },
    //   "user_type": "normal",
    //   "tags": [
    //     "normal",
    //     "test_user",
    //     "user_info_verified"
    //   ],
    //   "logo": null,
    //   "points": 100,
    //   "site_id": "MLA",
    //   "permalink": "http://perfil.mercadolibre.com.ar/TETE2870021",
    //   "seller_reputation": {
    //     "level_id": null,
    //     "power_seller_status": null,
    //     "transactions": {
    //       "period": "historic",
    //       "total": 0,
    //       "completed": 0,
    //       "canceled": 0,
    //       "ratings": {
    //         "positive": 0,
    //         "negative": 0,
    //         "neutral": 0
    //       }
    //     }
    //   },
    //   "buyer_reputation": {
    //     "tags": [
    //     ]
    //   },
    //   "status": {
    //     "site_status": "active"
    //   }
    // }

    // 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 str_val: [:0]const u8 = "";

    _ = j_resp.intOf("id");
    _ = try j_resp.stringOf(alloc, "nickname");
    _ = try j_resp.stringOf(alloc, "registration_date");
    _ = try j_resp.stringOf(alloc, "country_id");
    _ = try j_resp.stringOf(alloc, "address.state");
    _ = try j_resp.stringOf(alloc, "address.city");
    _ = try j_resp.stringOf(alloc, "user_type");
    _ = try j_resp.stringOf(alloc, "logo");
    _ = j_resp.intOf("points");
    _ = try j_resp.stringOf(alloc, "site_id");
    _ = try j_resp.stringOf(alloc, "permalink");
    _ = try j_resp.stringOf(alloc, "seller_reputation.level_id");
    _ = try j_resp.stringOf(alloc, "seller_reputation.power_seller_status");
    _ = try j_resp.stringOf(alloc, "seller_reputation.transactions.period");
    _ = j_resp.intOf("seller_reputation.transactions.total");
    _ = j_resp.intOf("seller_reputation.transactions.completed");
    _ = j_resp.intOf("seller_reputation.transactions.canceled");
    _ = j_resp.intOf("seller_reputation.transactions.ratings.positive");
    _ = j_resp.intOf("seller_reputation.transactions.ratings.negative");
    _ = j_resp.intOf("seller_reputation.transactions.ratings.neutral");
    _ = try j_resp.stringOf(alloc, "status.site_status");
    var i: i32 = 0;
    var count_i: i32 = j_resp.sizeOfArray("tags");
    while (i < count_i) {
        j_resp.setI(i);
        str_val = try j_resp.stringOf(alloc, "tags[i]");
        i = i + 1;
    }

    i = 0;
    count_i = j_resp.sizeOfArray("buyer_reputation.tags");
    while (i < count_i) {
        j_resp.setI(i);
        i = i + 1;
    }
}