using Shared.DTO.VariablesEntorno; namespace Shared.Utils { public static class ObtenerVariablesEntorno { public static Dictionary GetVarEnviromentDict(string key) { string? variableEnv = Environment.GetEnvironmentVariable(key); if (string.IsNullOrWhiteSpace(variableEnv)) throw new ArgumentException($"La variable de entorno '{key}' no está definida."); var dictionary = new Dictionary(StringComparer.OrdinalIgnoreCase); foreach (var data in variableEnv.Split(';', StringSplitOptions.RemoveEmptyEntries)) { int div = data.IndexOf('='); if (div <= 0 || div >= data.Length - 1) continue; // Evita keys o values vacíos o sin '=' var keyValue = data[..div].Trim().ToUpperInvariant(); var value = data[(div + 1)..].Trim(); if (!string.IsNullOrEmpty(keyValue) && !string.IsNullOrEmpty(value)) dictionary[keyValue] = value; } return dictionary; } public static void AmbientarCredencialesDGA(string env) { var dictEnv = GetVarEnviromentDict(env); dictEnv.TryGetValue("RUTESVAL", out string? rutEsval); CredencialDGA.RutEsval = rutEsval ?? string.Empty; dictEnv.TryGetValue("RUTAV", out string? rutAv); CredencialDGA.RutAv = rutAv ?? string.Empty; dictEnv.TryGetValue("RUTUSUARIO", out string? rutUsuario); CredencialDGA.RutUsuario = rutUsuario ?? string.Empty; dictEnv.TryGetValue("PASSWORD", out string? password); CredencialDGA.Password = password ?? string.Empty; } public static void AmbientarConexionBd(string env) { BdConexion.StringConnection = Environment.GetEnvironmentVariable(env) ?? string.Empty; } public static void AmbientarApiUrlNexus(string env) { var dictEnv = GetVarEnviromentDict(env); dictEnv.TryGetValue("URL", out string? url); NexusApiUrl.ApiUrl = url ?? string.Empty; dictEnv.TryGetValue("APIKEY", out string? apiKey); NexusApiUrl.ApiKey = apiKey ?? string.Empty; dictEnv.TryGetValue("VERSION", out string? version); NexusApiUrl.Version = version ?? string.Empty; dictEnv.TryGetValue("DATASOURCE", out string? dataSource); NexusApiUrl.DataSource = dataSource ?? string.Empty; dictEnv.TryGetValue("RESOLUTION", out string? resolution); NexusApiUrl.Resolution = resolution ?? string.Empty; } public static void AmbientarUrlApiSubterranea(string env) { var dictEnv = GetVarEnviromentDict(env); dictEnv.TryGetValue("URL", out string? url); SubterraneaApiUrl.BaseUrl = url ?? string.Empty; dictEnv.TryGetValue("ENDPOINT", out string? endpoint); SubterraneaApiUrl.EndPoint = endpoint ?? string.Empty; } public static void ValidarVariablesEntorno() { if (string.IsNullOrWhiteSpace(CredencialDGA.RutEsval)) throw new Exception("La propiedad CredencialDGA.RutEsval está vacía o nula."); if (string.IsNullOrWhiteSpace(CredencialDGA.RutAv)) throw new Exception("La propiedad CredencialDGA.RutAv está vacía o nula."); if (string.IsNullOrWhiteSpace(CredencialDGA.RutUsuario)) throw new Exception("La propiedad CredencialDGA.RutUsuario está vacía o nula."); if (string.IsNullOrWhiteSpace(CredencialDGA.Password)) throw new Exception("La propiedad CredencialDGA.Password está vacía o nula."); if (string.IsNullOrWhiteSpace(NexusApiUrl.ApiUrl)) throw new Exception("La propiedad NexusApiUrl.ApiUrl está vacía o nula."); if (string.IsNullOrWhiteSpace(NexusApiUrl.ApiKey)) throw new Exception("La propiedad NexusApiUrl.ApiKey está vacía o nula."); if (string.IsNullOrWhiteSpace(NexusApiUrl.Version)) throw new Exception("La propiedad NexusApiUrl.Version está vacía o nula."); if (string.IsNullOrWhiteSpace(NexusApiUrl.DataSource)) throw new Exception("La propiedad NexusApiUrl.DataSource está vacía o nula."); if (string.IsNullOrWhiteSpace(NexusApiUrl.Resolution)) throw new Exception("La propiedad NexusApiUrl.Resolution está vacía o nula."); if (string.IsNullOrWhiteSpace(BdConexion.StringConnection)) throw new Exception("La propiedad BdConexion.StringConnection está vacía o nula."); if (string.IsNullOrWhiteSpace(SubterraneaApiUrl.BaseUrl)) throw new Exception("La propiedad SubterraneaApiUrl.BaseUrl está vacía o nula."); if (string.IsNullOrWhiteSpace(SubterraneaApiUrl.EndPoint)) throw new Exception("La propiedad SubterraneaApiUrl.EndPoint está vacía o nula."); } } }