客户端添加切换租户

This commit is contained in:
cn-kali-team
2026-04-29 18:57:00 +08:00
committed by lyingbug
parent b3898eb101
commit 1e170793b3
4 changed files with 103 additions and 2 deletions
+21 -1
View File
@@ -23,7 +23,7 @@
```go
import (
"context"
"github.com/Tencent/WeKnora/internal/client"
"github.com/Tencent/WeKnora/client"
"time"
)
@@ -35,6 +35,26 @@ apiClient := client.NewClient(
)
```
### 租户配置
客户端支持通过 `WithTenantID` 设置默认租户,请求时会自动携带 `X-Tenant-ID` 请求头:
```go
tenantID := uint64(10000)
apiClient := client.NewClient(
"http://api.example.com",
client.WithToken("your-auth-token"),
client.WithTenantID(tenantID),
)
```
如果某个请求需要临时切换租户,可以在 `context` 中设置 `TenantID`,值可以是 `uint64`、`*uint64` 或字符串形式的数字,客户端会优先使用该值:
```go
ctx := context.WithValue(context.Background(), "TenantID", uint64(10000))
// 调用任意客户端方法时传入 ctx,即可切换到租户 10000
```
### 示例:创建知识库并上传文件
```go