public PowerBISettings()

in src/Common/Commands.Common/PowerBISettings.cs [29:111]


        public PowerBISettings(string settingsFilePath = null, PowerBIEnvironmentType targetEnvironmentType = PowerBIEnvironmentType.Public, bool refreshGlobalServiceConfig = false)
        {
            if (string.IsNullOrEmpty(settingsFilePath))
            {
                var executingDirectory = DirectoryUtility.GetExecutingDirectory();
                settingsFilePath = Path.Combine(executingDirectory, FileName);
            }

            if (!File.Exists(settingsFilePath))
            {
                throw new FileNotFoundException("Unable to find setting configuration", settingsFilePath);
            }

            PowerBIConfiguration configuration = null;
            using (var fileReader = File.OpenRead(settingsFilePath))
            {
                var serializer = new DataContractJsonSerializer(typeof(PowerBIConfiguration));
                using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(fileReader, Encoding.UTF8, XmlDictionaryReaderQuotas.Max, null))
                {
                    configuration = serializer.ReadObject(jsonReader) as PowerBIConfiguration;
                }
            }

            this.Settings = configuration.Settings;
            var targetEnvironment = configuration.Environments
                    .FirstOrDefault(e => Enum.TryParse(e.Name, out PowerBIEnvironmentType result) && result == targetEnvironmentType);
            var globalServiceEndpoint = (targetEnvironment != null && targetEnvironment.GlobalService != null) ? targetEnvironment.GlobalService : "https://api.powerbi.com";

            var cloudEnvironments = GetGlobalServiceConfig(globalServiceEndpoint, enforceRefresh: refreshGlobalServiceConfig).Result;
            if (cloudEnvironments != null)
            {
                // Ignore non-valid environments
                var environments = configuration.Environments
                    .Where(e => Enum.TryParse(e.Name, out PowerBIEnvironmentType result))
                    .Select(e =>
                        {
                            if (!string.IsNullOrEmpty(e.CloudName))
                            {
                                string cloudName = e.CloudName;
                                var cloudEnvironment = cloudEnvironments.Environments.FirstOrDefault(c => c.CloudName.Equals(cloudName, StringComparison.OrdinalIgnoreCase));
                                if (cloudEnvironment == null)
                                {
                                    throw new NotSupportedException($"Unable to find cloud name: {cloudName}");
                                }

                                var backendService = cloudEnvironment.Services.First(s => s.Name.Equals("powerbi-backend", StringComparison.OrdinalIgnoreCase));
                                var redirectApp = cloudEnvironment.Clients.First(s => s.Name.Equals(e.RedirectApp, StringComparison.OrdinalIgnoreCase));
                                return new PowerBIEnvironment()
                                {
                                    Name = (PowerBIEnvironmentType)Enum.Parse(typeof(PowerBIEnvironmentType), e.Name),
                                    AzureADAuthority = cloudEnvironment.Services.First(s => s.Name.Equals("aad", StringComparison.OrdinalIgnoreCase)).Endpoint,
                                    AzureADClientId = redirectApp.AppId,
                                    AzureADRedirectAddress = redirectApp.RedirectUri,
                                    AzureADResource = backendService.ResourceId,
                                    GlobalServiceEndpoint = backendService.Endpoint
                                };
                            }
                            else
                            {
                                // Debug environments
                                string cloudName = "GlobalCloud";
                                var cloudEnvironment = cloudEnvironments.Environments.FirstOrDefault(c => c.CloudName.Equals(cloudName, StringComparison.OrdinalIgnoreCase));
                                if (cloudEnvironment == null)
                                {
                                    throw new NotSupportedException($"Unable to find cloud name: {cloudName}");
                                }

                                var redirectApp = cloudEnvironment.Clients.First(s => s.Name.Equals(e.RedirectApp, StringComparison.OrdinalIgnoreCase));
                                return new PowerBIEnvironment()
                                {
                                    Name = (PowerBIEnvironmentType)Enum.Parse(typeof(PowerBIEnvironmentType), e.Name),
                                    AzureADAuthority = e.Authority,
                                    AzureADClientId = redirectApp.AppId,
                                    AzureADRedirectAddress = redirectApp.RedirectUri,
                                    AzureADResource = e.Resource,
                                    GlobalServiceEndpoint = e.GlobalService
                                };
                            }
                        })
                     .Cast<IPowerBIEnvironment>().ToDictionary(e => e.Name, e => e);
                this.Environments = environments;
            }
        }