Files
IT-Nexus/agent-cs/Services/ApiService.cs
Simon Grüssing e2521f405d Fix: Agent-Rollout downloadete immer globale AGENT_VERSION statt Gruppen-Ziel (v2.5.0)
- downloadSetup() nutzte process.env.AGENT_VERSION statt der Patch-Gruppen-
  Zielversion → Staged Rollout (Test/Pilot/Produktion) hatte nie funktioniert,
  jeder Agent bekam beim Auto-Update immer dieselbe globale Version
- Fix: downloadSetup() löst jetzt per ?hostname=X die Gruppen-Zielversion auf,
  exakt wie checkin() es bereits tat
- ApiService.DownloadSetupAsync() schickt jetzt den Hostnamen mit
- Version: 2.4.0 → 2.5.0

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-19 09:35:01 +02:00

90 lines
3.3 KiB
C#

using ITNexusAgent.Models;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
namespace ITNexusAgent.Services;
public class ApiService(string serverUrl, string agentKey)
{
private readonly HttpClient _http = new() { Timeout = TimeSpan.FromSeconds(30) };
private readonly string _baseUrl = serverUrl.TrimEnd('/');
private readonly string _agentKey = agentKey;
private HttpRequestMessage BuildRequest(HttpMethod method, string path, object? body = null)
{
var req = new HttpRequestMessage(method, $"{_baseUrl}{path}");
req.Headers.Add("X-Agent-Key", _agentKey);
if (body != null)
{
var json = JsonConvert.SerializeObject(body);
req.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
return req;
}
public async Task<CheckinResponse> CheckinAsync(CheckinPayload payload)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/checkin", payload);
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
var body = await resp.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<CheckinResponse>(body) ?? new CheckinResponse();
}
public async Task<List<Announcement>> PollAnnouncementsAsync(string hostname)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/announcements-poll",
new { hostname });
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
var body = await resp.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeAnonymousType(body, new { announcements = new List<Announcement>() });
return result?.announcements ?? [];
}
public async Task AckAnnouncementAsync(int id, string hostname)
{
var req = BuildRequest(HttpMethod.Post, $"/api/announcements/{id}/ack-agent",
new { hostname });
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
}
public async Task ReportCommandResultAsync(CommandResult result)
{
var req = BuildRequest(HttpMethod.Post, "/api/patch/commands/result", result);
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
}
public async Task<byte[]> DownloadAgentAsync()
{
var req = BuildRequest(HttpMethod.Get, "/api/monitoring/agent-script");
var resp = await _http.SendAsync(req);
return await resp.Content.ReadAsByteArrayAsync();
}
public async Task<byte[]> DownloadSetupAsync(string hostname)
{
var req = BuildRequest(HttpMethod.Get, $"/api/monitoring/agent-setup?hostname={Uri.EscapeDataString(hostname)}");
var resp = await _http.SendAsync(req);
resp.EnsureSuccessStatusCode();
return await resp.Content.ReadAsByteArrayAsync();
}
public async Task SendMessageAsync(string hostname, string message)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/message",
new { hostname, message });
await _http.SendAsync(req);
}
public async Task SendRebootRequestAsync(string hostname)
{
var req = BuildRequest(HttpMethod.Post, "/api/monitoring/reboot-request",
new { hostname });
await _http.SendAsync(req);
}
}