Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Cnblogs.DashScope.Core/DashScopeClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cnblogs.DashScope.Core.Internals;
using System.Net.Http.Headers;
using Cnblogs.DashScope.Core.Internals;

namespace Cnblogs.DashScope.Core;

Expand Down Expand Up @@ -33,6 +34,8 @@ private static HttpClient GetConfiguredClient(string apiKey, TimeSpan? timeout)
BaseAddress = new Uri(DashScopeDefaults.DashScopeApiBaseAddress),
Timeout = timeout ?? TimeSpan.FromMinutes(2)
};

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
ClientPools.Add(GetCacheKey(), client);
}

Expand Down
18 changes: 17 additions & 1 deletion test/Cnblogs.DashScope.Sdk.UnitTests/DashScopeClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Net.Http.Headers;
using System.Reflection;
using Cnblogs.DashScope.Core;
using FluentAssertions;

Expand Down Expand Up @@ -59,6 +60,21 @@ public void DashScopeClient_Constructor_CacheableParams(
value.Should().Be(value2);
}

[Fact]
public void DashScopeClient_Constructor_WithApiKeyHeader()
{
// Arrange
const string apiKey = "key";
var client = new DashScopeClient(apiKey);

// Act
var value = HttpClientAccessor.GetValue(client) as HttpClient;

// Assert
value?.DefaultRequestHeaders.Authorization?.Should()
.BeEquivalentTo(new AuthenticationHeaderValue("Bearer", apiKey));
}

public static TheoryData<string, string, TimeSpan?, TimeSpan?> ParamsShouldNotCache
=> new()
{
Expand Down