using System.Text.Json; namespace ZB.MOM.WW.ScadaBridge.DelmiaNotifier; /// Reads and interprets the notifier's configuration and secret. Parse logic is string-based so it is unit-testable without touching disk. internal static class ConfigLoader { private const string ApiKeyEnvVar = "SCADABRIDGE_API_KEY"; /// Deserialize the appsettings.json text via the source-gen context. /// The raw JSON text of appsettings.json. /// The deserialized , or a default instance if deserialization yields null. public static NotifierConfig Load(string jsonText) => JsonSerializer.Deserialize(jsonText, NotifierJsonContext.Default.NotifierConfig) ?? new NotifierConfig(); /// Read and parse the appsettings.json sitting next to the executable. /// The deserialized . public static NotifierConfig LoadFromDefaultFile() { var path = Path.Combine(AppContext.BaseDirectory, "appsettings.json"); return Load(File.ReadAllText(path)); } /// Split a comma-separated base-URL list into trimmed, non-empty entries. /// The comma-separated base-URL list, or null/empty. /// The trimmed, non-empty base URLs; an empty array if none. public static string[] SplitBaseUrls(string? baseUrls) { if (string.IsNullOrWhiteSpace(baseUrls)) { return []; } return baseUrls.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); } /// Resolve the API key from SCADABRIDGE_API_KEY; null/whitespace → null. Env accessor is injected for testability. /// Accessor used to read an environment variable by name. /// The resolved API key, or null if unset/whitespace. public static string? ResolveApiKey(Func envGet) { var key = envGet(ApiKeyEnvVar); return string.IsNullOrWhiteSpace(key) ? null : key; } }