A SpaceTraders Agent

create qol function for http requests

altagos.dev d1ae091c e313b3f7

verified
+56 -8
+56 -8
src/st/http.zig
··· 160 160 auth: AuthType = .none, 161 161 body: Body = .empty, 162 162 163 + free_body_after_sending: bool = false, 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 + pub fn get( 235 + cl: *Client, 236 + comptime T: type, 237 + comptime path: []const u8, 238 + args: anytype, 239 + auth: AuthType, 240 + ) !RawResponse(T) { 241 + return cl.request(T, path, args, .{ .auth = auth }); 242 + } 243 + 244 + pub fn post( 245 + cl: *Client, 246 + comptime T: type, 247 + comptime path: []const u8, 248 + args: anytype, 249 + body: anytype, 250 + auth: AuthType, 251 + ) !RawResponse(T) { 252 + const buffer = try json.Stringify.valueAlloc(cl.allocator, body, .{}); 253 + log.debug("json body: {s}", .{buffer}); 254 + return cl.request(T, path, args, .{ 255 + .method = .POST, 256 + .auth = auth, 257 + .body = .{ .buffer = buffer }, 258 + .free_body_after_sending = true, 259 + }); 260 + } 261 + 262 + pub fn patch( 263 + cl: *Client, 264 + comptime T: type, 265 + comptime path: []const u8, 266 + args: anytype, 267 + body: anytype, 268 + auth: AuthType, 269 + ) !RawResponse(T) { 270 + const buffer = try json.Stringify.valueAlloc(cl.allocator, body, .{}); 271 + log.debug("json body: {s}", .{buffer}); 272 + return cl.request(T, path, args, .{ 273 + .method = .PATCH, 274 + .auth = auth, 275 + .body = .{ .buffer = buffer }, 276 + .free_body_after_sending = true, 277 + }); 278 + } 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 - .user_agent = .{ .override = "All your codebases are belong to us" }, 324 + .user_agent = .{ .override = "SPACE/0.1" }, 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 - .buffer => |body| try req.sendBodyComplete(body), 336 + .buffer => |body| { 337 + try req.sendBodyComplete(body); 338 + if (opts.free_body_after_sending) client.allocator.free(body); 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 - if (std.mem.eql(u8, response.head.reason, "OK")) { 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 - const body_reader = response.readerDecompressing(&transfer_buffer, &decompress, &decompress_buffer); 344 - const body = body_reader.allocRemaining(client.allocator, .unlimited) catch return error.InvalidResponse; 345 - defer client.allocator.free(body); 346 - 347 - log.err("Error parsing response: {} - Body:\n{s}", .{ err, body }); 395 + log.err("Error parsing response: {}", .{err}); 348 396 349 397 return RequestError.InvalidResponse; 350 398 };