Zig
Zig
ETrade v1 List Orders
See more HTTP Misc Examples
Gets the order details for a selected brokerage account based on the search criteria provided.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.
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.
http.setUrlVar("accountIdKey", "vsnhtF7d9jXxBy6HyaAC4vQ") catch {};
const resp_str = http.quickGetStr(alloc, "https://apisb.etrade.com/v1/accounts/{$accountIdKey}/orders?securityType=EQ&count=100") 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 order_id: i32 = 0;
var details: [:0]const u8 = "";
var order_type: [:0]const u8 = "";
var order_detail: [:0]const u8 = "";
i = 0;
const count_i = xml.numChildrenHavingTag("Order");
while (i < count_i) {
xml.setI(i);
order_id = xml.getChildIntValue("Order[i]|orderId");
details = try xml.getChildContent(alloc, "Order[i]|details");
order_type = try xml.getChildContent(alloc, "Order[i]|orderType");
order_detail = try xml.getChildContent(alloc, "Order[i]|OrderDetail");
i = i + 1;
}
// Sample XML Response
// <?xml version="1.0" encoding="UTF-8"?>
// <OrdersResponse>
// <Order>
// <orderId>96</orderId>
// <details>https://api.etrade.com/v1/accounts/ZrnXF-hPu853sBzwHfWGBQ/orders/96</details>
// <orderType>EQ</orderType>
// <OrderDetail>�</OrderDetail>
// </Order>
// <Order>
// <orderId>95</orderId>
// <details>https://api.etrade.com/v1/accounts/ZrnXF-hPu853sBzwHfWGBQ/orders/95</details>
// <orderType>EQ</orderType>
// <OrderDetail>�</OrderDetail>
// </Order>
// <Order>
// <orderId>94</orderId>
// <details>https://api.etrade.com/v1/accounts/ZrnXF-hPu853sBzwHfWGBQ/orders/94</details>
// <orderType>EQ</orderType>
// <OrderDetail>�</OrderDetail>
// </Order>
// <Order>
// <orderId>93</orderId>
// <details>https://api.etrade.com/v1/accounts/ZrnXF-hPu853sBzwHfWGBQ/orders/93</details>
// <orderType>EQ</orderType>
// <OrderDetail>�</OrderDetail>
// </Order>
// </OrdersResponse>
}