Initial commit and getResponse http utility

This commit is contained in:
Alex Selimov 2026-05-25 07:01:11 -04:00
commit 660f367c2e
2 changed files with 79 additions and 0 deletions

3
README.md Normal file
View file

@ -0,0 +1,3 @@
vouchChain ⛓️
Get an expanded chain of vouched contributors to your code while still fending off AI slop factories.

76
src/http_utils.zig Normal file
View file

@ -0,0 +1,76 @@
const std = @import("std");
const HttpError = error{UnexpectedStatus};
pub fn getResponse(comptime T: type, allocator: std.mem.Allocator, url: []const u8) !std.json.Parsed(T) {
// Set up single threaded context
var threaded: std.Io.Threaded = .init_single_threaded;
const io: std.Io = threaded.io();
// Create a client
var client: std.http.Client = .{ .allocator = allocator, .io = io };
defer client.deinit();
//Get the endpoint
const endpoint = try std.Uri.parse(url);
var request = try client.request(.GET, endpoint, .{});
defer request.deinit();
try request.sendBodiless();
var response = try request.receiveHead(&.{});
var transfer_buffer: [64]u8 = undefined;
var decompress: std.http.Decompress = undefined;
var decompress_buffer: [std.compress.flate.max_window_len]u8 = undefined;
const response_reader = response.readerDecompressing(
transfer_buffer[0..],
&decompress,
decompress_buffer[0..],
);
var json_reader: std.json.Reader = .init(allocator, response_reader);
defer json_reader.deinit();
const parsed: std.json.Parsed(T) = try std.json.parseFromTokenSource(
T,
allocator,
&json_reader,
.{
.ignore_unknown_fields = true,
.allocate = .alloc_always,
},
);
if (response.head.status.class() != .success) {
return HttpError.UnexpectedStatus;
}
return parsed;
}
test "Test getQuoteResponse" {
const QuoteResponse = struct {
quote: struct {
author: struct {
bio: []u8,
description: []u8,
id: []u8,
link: []u8,
name: []u8,
slug: []u8,
},
content: []u8,
id: []u8,
tags: []struct {
id: []u8,
name: []u8,
},
},
};
const allocator = std.testing.allocator;
const url: []const u8 = "https://api.quotable.kurokeita.dev/api/quotes/random";
const parsed_quote = try getResponse(QuoteResponse, allocator, url[0..]);
defer parsed_quote.deinit();
std.debug.print("{s}", .{parsed_quote.value.quote.id});
}