summaryrefslogtreecommitdiff
path: root/src/marlin.agents.shell/MarlinApiClient.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/marlin.agents.shell/MarlinApiClient.cs')
-rw-r--r--src/marlin.agents.shell/MarlinApiClient.cs38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/marlin.agents.shell/MarlinApiClient.cs b/src/marlin.agents.shell/MarlinApiClient.cs
new file mode 100644
index 0000000..aa4f013
--- /dev/null
+++ b/src/marlin.agents.shell/MarlinApiClient.cs
@@ -0,0 +1,38 @@
+namespace marlin.agents.shell;
+
+using System.Net.Http.Json;
+using core.models;
+
+public class MarlinApiClient
+{
+ private readonly HttpClient _http;
+
+ public MarlinApiClient(string baseUrl, Guid agentId, string agentToken)
+ {
+ _http = new HttpClient { BaseAddress = new Uri(baseUrl) };
+ _http.DefaultRequestHeaders.Add("Agent", $"{agentId} {agentToken}");
+ }
+
+ public static MarlinApiClient FromEnvironment()
+ {
+ var baseUrl = RequireEnv("MARLIN_API_BASE_URL");
+ var agentId = RequireEnv("MARLIN_AGENT_ID");
+ var agentToken = RequireEnv("MARLIN_AGENT_TOKEN");
+
+ return new MarlinApiClient(baseUrl, Guid.Parse(agentId), agentToken);
+ }
+
+ private static string RequireEnv(string name) =>
+ Environment.GetEnvironmentVariable(name)
+ ?? throw new InvalidOperationException($"Missing required environment variable: {name}");
+
+ public async Task<AgentConfiguration> Handshake(AgentStatus status, CancellationToken ct = default)
+ {
+ var response = await _http.PostAsJsonAsync("api/dispatch/handshake", status, ct);
+ response.EnsureSuccessStatusCode();
+ return (await response.Content.ReadFromJsonAsync<AgentConfiguration>(ct))!;
+ }
+
+ public async Task<DispatchJob?> QueueNextJob(CancellationToken ct = default)
+ => await _http.GetFromJsonAsync<DispatchJob?>("api/dispatch/queue", ct);
+}