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);
|
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),
|
twitch.TwitchRes([]const twitch.Stream),
|
||||||
@ptrCast([*:0]const u8, request.items),
|
@ptrCast([*:0]const u8, request.items),
|
||||||
headers,
|
headers,
|
||||||
|
@ -246,6 +246,8 @@ fn updateAlert(
|
||||||
|
|
||||||
request.deinit();
|
request.deinit();
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
.ok => |*streams| {
|
||||||
if (streams.data.len > 0) {
|
if (streams.data.len > 0) {
|
||||||
var embeds = std.ArrayList(webhook.Embed).init(allocator);
|
var embeds = std.ArrayList(webhook.Embed).init(allocator);
|
||||||
|
|
||||||
|
@ -269,6 +271,12 @@ fn updateAlert(
|
||||||
}
|
}
|
||||||
|
|
||||||
streams.deinit(allocator);
|
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 {
|
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);
|
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),
|
twitch.TwitchRes([]const twitch.User),
|
||||||
@ptrCast([*:0]const u8, request.items),
|
@ptrCast([*:0]const u8, request.items),
|
||||||
headers,
|
headers,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
switch (result) {
|
||||||
|
.ok => |*streamers| {
|
||||||
for (streamers.data) |streamer| {
|
for (streamers.data) |streamer| {
|
||||||
var stm = try db.prepare("INSERT OR REPLACE INTO STREAMER(idStreamer, loginStreamer, nameStreamer, imageUrlStreamer) VALUES(?, ?, ?, ?)");
|
var stm = try db.prepare("INSERT OR REPLACE INTO STREAMER(idStreamer, loginStreamer, nameStreamer, imageUrlStreamer) VALUES(?, ?, ?, ?)");
|
||||||
try stm.bind(1, sqlite.U8Array.text(streamer.id));
|
try stm.bind(1, sqlite.U8Array.text(streamer.id));
|
||||||
|
@ -460,4 +470,10 @@ fn insertOrReplaceStreamers(
|
||||||
}
|
}
|
||||||
|
|
||||||
streamers.deinit(allocator);
|
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,
|
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 {
|
pub const Client = struct {
|
||||||
ptr: *cURL.CURL,
|
ptr: *cURL.CURL,
|
||||||
allocator: mem.Allocator,
|
allocator: mem.Allocator,
|
||||||
|
@ -57,7 +66,7 @@ pub const Client = struct {
|
||||||
return error.CURLPerformFailed;
|
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);
|
var response_buffer = std.ArrayList(u8).init(self.allocator);
|
||||||
if (cURL.curl_easy_setopt(self.ptr, cURL.CURLOPT_URL, url) != cURL.CURLE_OK)
|
if (cURL.curl_easy_setopt(self.ptr, cURL.CURLOPT_URL, url) != cURL.CURLE_OK)
|
||||||
return error.CURLPerformFailed;
|
return error.CURLPerformFailed;
|
||||||
|
@ -92,17 +101,27 @@ pub const Client = struct {
|
||||||
if (cURL.curl_easy_perform(self.ptr) != cURL.CURLE_OK)
|
if (cURL.curl_easy_perform(self.ptr) != cURL.CURLE_OK)
|
||||||
return error.CURLPerformFailed;
|
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)
|
if (header_slist != null)
|
||||||
cURL.curl_slist_free_all(header_slist);
|
cURL.curl_slist_free_all(header_slist);
|
||||||
|
|
||||||
|
if (httpCode == 200) {
|
||||||
var content = response_buffer.items;
|
var content = response_buffer.items;
|
||||||
var stream = json.TokenStream.init(content);
|
var stream = json.TokenStream.init(content);
|
||||||
|
|
||||||
@setEvalBranchQuota(10_000);
|
@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();
|
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 {
|
pub fn postJSON(self: *@This(), url: []const u8, data: anytype, headers: ?std.StringHashMap([]const u8)) anyerror![]const u8 {
|
||||||
|
|
Loading…
Reference in New Issue