feat(client): split WithToken, add Raw + auth helpers (PR-2)

Five additive SDK changes that PR-3 (CLI Factory adapter) and PR-4
(auth login / status) consume.

WithToken split (Breaking + alias):
- WithAPIKey(key) sets X-API-Key header (long-lived API keys)
- WithBearerToken(token) sets Authorization: Bearer header (JWTs)
- WithToken kept as deprecated alias for WithAPIKey for two minor versions
- doRequest now sets X-API-Key and Authorization headers independently;
  callers can carry both kinds of credentials simultaneously

applyAuthHeaders extraction:
- Header injection (X-API-Key, Authorization, X-Request-ID, X-Tenant-ID)
  is now a Client method shared between doRequest and the multipart path
  in CreateKnowledgeFromFile. Eliminates ~30 lines of drift risk and
  fixes the latent missing X-Tenant-ID on file uploads.
- Drops the dead second tenant-header switch in doRequest.

Raw escape hatch:
- Client.Raw(ctx, method, path, body) is the public passthrough used by
  the future \`weknora api\` command. godoc marks it experimental.

Auth methods (auth.go, new):
- Login(ctx, LoginRequest) -> POST /auth/login (existing server endpoint)
- GetCurrentUser(ctx) -> GET /auth/me, returns *CurrentUserResponse
- RefreshToken(ctx, refreshToken) -> POST /auth/refresh
- Types: LoginRequest, LoginResponse, AuthUser, AuthTenant,
  CurrentUserResponse, RefreshTokenResponse

No server changes. The auth methods consume endpoints that already exist;
PR-3/PR-4 wire them through the CLI.
This commit is contained in:
nullkey
2026-05-06 14:22:53 +08:00
committed by lyingbug
parent a39805aa89
commit 37d4fbde5d
4 changed files with 225 additions and 51 deletions
+1 -7
View File
@@ -164,14 +164,8 @@ func (c *Client) CreateKnowledgeFromFile(ctx context.Context,
return nil, fmt.Errorf("failed to close writer: %w", err)
}
// Set request headers
req.Header.Set("Content-Type", writer.FormDataContentType())
if c.token != "" {
req.Header.Set("X-API-Key", c.token)
}
if requestID := ctx.Value("RequestID"); requestID != nil {
req.Header.Set("X-Request-ID", requestID.(string))
}
c.applyAuthHeaders(ctx, req)
// Set the request body
req.Body = io.NopCloser(body)