TokenCredential GetSearchTokenCredential()

in src/WebJobs.Extensions.OpenAI.AzureAISearch/AzureAISearchProvider.cs [331:372]


    TokenCredential GetSearchTokenCredential() =>
        this.searchConnectionConfigSection.Exists()
            ? this.azureComponentFactory.CreateTokenCredential(this.searchConnectionConfigSection)
            : new DefaultAzureCredential();

    void SetConfigSectionProperties()
    {
        // Retrieve the configuration section
        this.searchConnectionConfigSection = this.configuration.GetSection(this.searchConnectionNamePrefix);

        // Extract values from the configuration section if it exists
        if (this.searchConnectionConfigSection.Exists())
        {
            this.endpoint = this.searchConnectionConfigSection[endpointSettingSuffix];
            this.apiKey = this.searchConnectionConfigSection[apiKeySettingSuffix];


            if (!string.IsNullOrEmpty(this.apiKey))
            {
                this.logger.LogInformation("Using key based authentication.");
            }

            if (!string.IsNullOrEmpty(this.endpoint))
            {
                this.logger.LogInformation("Using configured endpoint in the section - {this.searchConnectionNamePrefix}", this.searchConnectionNamePrefix);
                return;
            }
        }

        // Fallback to global configuration property
        this.endpoint = this.configuration.GetValue<string>(this.searchConnectionNamePrefix);
        if (!string.IsNullOrEmpty(this.endpoint))
        {
            this.logger.LogInformation("Using DefaultAzureCredential with fallback to connection name configuration property {property} as the endpoint setting", this.searchConnectionNamePrefix);
            return;
        }

        // Throw exception if no valid configuration is found
        string errorMessage = $"Configuration section or endpoint '{this.searchConnectionNamePrefix}' does not exist.";
        this.logger.LogError(errorMessage);
        throw new InvalidOperationException(errorMessage);
    }