using System.Net.Http.Json; using System.Text.Json; using Moonlight.Api.Http.Services.ContainerHelper; using Moonlight.Api.Http.Services.ContainerHelper.Requests; using Moonlight.Api.Http.Services.ContainerHelper.Events; namespace Moonlight.Api.Services; public class ContainerHelperService { private readonly IHttpClientFactory HttpClientFactory; public ContainerHelperService(IHttpClientFactory httpClientFactory) { HttpClientFactory = httpClientFactory; } public async Task CheckConnectionAsync() { var client = HttpClientFactory.CreateClient("ContainerHelper"); try { var response = await client.GetAsync("api/ping"); response.EnsureSuccessStatusCode(); return true; } catch (Exception) { return false; } } public async IAsyncEnumerable RebuildAsync(bool noBuildCache) { var client = HttpClientFactory.CreateClient("ContainerHelper"); var request = new HttpRequestMessage(HttpMethod.Post, "api/rebuild"); request.Content = JsonContent.Create( new RequestRebuildDto(noBuildCache), null, SerializationContext.Default.Options ); var response = await client.SendAsync( request, HttpCompletionOption.ResponseHeadersRead ); if (!response.IsSuccessStatusCode) { var responseText = await response.Content.ReadAsStringAsync(); yield return new RebuildEventDto() { Type = RebuildEventType.Failed, Data = responseText }; yield break; } await using var responseStream = await response.Content.ReadAsStreamAsync(); using var streamReader = new StreamReader(responseStream); do { var line = await streamReader.ReadLineAsync(); if (line == null) break; if (string.IsNullOrWhiteSpace(line)) continue; var data = line.Trim("data: "); var deserializedData = JsonSerializer.Deserialize(data, SerializationContext.Default.Options); yield return deserializedData; // Exit if service will go down for a clean exit if (deserializedData is { Type: RebuildEventType.Step, Data: "ServiceDown" }) yield break; } while (true); yield return new RebuildEventDto() { Type = RebuildEventType.Succeeded, Data = string.Empty }; } public async Task SetVersionAsync(string version) { var client = HttpClientFactory.CreateClient("ContainerHelper"); var response = await client.PostAsJsonAsync( "api/configuration/version", new SetVersionDto(version), SerializationContext.Default.Options ); if (response.IsSuccessStatusCode) return; var problemDetails = await response.Content.ReadFromJsonAsync(SerializationContext.Default.Options); if (problemDetails == null) throw new HttpRequestException($"Failed to set version: {response.ReasonPhrase}"); throw new HttpRequestException($"Failed to set version: {problemDetails.Detail ?? problemDetails.Title}"); } }