summaryrefslogtreecommitdiff
path: root/src/http.zig
blob: bb3b3ac888285c866bee38f94572dfa73182a85f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const std = @import("std");
const zelda = @import("zelda");

const Allocator = std.mem.Allocator;
const File = std.fs.File;

// TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the
// file
pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void {
    var response = try zelda.get(allocator, url);
    defer response.deinit();

    if (response.status_code.group() != .success) {
        std.log.err("HTTP Error: {}", .{response.status_code});
        return error.HttpError;
    }

    if (response.body) |body| {
        try file.writer().writeAll(body);
    } else {
        std.log.warn("Downloaded nothing from {s}...", .{url});
    }
}