A SpaceTraders Agent

separate api calls from models

altagos.dev 83aec082 84a9d7e7

verified
+75 -64
+2 -7
src/main.zig
··· 5 5 const meta = @import("meta"); 6 6 7 7 const st = @import("st"); 8 - const m = st.models; 8 + const api = st.api; 9 9 const Client = st.http.Client; 10 - const RawResponse = st.http.RawResponse; 11 - const Response = st.http.Response; 12 - 13 - const Info = st.models.Info; 14 - const agents = st.models.agents; 15 10 16 11 const agent = @import("agent"); 17 12 const Config = agent.config.Config; ··· 31 26 // _ = io; 32 27 // _ = client; 33 28 34 - var fleet_f = try st.fleet.listShips(client, .{}); 29 + var fleet_f = try api.fleet.listShips(client, .{}); 35 30 defer _ = fleet_f.cancel(io) catch {}; 36 31 37 32 const fleet = try fleet_f.await(io);
+72
src/st/api.zig
··· 1 + pub const fleet = @import("fleet.zig"); 2 + 3 + const st = @import("root.zig"); 4 + const http = st.http; 5 + const Response = st.http.Response; 6 + 7 + const models = st.models; 8 + const Wrapper = models.Wrapper; 9 + const Query = models.Query; 10 + 11 + const Client = st.Client; 12 + 13 + pub fn account(client: *Client) !Response(models.accounts.AccountWrapper) { 14 + return client.request( 15 + Wrapper(models.accounts.AccountWrapper), 16 + "/my/account", 17 + .{}, 18 + .{ .auth = .agent }, 19 + ); 20 + } 21 + 22 + pub fn info(client: *Client) !http.RawResponse(models.Info) { 23 + return client.request( 24 + models.Info, 25 + "/", 26 + .{}, 27 + .{ .auth = .none }, 28 + ); 29 + } 30 + 31 + pub const agent = struct { 32 + pub fn register( 33 + client: *Client, 34 + symbol: []const u8, 35 + faction: models.factions.Symbol, 36 + ) !Response(models.agents.Register) { 37 + return client.post( 38 + Wrapper(models.agents.Register), 39 + "/register", 40 + .{}, 41 + .{ .symbol = symbol, .faction = faction }, 42 + .account, 43 + ); 44 + } 45 + 46 + pub fn list(client: *Client, opts: Query) !Response([]models.agents.Agent) { 47 + return client.get( 48 + Wrapper([]models.agents.Agent), 49 + "/agents{f}", 50 + .{opts}, 51 + .agent, 52 + ); 53 + } 54 + 55 + pub fn get(client: *Client, symbol: []const u8) !Response(models.agents.Agent) { 56 + return client.get( 57 + Wrapper(models.agents.Agent), 58 + "/agents/{s}", 59 + .{symbol}, 60 + .agent, 61 + ); 62 + } 63 + 64 + pub fn own(client: *Client) !Response(models.agents.Agent) { 65 + return client.get( 66 + Wrapper(models.agents.Agent), 67 + "/my/agent", 68 + .{}, 69 + .agent, 70 + ); 71 + } 72 + };
-9
src/st/models/Info.zig
··· 54 54 name: []const u8, 55 55 url: []const u8, 56 56 }; 57 - 58 - pub fn get(client: *Client) !RawResponse(Info) { 59 - return client.request( 60 - Info, 61 - "/", 62 - .{}, 63 - .{ .auth = .none }, 64 - ); 65 - }
-9
src/st/models/accounts.zig
··· 13 13 token: ?[]const u8 = null, 14 14 createdAt: []const u8, 15 15 }; 16 - 17 - pub fn get(client: *Client) !Response(AccountWrapper) { 18 - return client.request( 19 - Wrapper(AccountWrapper), 20 - "/my/account", 21 - .{}, 22 - .{ .auth = .agent }, 23 - ); 24 - }
-37
src/st/models/agents.zig
··· 24 24 contract: Contract, 25 25 ships: []Ship, 26 26 }; 27 - 28 - pub fn list(client: *Client, opts: Query) !Response([]Agent) { 29 - return client.get( 30 - Wrapper([]Agent), 31 - "/agents{f}", 32 - .{opts}, 33 - .agent, 34 - ); 35 - } 36 - 37 - pub fn get(client: *Client, symbol: []const u8) !Response(Agent) { 38 - return client.get( 39 - Wrapper(Agent), 40 - "/agents/{s}", 41 - .{symbol}, 42 - .agent, 43 - ); 44 - } 45 - 46 - pub fn own(client: *Client) !Response(Agent) { 47 - return client.get( 48 - Wrapper(Agent), 49 - "/my/agent", 50 - .{}, 51 - .agent, 52 - ); 53 - } 54 - 55 - pub fn register(client: *Client, symbol: []const u8, faction: FactionSymbol, email: ?[]const u8) !Response(Register) { 56 - return client.post( 57 - Wrapper(Register), 58 - "/register", 59 - .{}, 60 - .{ .symbol = symbol, .faction = faction, .email = email }, 61 - .account, 62 - ); 63 - }
+1 -2
src/st/root.zig
··· 1 + pub const api = @import("api.zig"); 1 2 pub const http = @import("http.zig"); 2 3 pub const models = @import("models.zig"); 3 - 4 - pub const fleet = @import("fleet.zig"); 5 4 6 5 test { 7 6 const testing = @import("std").testing;