Skip to content

Commit 47a42d5

Browse files
committed
feat: support set workspace when injecting service or creating client
1 parent dd5c316 commit 47a42d5

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

src/Cnblogs.DashScope.AspNetCore/ServiceCollectionInjector.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ public static IHttpClientBuilder AddDashScopeClient(this IServiceCollection serv
3838
var apiKey = section["apiKey"]
3939
?? throw new InvalidOperationException("There is no apiKey provided in given section");
4040
var baseAddress = section["baseAddress"];
41-
return string.IsNullOrEmpty(baseAddress)
42-
? services.AddDashScopeClient(apiKey)
43-
: services.AddDashScopeClient(apiKey, baseAddress);
41+
var workspaceId = section["workspaceId"];
42+
return services.AddDashScopeClient(apiKey, baseAddress, workspaceId);
4443
}
4544

4645
/// <summary>
@@ -49,16 +48,24 @@ public static IHttpClientBuilder AddDashScopeClient(this IServiceCollection serv
4948
/// <param name="services">The service collection to add service to.</param>
5049
/// <param name="apiKey">The DashScope api key.</param>
5150
/// <param name="baseAddress">The DashScope api base address, you may change this value if you are using proxy.</param>
51+
/// <param name="workspaceId">Default workspace id to use.</param>
5252
/// <returns></returns>
5353
public static IHttpClientBuilder AddDashScopeClient(
5454
this IServiceCollection services,
5555
string apiKey,
56-
string baseAddress = "https://dashscope.aliyuncs.com/api/v1/")
56+
string? baseAddress = null,
57+
string? workspaceId = null)
5758
{
59+
baseAddress ??= "https://dashscope.aliyuncs.com/api/v1/";
5860
return services.AddHttpClient<IDashScopeClient, DashScopeClientCore>(
5961
h =>
6062
{
6163
h.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
64+
if (string.IsNullOrWhiteSpace(workspaceId) == false)
65+
{
66+
h.DefaultRequestHeaders.Add("X-DashScope-WorkSpace", workspaceId);
67+
}
68+
6269
h.BaseAddress = new Uri(baseAddress);
6370
});
6471
}

src/Cnblogs.DashScope.Core/ApplicationRagOptions.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text.Json;
2-
3-
namespace Cnblogs.DashScope.Core;
1+
namespace Cnblogs.DashScope.Core;
42

53
/// <summary>
64
/// Options for RAG application.

src/Cnblogs.DashScope.Core/DashScopeClient.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,43 @@ public class DashScopeClient : DashScopeClientCore
1515
/// </summary>
1616
/// <param name="apiKey">The DashScope api key.</param>
1717
/// <param name="timeout">The timeout for internal http client, defaults to 2 minute.</param>
18+
/// <param name="baseAddress">The base address for dashscope api call.</param>
19+
/// <param name="workspaceId">The workspace id.</param>
1820
/// <remarks>
19-
/// The underlying httpclient is cached by apiKey and timeout.
20-
/// Client created with same apiKey and timeout value will share same underlying <see cref="HttpClient"/> instance.
21+
/// The underlying httpclient is cached by constructor parameter list.
22+
/// Client created with same parameter value will share same underlying <see cref="HttpClient"/> instance.
2123
/// </remarks>
22-
public DashScopeClient(string apiKey, TimeSpan? timeout = null)
23-
: base(GetConfiguredClient(apiKey, timeout))
24+
public DashScopeClient(
25+
string apiKey,
26+
TimeSpan? timeout = null,
27+
string? baseAddress = null,
28+
string? workspaceId = null)
29+
: base(GetConfiguredClient(apiKey, timeout, baseAddress, workspaceId))
2430
{
2531
}
2632

27-
private static HttpClient GetConfiguredClient(string apiKey, TimeSpan? timeout)
33+
private static HttpClient GetConfiguredClient(
34+
string apiKey,
35+
TimeSpan? timeout = null,
36+
string? baseAddress = null,
37+
string? workspaceId = null)
2838
{
2939
var client = ClientPools.GetValueOrDefault(GetCacheKey());
3040
if (client is null)
3141
{
3242
client = new HttpClient
3343
{
34-
BaseAddress = new Uri(DashScopeDefaults.DashScopeApiBaseAddress),
44+
BaseAddress = new Uri(baseAddress ?? DashScopeDefaults.DashScopeApiBaseAddress),
3545
Timeout = timeout ?? TimeSpan.FromMinutes(2)
3646
};
3747

3848
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
49+
client.DefaultRequestHeaders.Add("X-DashScope-WorkSpace", workspaceId);
3950
ClientPools.Add(GetCacheKey(), client);
4051
}
4152

4253
return client;
4354

44-
string GetCacheKey() => $"{apiKey}-{timeout?.TotalMilliseconds}";
55+
string GetCacheKey() => $"{apiKey}-{timeout?.TotalMilliseconds}-{baseAddress}-{workspaceId}";
4556
}
4657
}

src/Cnblogs.DashScope.Core/DashScopeClientCore.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ private static HttpRequestMessage BuildRequest<TPayload>(
297297
string url,
298298
TPayload? payload = null,
299299
bool sse = false,
300-
bool isTask = false)
300+
bool isTask = false,
301+
string? workspaceId = null)
301302
where TPayload : class
302303
{
303304
var message = new HttpRequestMessage(method, url)
@@ -315,6 +316,11 @@ private static HttpRequestMessage BuildRequest<TPayload>(
315316
message.Headers.Add("X-DashScope-Async", "enable");
316317
}
317318

319+
if (string.IsNullOrWhiteSpace(workspaceId) == false)
320+
{
321+
message.Headers.Add("X-DashScope-WorkspaceId", workspaceId);
322+
}
323+
318324
return message;
319325
}
320326

src/Cnblogs.DashScope.Core/IDashScopeClient.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text.Json;
2-
3-
namespace Cnblogs.DashScope.Core;
1+
namespace Cnblogs.DashScope.Core;
42

53
/// <summary>
64
/// DashScope APIs.

0 commit comments

Comments
 (0)