@@ -292,7 +292,7 @@ await _notificationService.DispatchNotification(
292292 CancellationToken cancellationToken )
293293 {
294294 var client = _httpClientFactory . CreateClient ( HttpClientName ) ;
295- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
295+ SetAuthorizationIfNeeded ( client , apiKey ) ;
296296
297297 var requestBody = BuildRequestBody ( chat , conversation , true ) ;
298298
@@ -411,7 +411,7 @@ await _notificationService.DispatchNotification(
411411 CancellationToken cancellationToken )
412412 {
413413 var client = _httpClientFactory . CreateClient ( HttpClientName ) ;
414- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
414+ SetAuthorizationIfNeeded ( client , apiKey ) ;
415415
416416 var requestBody = BuildRequestBody ( chat , conversation , false ) ;
417417
@@ -522,9 +522,7 @@ public virtual async Task<string[]> GetCurrentModels()
522522 ValidateApiKey ( ) ;
523523
524524 var client = _httpClientFactory . CreateClient ( HttpClientName ) ;
525- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue (
526- "Bearer" ,
527- GetApiKey ( ) ) ;
525+ SetAuthorizationIfNeeded ( client , GetApiKey ( ) ) ;
528526
529527 using var response = await client . GetAsync ( ModelsUrl ) ;
530528 response . EnsureSuccessStatusCode ( ) ;
@@ -575,6 +573,14 @@ private static bool HasFiles(Message message)
575573 return message . Files != null && message . Files . Count > 0 ;
576574 }
577575
576+ private static void SetAuthorizationIfNeeded ( HttpClient client , string apiKey )
577+ {
578+ if ( ! string . IsNullOrEmpty ( apiKey ) )
579+ {
580+ client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
581+ }
582+ }
583+
578584 private async Task ProcessStreamingChatAsync (
579585 Chat chat ,
580586 List < ChatMessage > conversation ,
@@ -585,7 +591,7 @@ private async Task ProcessStreamingChatAsync(
585591 CancellationToken cancellationToken )
586592 {
587593 var client = _httpClientFactory . CreateClient ( HttpClientName ) ;
588- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
594+ SetAuthorizationIfNeeded ( client , apiKey ) ;
589595
590596 var requestBody = BuildRequestBody ( chat , conversation , true ) ;
591597
@@ -670,7 +676,7 @@ private async Task ProcessNonStreamingChatAsync(
670676 CancellationToken cancellationToken )
671677 {
672678 var client = _httpClientFactory . CreateClient ( HttpClientName ) ;
673- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , apiKey ) ;
679+ SetAuthorizationIfNeeded ( client , apiKey ) ;
674680
675681 var requestBody = BuildRequestBody ( chat , conversation , false ) ;
676682
0 commit comments