Integracion_DGA/SHARED/Utils/ObtenerVariablesEntorno.cs

84 lines
3.1 KiB
C#
Raw Normal View History

2025-07-01 09:59:59 -04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared.DTO.VariablesEntorno;
namespace Shared.Utils
{
public static class ObtenerVariablesEntorno
{
public static Dictionary<string, string> 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<string, string>(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("RUTEMPRESA", out string? rutEmpresa);
CredencialDGA.RutEmpresa = rutEmpresa ?? 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;
}
}
}