namespace Kiseki.Launcher; using System.Text.Json; public static class Web { public const int RESPONSE_FAILURE = -1; public const int RESPONSE_SUCCESS = 0; public const int RESPONSE_MAINTENANCE = 1; public static string CurrentUrl { get; private set; } = ""; public static bool IsInMaintenance { get; private set; } = false; public static readonly HttpClient HttpClient = new(); public static bool Initialize() { CurrentUrl = IsInMaintenance ? $"{Constants.MAINTENANCE_DOMAIN}.{Constants.BASE_URL}" : Constants.BASE_URL; // Synchronous block is intentional Task task = CheckHealth(); task.Wait(); int response = task.Result; if (response != RESPONSE_SUCCESS) { if (response == RESPONSE_MAINTENANCE) IsInMaintenance = true; return false; } return true; } public static string Url(string path) => $"https://{CurrentUrl}{path}"; public static async Task CheckHealth() { var response = await Helpers.Http.GetJson(Url("/api/health")); return response is null ? RESPONSE_FAILURE : response.Status; } public static bool LoadLicense(string license) { // The license is just headers required to access the website in a JSON document Dictionary headers; try { headers = JsonSerializer.Deserialize>(license)!; } catch { return false; } for (int i = 0; i < headers.Count; i++) { HttpClient.DefaultRequestHeaders.Add(headers.ElementAt(i).Key, headers.ElementAt(i).Value); } return true; } }