Add a result type to get requests
This commit is contained in:
parent
75a3cf2a97
commit
9364729873
20
src/app.zig
20
src/app.zig
|
@ -238,7 +238,7 @@ fn updateAlert(
|
|||
|
||||
try request.append(0);
|
||||
|
||||
var streams: twitch.TwitchRes([]const twitch.Stream) = try client.getJSON(
|
||||
var result: curl.Result(twitch.TwitchRes([]const twitch.Stream)) = try client.getJSON(
|
||||
twitch.TwitchRes([]const twitch.Stream),
|
||||
@ptrCast([*:0]const u8, request.items),
|
||||
headers,
|
||||
|
@ -246,6 +246,8 @@ fn updateAlert(
|
|||
|
||||
request.deinit();
|
||||
|
||||
switch (result) {
|
||||
.ok => |*streams| {
|
||||
if (streams.data.len > 0) {
|
||||
var embeds = std.ArrayList(webhook.Embed).init(allocator);
|
||||
|
||||
|
@ -269,6 +271,12 @@ fn updateAlert(
|
|||
}
|
||||
|
||||
streams.deinit(allocator);
|
||||
},
|
||||
.errorCode => |errorCode| {
|
||||
std.log.err("Failed to get streams : error code {}\n", .{errorCode});
|
||||
return error.CurlFailed;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn appendEmbed(allocator: std.mem.Allocator, stream: *const twitch.Stream, db: *sqlite.Database) anyerror!?webhook.Embed {
|
||||
|
@ -442,12 +450,14 @@ fn insertOrReplaceStreamers(
|
|||
|
||||
try request.append(0);
|
||||
|
||||
var streamers: twitch.TwitchRes([]const twitch.User) = try client.getJSON(
|
||||
var result: curl.Result(twitch.TwitchRes([]const twitch.User)) = try client.getJSON(
|
||||
twitch.TwitchRes([]const twitch.User),
|
||||
@ptrCast([*:0]const u8, request.items),
|
||||
headers,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
.ok => |*streamers| {
|
||||
for (streamers.data) |streamer| {
|
||||
var stm = try db.prepare("INSERT OR REPLACE INTO STREAMER(idStreamer, loginStreamer, nameStreamer, imageUrlStreamer) VALUES(?, ?, ?, ?)");
|
||||
try stm.bind(1, sqlite.U8Array.text(streamer.id));
|
||||
|
@ -460,4 +470,10 @@ fn insertOrReplaceStreamers(
|
|||
}
|
||||
|
||||
streamers.deinit(allocator);
|
||||
},
|
||||
.errorCode => |errorCode| {
|
||||
std.log.err("Failed to get streamers : error code {}\n", .{errorCode});
|
||||
return error.CurlFailed;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
25
src/curl.zig
25
src/curl.zig
|
@ -12,6 +12,15 @@ const ArrayListReader = struct {
|
|||
position: usize,
|
||||
};
|
||||
|
||||
const ResultTag = enum { ok, errorCode };
|
||||
|
||||
pub fn Result(comptime T: type) type {
|
||||
return union(ResultTag) {
|
||||
ok: T,
|
||||
errorCode: c_long,
|
||||
};
|
||||
}
|
||||
|
||||
pub const Client = struct {
|
||||
ptr: *cURL.CURL,
|
||||
allocator: mem.Allocator,
|
||||
|
@ -57,7 +66,7 @@ pub const Client = struct {
|
|||
return error.CURLPerformFailed;
|
||||
}
|
||||
|
||||
pub fn getJSON(self: *@This(), comptime T: type, url: [*:0]const u8, headers: ?*std.StringHashMap([]const u8)) anyerror!T {
|
||||
pub fn getJSON(self: *@This(), comptime T: type, url: [*:0]const u8, headers: ?*std.StringHashMap([]const u8)) anyerror!Result(T) {
|
||||
var response_buffer = std.ArrayList(u8).init(self.allocator);
|
||||
if (cURL.curl_easy_setopt(self.ptr, cURL.CURLOPT_URL, url) != cURL.CURLE_OK)
|
||||
return error.CURLPerformFailed;
|
||||
|
@ -92,17 +101,27 @@ pub const Client = struct {
|
|||
if (cURL.curl_easy_perform(self.ptr) != cURL.CURLE_OK)
|
||||
return error.CURLPerformFailed;
|
||||
|
||||
var httpCode: c_long = undefined;
|
||||
if (cURL.curl_easy_getinfo(self.ptr, cURL.CURLINFO_RESPONSE_CODE, &httpCode) != cURL.CURLE_OK)
|
||||
return error.CURLPerformFailed;
|
||||
|
||||
if (header_slist != null)
|
||||
cURL.curl_slist_free_all(header_slist);
|
||||
|
||||
if (httpCode == 200) {
|
||||
var content = response_buffer.items;
|
||||
var stream = json.TokenStream.init(content);
|
||||
|
||||
@setEvalBranchQuota(10_000);
|
||||
const res = json.parse(T, &stream, .{ .allocator = self.allocator, .ignore_unknown_fields = true });
|
||||
const res = try json.parse(T, &stream, .{ .allocator = self.allocator, .ignore_unknown_fields = true });
|
||||
|
||||
response_buffer.deinit();
|
||||
return res;
|
||||
return Result(T){ .ok = res };
|
||||
} else {
|
||||
response_buffer.deinit();
|
||||
|
||||
return Result(T){ .errorCode = httpCode };
|
||||
}
|
||||
}
|
||||
|
||||
pub fn postJSON(self: *@This(), url: []const u8, data: anytype, headers: ?std.StringHashMap([]const u8)) anyerror![]const u8 {
|
||||
|
|
Loading…
Reference in New Issue