Sample code for 30+ languages & platforms
Zig

ETrade v1 List Accounts

See more HTTP Misc Examples

List ETrade accounts using the ETrade v1 API.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const http = try chilkat.Http.init();
    defer http.deinit();

    http.setOAuth1(true);
    http.setOAuthVerifier("");
    http.setOAuthConsumerKey("ETRADE_CONSUMER_KEY");
    http.setOAuthConsumerSecret("ETRADE_CONSUMER_SECRET");

    // Load the access token previously obtained via the OAuth1 3-Legged Authorization examples Step1 and Step2.
    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.loadFile("qa_data/tokens/etrade.json") catch {
        std.debug.print("Failed to load OAuth1 token\n", .{});
        return;
    };

    http.setOAuthToken(try json.stringOf(alloc, "oauth_token"));
    http.setOAuthTokenSecret(try json.stringOf(alloc, "oauth_token_secret"));

    // See the ETrade v1 API documentation HERE.

    const resp_str = http.quickGetStr(alloc, "https://apisb.etrade.com/v1/accounts/list") catch {
        std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        return;
    };

    // A 200 status code indicates success.
    const status_code = http.getLastStatus();
    std.debug.print("statusCode = {d}\n", .{status_code});

    // Use the following online tool to generate parsing code from sample XML:
    // Generate Parsing Code from XML

    // A sample XML response is shown below...

    const xml = try chilkat.Xml.init();
    defer xml.deinit();
    xml.loadXml(resp_str) catch {};

    var i: i32 = 0;
    var account_id: i32 = 0;
    var account_id_key: [:0]const u8 = "";
    var account_mode: [:0]const u8 = "";
    var account_desc: [:0]const u8 = "";
    var account_name: [:0]const u8 = "";
    var account_type: [:0]const u8 = "";
    var institution_type: [:0]const u8 = "";
    var account_status: [:0]const u8 = "";
    var closed_date: i32 = 0;

    i = 0;
    const count_i = xml.numChildrenHavingTag("Accounts|Account");
    while (i < count_i) {
        xml.setI(i);
        account_id = xml.getChildIntValue("Accounts|Account[i]|accountId");
        account_id_key = try xml.getChildContent(alloc, "Accounts|Account[i]|accountIdKey");
        account_mode = try xml.getChildContent(alloc, "Accounts|Account[i]|accountMode");
        account_desc = try xml.getChildContent(alloc, "Accounts|Account[i]|accountDesc");
        account_name = try xml.getChildContent(alloc, "Accounts|Account[i]|accountName");
        account_type = try xml.getChildContent(alloc, "Accounts|Account[i]|accountType");
        institution_type = try xml.getChildContent(alloc, "Accounts|Account[i]|institutionType");
        account_status = try xml.getChildContent(alloc, "Accounts|Account[i]|accountStatus");
        closed_date = xml.getChildIntValue("Accounts|Account[i]|closedDate");
        i = i + 1;
    }

    // <?xml version="1.0" encoding="UTF-8"?>
    // <AccountListResponse>
    //    <Accounts>
    //       <Account>
    //          <accountId>84010429</accountId>
    //          <accountIdKey>JIdOIAcSpwR1Jva7RQBraQ</accountIdKey>
    //          <accountMode>MARGIN</accountMode>
    //          <accountDesc>INDIVIDUAL</accountDesc>
    //          <accountName>Individual Brokerage</accountName>
    //          <accountType>INDIVIDUAL</accountType>
    //          <institutionType>BROKERAGE</institutionType>
    //          <accountStatus>ACTIVE</accountStatus>
    //          <closedDate>0</closedDate>
    //       </Account>
    //       <Account>
    //          <accountId>84010430</accountId>
    //          <accountIdKey>JAAOIAcSpwR1Jva7RQBraQ</accountIdKey>
    //          <accountMode>MARGIN</accountMode>
    //          <accountDesc>INDIVIDUAL</accountDesc>
    //          <accountName>Individual Brokerage</accountName>
    //          <accountType>INDIVIDUAL</accountType>
    //          <institutionType>BROKERAGE</institutionType>
    //          <accountStatus>ACTIVE</accountStatus>
    //          <closedDate>0</closedDate>
    //       </Account>
    //    </Accounts>
    // </AccountListResponse>
}