tangled
alpha
login
or
join now
altagos.dev
/
space
0
fork
atom
A SpaceTraders Agent
0
fork
atom
overview
issues
pulls
pipelines
create qol function for http requests
altagos.dev
3 months ago
d1ae091c
e313b3f7
verified
This commit was signed with the committer's
known signature
.
altagos.dev
SSH Key Fingerprint:
SHA256:UbTjEcCZlc6GzQWLCuDK3D//HESWD2xFPkzue9XMras=
+56
-8
1 changed file
expand all
collapse all
unified
split
src
st
http.zig
+56
-8
src/st/http.zig
···
160
160
auth: AuthType = .none,
161
161
body: Body = .empty,
162
162
163
163
+
free_body_after_sending: bool = false,
164
164
+
163
165
pub const Body = union(enum) {
164
166
empty: void,
165
167
buffer: []u8,
···
229
231
client.http.deinit();
230
232
}
231
233
234
234
+
pub fn get(
235
235
+
cl: *Client,
236
236
+
comptime T: type,
237
237
+
comptime path: []const u8,
238
238
+
args: anytype,
239
239
+
auth: AuthType,
240
240
+
) !RawResponse(T) {
241
241
+
return cl.request(T, path, args, .{ .auth = auth });
242
242
+
}
243
243
+
244
244
+
pub fn post(
245
245
+
cl: *Client,
246
246
+
comptime T: type,
247
247
+
comptime path: []const u8,
248
248
+
args: anytype,
249
249
+
body: anytype,
250
250
+
auth: AuthType,
251
251
+
) !RawResponse(T) {
252
252
+
const buffer = try json.Stringify.valueAlloc(cl.allocator, body, .{});
253
253
+
log.debug("json body: {s}", .{buffer});
254
254
+
return cl.request(T, path, args, .{
255
255
+
.method = .POST,
256
256
+
.auth = auth,
257
257
+
.body = .{ .buffer = buffer },
258
258
+
.free_body_after_sending = true,
259
259
+
});
260
260
+
}
261
261
+
262
262
+
pub fn patch(
263
263
+
cl: *Client,
264
264
+
comptime T: type,
265
265
+
comptime path: []const u8,
266
266
+
args: anytype,
267
267
+
body: anytype,
268
268
+
auth: AuthType,
269
269
+
) !RawResponse(T) {
270
270
+
const buffer = try json.Stringify.valueAlloc(cl.allocator, body, .{});
271
271
+
log.debug("json body: {s}", .{buffer});
272
272
+
return cl.request(T, path, args, .{
273
273
+
.method = .PATCH,
274
274
+
.auth = auth,
275
275
+
.body = .{ .buffer = buffer },
276
276
+
.free_body_after_sending = true,
277
277
+
});
278
278
+
}
279
279
+
232
280
pub fn request(
233
281
client: *Client,
234
282
comptime T: type,
···
273
321
var req = try client.http.request(opts.method, uri, .{
274
322
.headers = .{
275
323
.authorization = opts.authorization(client),
276
276
-
.user_agent = .{ .override = "All your codebases are belong to us" },
324
324
+
.user_agent = .{ .override = "SPACE/0.1" },
325
325
+
.content_type = .{ .override = "application/json" },
277
326
},
278
327
});
279
328
defer req.deinit();
···
284
333
285
334
switch (opts.body) {
286
335
.empty => try req.sendBodiless(),
287
287
-
.buffer => |body| try req.sendBodyComplete(body),
336
336
+
.buffer => |body| {
337
337
+
try req.sendBodyComplete(body);
338
338
+
if (opts.free_body_after_sending) client.allocator.free(body);
339
339
+
},
288
340
}
289
341
290
342
var redirect_buffer: [1024]u8 = undefined;
291
343
292
344
var response = try req.receiveHead(&redirect_buffer);
293
345
const colour = blk: {
294
294
-
if (std.mem.eql(u8, response.head.reason, "OK")) {
346
346
+
if (@intFromEnum(response.head.status) >= 200 and @intFromEnum(response.head.status) < 300) {
295
347
break :blk "\x1b[92m";
296
348
} else {
297
349
break :blk "\x1b[1m\x1b[91m";
···
340
392
const result = json.parseFromTokenSource(T, client.allocator, &json_reader, .{
341
393
.ignore_unknown_fields = true,
342
394
}) catch |err| {
343
343
-
const body_reader = response.readerDecompressing(&transfer_buffer, &decompress, &decompress_buffer);
344
344
-
const body = body_reader.allocRemaining(client.allocator, .unlimited) catch return error.InvalidResponse;
345
345
-
defer client.allocator.free(body);
346
346
-
347
347
-
log.err("Error parsing response: {} - Body:\n{s}", .{ err, body });
395
395
+
log.err("Error parsing response: {}", .{err});
348
396
349
397
return RequestError.InvalidResponse;
350
398
};