From 664579623a7f55e8255beae9003dcc8c2e86ec96 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 23 Oct 2026 19:50:18 +0330 Subject: [PATCH] add Extension Method for Handling Attachment of ApiKey on AI Model Proxyfing ... --- Constants/XAiModelsConstants.cs | 1 + Extensions/XAiModelsExtensions.cs | 32 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Constants/XAiModelsConstants.cs b/Constants/XAiModelsConstants.cs index 45a9ab0..e2217d3 100644 --- a/Constants/XAiModelsConstants.cs +++ b/Constants/XAiModelsConstants.cs @@ -59,5 +59,6 @@ namespace xAiModels.Constants public const string DefaultXAiProjectDescriptionResourceID = "XAiPrjD"; // + public const string XOpenAINoKey = "no-key-required"; } } \ No newline at end of file diff --git a/Extensions/XAiModelsExtensions.cs b/Extensions/XAiModelsExtensions.cs index 8db7348..8219893 100644 --- a/Extensions/XAiModelsExtensions.cs +++ b/Extensions/XAiModelsExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Linq; +using System.Net.Http; using xAiModels.Models; +using xCommons.Constants; using xModels.Extensions; using xCommons.Extensions; using xAiModels.Constants; @@ -462,5 +464,35 @@ namespace xAiModels.Extensions // return result; } + + /// + /// Add Model Provider API Key ... + /// + /// + /// + public static void AddModelDescriptorApiKey( + this XAiModelDescriptor descriptor, + HttpRequestMessage request + ) + { + // + // Validate ... + if (!descriptor.IsValid() || request.IsNull()) + { + return; + } + + // + var apiKey = + descriptor.Provider != XAiModelProviderType.OpenAI + ? descriptor.ApiKey + : descriptor.ApiKey.IsNullOrEmpty() + ? XAiModelsConstants.XOpenAINoKey + : descriptor.ApiKey; + request.Headers.TryAddWithoutValidation( + name: XAuthorization.Header, + value: $"{XAuthorization.TokenIdentifier} {apiKey}" + ); + } } } \ No newline at end of file