diff options
Diffstat (limited to 'src/http.zig')
| -rw-r--r-- | src/http.zig | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/http.zig b/src/http.zig new file mode 100644 index 0000000..bb3b3ac --- /dev/null +++ b/src/http.zig | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const zelda = @import("zelda"); | ||
| 3 | |||
| 4 | const Allocator = std.mem.Allocator; | ||
| 5 | const File = std.fs.File; | ||
| 6 | |||
| 7 | // TODO: zelda should be able to do this internally so I wouldn't have to allocate memory for the | ||
| 8 | // file | ||
| 9 | pub fn downloadToFile(allocator: Allocator, file: *File, url: []const u8) !void { | ||
| 10 | var response = try zelda.get(allocator, url); | ||
| 11 | defer response.deinit(); | ||
| 12 | |||
| 13 | if (response.status_code.group() != .success) { | ||
| 14 | std.log.err("HTTP Error: {}", .{response.status_code}); | ||
| 15 | return error.HttpError; | ||
| 16 | } | ||
| 17 | |||
| 18 | if (response.body) |body| { | ||
| 19 | try file.writer().writeAll(body); | ||
| 20 | } else { | ||
| 21 | std.log.warn("Downloaded nothing from {s}...", .{url}); | ||
| 22 | } | ||
| 23 | } | ||