use std.json for the json formatting
This commit is contained in:
parent
373c30d947
commit
4b2493afe0
1 changed files with 10 additions and 10 deletions
|
|
@ -58,18 +58,18 @@ pub fn lookupCusips(
|
|||
) ![]FigiResult {
|
||||
if (cusips.len == 0) return try allocator.alloc(FigiResult, 0);
|
||||
|
||||
// Build JSON request body: [{"idType":"ID_CUSIP","idValue":"..."},...]
|
||||
var body_buf: std.ArrayList(u8) = .empty;
|
||||
defer body_buf.deinit(allocator);
|
||||
const Job = struct { idType: []const u8, idValue: []const u8 };
|
||||
|
||||
try body_buf.append(allocator, '[');
|
||||
// Build jobs array
|
||||
var jobs = try allocator.alloc(Job, cusips.len);
|
||||
defer allocator.free(jobs);
|
||||
for (cusips, 0..) |cusip, i| {
|
||||
if (i > 0) try body_buf.append(allocator, ',');
|
||||
try body_buf.appendSlice(allocator, "{\"idType\":\"ID_CUSIP\",\"idValue\":\"");
|
||||
try body_buf.appendSlice(allocator, cusip);
|
||||
try body_buf.appendSlice(allocator, "\"}");
|
||||
jobs[i] = .{ .idType = "ID_CUSIP", .idValue = cusip };
|
||||
}
|
||||
try body_buf.append(allocator, ']');
|
||||
|
||||
// Serialize to JSON
|
||||
const body = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(jobs, .{})});
|
||||
defer allocator.free(body);
|
||||
|
||||
// Build headers
|
||||
var headers_buf: [2]std.http.Header = undefined;
|
||||
|
|
@ -84,7 +84,7 @@ pub fn lookupCusips(
|
|||
var client = http.Client.init(allocator);
|
||||
defer client.deinit();
|
||||
|
||||
var response = try client.post(api_url, body_buf.items, headers_buf[0..n_headers]);
|
||||
var response = try client.post(api_url, body, headers_buf[0..n_headers]);
|
||||
defer response.deinit();
|
||||
|
||||
return parseResponse(allocator, response.body, cusips.len);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue