-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapi_cli.zig
More file actions
194 lines (163 loc) · 6.91 KB
/
api_cli.zig
File metadata and controls
194 lines (163 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const std = @import("std");
const cli = @import("cli.zig");
pub const ExecuteError = error{
InvalidMethod,
InvalidTarget,
};
pub const Result = struct {
status: std.http.Status,
body: []u8,
pub fn deinit(self: *Result, allocator: std.mem.Allocator) void {
allocator.free(self.body);
self.* = undefined;
}
};
pub fn run(allocator: std.mem.Allocator, opts: cli.ApiOptions) !void {
var result = try execute(allocator, opts);
defer result.deinit(allocator);
const formatted = if (opts.pretty)
try prettyBody(allocator, result.body)
else
try allocator.dupe(u8, result.body);
defer allocator.free(formatted);
if (formatted.len > 0) {
try writeAll(std.fs.File.stdout(), formatted);
if (formatted[formatted.len - 1] != '\n') {
try writeAll(std.fs.File.stdout(), "\n");
}
}
const code = @intFromEnum(result.status);
if (code < 200 or code >= 300) {
var buf: [64]u8 = undefined;
const line = try std.fmt.bufPrint(&buf, "HTTP {d}\n", .{code});
try writeAll(std.fs.File.stderr(), line);
return error.RequestFailed;
}
}
pub fn execute(allocator: std.mem.Allocator, opts: cli.ApiOptions) !Result {
const method = parseMethod(opts.method) orelse return ExecuteError.InvalidMethod;
const target = try normalizeTargetAlloc(allocator, opts.target);
defer allocator.free(target);
const url = try std.fmt.allocPrint(allocator, "http://{s}:{d}{s}", .{ opts.host, opts.port, target });
defer allocator.free(url);
const request_body = try loadBodyAlloc(allocator, opts);
defer if (request_body.owned) allocator.free(request_body.bytes);
var auth_header: ?[]u8 = null;
defer if (auth_header) |value| allocator.free(value);
var header_storage: [2]std.http.Header = undefined;
var header_count: usize = 0;
if (request_body.bytes.len > 0) {
header_storage[header_count] = .{ .name = "Content-Type", .value = opts.content_type };
header_count += 1;
}
if (opts.token) |token| {
auth_header = try std.fmt.allocPrint(allocator, "Bearer {s}", .{token});
header_storage[header_count] = .{ .name = "Authorization", .value = auth_header.? };
header_count += 1;
}
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
var response_body: std.io.Writer.Allocating = .init(allocator);
defer response_body.deinit();
const result = try client.fetch(.{
.location = .{ .url = url },
.method = method,
.payload = payloadForFetch(method, request_body.bytes),
.response_writer = &response_body.writer,
.extra_headers = header_storage[0..header_count],
});
return .{
.status = result.status,
.body = try response_body.toOwnedSlice(),
};
}
fn writeAll(file: std.fs.File, bytes: []const u8) !void {
var buf: [4096]u8 = undefined;
var writer = file.writer(&buf);
try writer.interface.writeAll(bytes);
try writer.interface.flush();
}
fn parseMethod(raw: []const u8) ?std.http.Method {
if (std.ascii.eqlIgnoreCase(raw, "GET")) return .GET;
if (std.ascii.eqlIgnoreCase(raw, "POST")) return .POST;
if (std.ascii.eqlIgnoreCase(raw, "PUT")) return .PUT;
if (std.ascii.eqlIgnoreCase(raw, "DELETE")) return .DELETE;
if (std.ascii.eqlIgnoreCase(raw, "PATCH")) return .PATCH;
if (std.ascii.eqlIgnoreCase(raw, "HEAD")) return .HEAD;
if (std.ascii.eqlIgnoreCase(raw, "OPTIONS")) return .OPTIONS;
return null;
}
fn normalizeTargetAlloc(allocator: std.mem.Allocator, raw: []const u8) ![]u8 {
if (raw.len == 0) return ExecuteError.InvalidTarget;
if (std.mem.startsWith(u8, raw, "http://") or std.mem.startsWith(u8, raw, "https://")) {
return ExecuteError.InvalidTarget;
}
if (raw[0] == '/') return allocator.dupe(u8, raw);
if (std.mem.startsWith(u8, raw, "api/")) return std.fmt.allocPrint(allocator, "/{s}", .{raw});
if (std.mem.eql(u8, raw, "health")) return allocator.dupe(u8, "/health");
return std.fmt.allocPrint(allocator, "/api/{s}", .{raw});
}
const LoadedBody = struct {
bytes: []const u8,
owned: bool = false,
};
fn loadBodyAlloc(allocator: std.mem.Allocator, opts: cli.ApiOptions) !LoadedBody {
if (opts.body_file) |path| {
if (std.mem.eql(u8, path, "-")) {
const bytes = try std.fs.File.stdin().readToEndAlloc(allocator, 8 * 1024 * 1024);
return .{ .bytes = bytes, .owned = true };
}
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
const bytes = try file.readToEndAlloc(allocator, 8 * 1024 * 1024);
return .{ .bytes = bytes, .owned = true };
}
if (opts.body) |body| return .{ .bytes = body, .owned = false };
return .{ .bytes = "", .owned = false };
}
fn payloadForFetch(method: std.http.Method, body: []const u8) ?[]const u8 {
if (body.len > 0) return body;
if (method.requestHasBody()) return body;
return null;
}
fn prettyBody(allocator: std.mem.Allocator, body: []const u8) ![]u8 {
if (body.len == 0) return allocator.dupe(u8, body);
const parsed = std.json.parseFromSlice(std.json.Value, allocator, body, .{
.allocate = .alloc_always,
.ignore_unknown_fields = true,
}) catch return allocator.dupe(u8, body);
defer parsed.deinit();
return std.json.Stringify.valueAlloc(allocator, parsed.value, .{
.whitespace = .indent_2,
});
}
test "normalizeTargetAlloc keeps explicit API path" {
const value = try normalizeTargetAlloc(std.testing.allocator, "/api/status");
defer std.testing.allocator.free(value);
try std.testing.expectEqualStrings("/api/status", value);
}
test "normalizeTargetAlloc prefixes api namespace" {
const value = try normalizeTargetAlloc(std.testing.allocator, "instances/nullclaw/demo");
defer std.testing.allocator.free(value);
try std.testing.expectEqualStrings("/api/instances/nullclaw/demo", value);
}
test "normalizeTargetAlloc supports health shorthand" {
const value = try normalizeTargetAlloc(std.testing.allocator, "health");
defer std.testing.allocator.free(value);
try std.testing.expectEqualStrings("/health", value);
}
test "parseMethod accepts common verbs case-insensitively" {
try std.testing.expectEqual(std.http.Method.DELETE, parseMethod("delete").?);
try std.testing.expectEqual(std.http.Method.PATCH, parseMethod("PATCH").?);
try std.testing.expect(parseMethod("TRACE") == null);
}
test "prettyBody indents JSON output" {
const value = try prettyBody(std.testing.allocator, "{\"ok\":true}");
defer std.testing.allocator.free(value);
try std.testing.expect(std.mem.indexOf(u8, value, "\n") != null);
try std.testing.expect(std.mem.indexOf(u8, value, " \"ok\"") != null);
}
test "payloadForFetch keeps empty body for POST" {
try std.testing.expect(payloadForFetch(.POST, "") != null);
try std.testing.expect(payloadForFetch(.GET, "") == null);
}