Fix memory leak

I'm not sure I'm happy with this yet, but it works, so who am I to judge
This commit is contained in:
mitteneer 2025-03-07 10:23:03 -05:00
parent 454505335c
commit a8d485ffe1

View file

@ -8,43 +8,33 @@ const Client = std.http.Client;
pub const user_agent: []const u8 = "ZuletztMBClient/0.0.1 (swebbguy@gmail.com)";
pub fn mbSearch(allocator: std.mem.Allocator, track_name: []const u8, album_name: []const u8, artist_name: []const u8) !?QR.Result {
pub fn mbSearch(allocator: std.mem.Allocator, ar: *std.ArrayList(u8), track_name: []const u8, album_name: []const u8, artist_name: []const u8) !?[]const u8 {
var client = Client{ .allocator = allocator };
defer client.deinit();
const query: []const u8 = try std.fmt.allocPrint(allocator, "https://musicbrainz.org/ws/2/recording/?query=\"{s}\"%20AND%20artist:\"{s}\"%20AND%20release:\"{s}\"&fmt=json", .{ track_name, artist_name, album_name });
defer allocator.free(query);
var mb_result = std.ArrayList(u8).init(allocator);
errdefer mb_result.deinit();
const response: Client.FetchResult = try client.fetch(.{ .response_storage = .{ .dynamic = &mb_result }, .location = .{ .url = query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } });
const response: Client.FetchResult = try client.fetch(.{ .response_storage = .{ .dynamic = ar }, .location = .{ .url = query }, .method = .GET, .headers = .{ .user_agent = .{ .override = user_agent } } });
switch (@intFromEnum(response.status)) {
0...299 => std.log.debug("Success", .{}),
0...299 => std.log.debug("Success\n", .{}),
300...399 => {
std.log.debug("Redirected", .{});
std.log.err("Redirected\n", .{});
return null;
},
400...511 => {
std.log.err("Get rekt\n{s}", .{mb_result.items});
std.log.err("Get rekt\n{s}", .{ar.items});
return Client.ConnectError.ConnectionRefused;
},
512 => {
std.log.debug("Need to login", .{});
std.log.err("Need to login\n", .{});
return null;
},
else => unreachable,
}
//std.log.err("{s}", .{mb_result_slice});
const result = try std.json.parseFromSlice(QR.Result, allocator, mb_result.items, .{ .ignore_unknown_fields = true });
defer result.deinit();
const query_result = result.value;
return query_result;
return ar.items;
}
//pub fn getAlbumMBID(query: QR.Result)
@ -66,9 +56,21 @@ test "iamthemorning" {
const iatm_id: []const u8 = "5854a6de-af8f-4b99-8710-cb47d6436a19";
const search_result: ?QR.Result = try mbSearch(test_alloc, track, album, artist);
var mb_result = std.ArrayList(u8).init(test_alloc);
defer mb_result.deinit();
const search_result = try mbSearch(test_alloc, &mb_result, track, album, artist);
if (search_result) |sr| {
const recording = sr.recordings.?[0];
try testing.expect(std.mem.eql(u8, recording.id, iatm_id));
const result = try std.json.parseFromSlice(QR.Result, test_alloc, sr, .{ .ignore_unknown_fields = true });
defer result.deinit();
try testing.expect(std.mem.eql(u8, result.value.recordings.?[0].@"artist-credit"[0].artist.id, iatm_id));
}
//defer result.deinit();
//std.log.err("{s}", .{search_result.?.recordings.?[0].id});
//if (search_result) |sr| {
// const recording = sr.recordings.?[0];
// try testing.expect(std.mem.eql(u8, recording.id, iatm_id));
//}
}