Skip to content

Commit 369e560

Browse files
committed
feat(api): refactor provider key logic for API key usage and add test for compatibility grouping
- Extracted provider key determination into `apiKeyUsageProviderKey` for reuse and better readability. - Updated `GetAPIKeyUsage` to utilize the new function. - Added a new test case to validate compatibility grouping logic via `compat_name` attribute. Closes: router-for-me#3940 router-for-me#3941
1 parent babef2a commit 369e560

2 files changed

Lines changed: 62 additions & 4 deletions

File tree

internal/api/handlers/management/api_key_usage.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ func mergeRecentRequestBuckets(dst, src []coreauth.RecentRequestBucket) []coreau
4040
return dst
4141
}
4242

43+
func apiKeyUsageProviderKey(auth *coreauth.Auth) string {
44+
provider := strings.ToLower(strings.TrimSpace(auth.Provider))
45+
if auth.Attributes != nil {
46+
if compatName := strings.TrimSpace(auth.Attributes["compat_name"]); compatName != "" {
47+
provider = strings.ToLower(compatName)
48+
}
49+
}
50+
if provider == "" {
51+
return "unknown"
52+
}
53+
return provider
54+
}
55+
4356
// GetAPIKeyUsage returns recent request buckets for all in-memory api_key auths,
4457
// grouped by provider and keyed by "base_url|api_key".
4558
func (h *Handler) GetAPIKeyUsage(c *gin.Context) {
@@ -78,10 +91,7 @@ func (h *Handler) GetAPIKeyUsage(c *gin.Context) {
7891
}
7992
}
8093
compositeKey := baseURL + "|" + apiKey
81-
provider := strings.ToLower(strings.TrimSpace(auth.Provider))
82-
if provider == "" {
83-
provider = "unknown"
84-
}
94+
provider := apiKeyUsageProviderKey(auth)
8595

8696
recent := auth.RecentRequestsSnapshot(now)
8797
providerBucket, ok := out[provider]

internal/api/handlers/management/api_key_usage_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,51 @@ func TestGetAPIKeyUsage_GroupsByProviderAndAPIKey(t *testing.T) {
9292
t.Fatalf("claude totals = %d/%d, want 1/0", claudeSuccess, claudeFailed)
9393
}
9494
}
95+
96+
func TestGetAPIKeyUsage_GroupsOpenAICompatibleByCompatName(t *testing.T) {
97+
t.Setenv("MANAGEMENT_PASSWORD", "")
98+
99+
manager := coreauth.NewManager(nil, nil, nil)
100+
if _, err := manager.Register(context.Background(), &coreauth.Auth{
101+
ID: "vast-auth",
102+
Provider: "openai-compatible-vast",
103+
Attributes: map[string]string{
104+
"api_key": "vast-key",
105+
"base_url": "https://www.vastnum.com/v1",
106+
"compat_name": "VAST",
107+
},
108+
}); err != nil {
109+
t.Fatalf("register vast auth: %v", err)
110+
}
111+
112+
manager.MarkResult(context.Background(), coreauth.Result{AuthID: "vast-auth", Provider: "openai-compatible-vast", Model: "gpt-5", Success: true})
113+
114+
h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: t.TempDir()}, manager)
115+
116+
rec := httptest.NewRecorder()
117+
ginCtx, _ := gin.CreateTestContext(rec)
118+
req := httptest.NewRequest(http.MethodGet, "/v0/management/api-key-usage", nil)
119+
ginCtx.Request = req
120+
h.GetAPIKeyUsage(ginCtx)
121+
122+
if rec.Code != http.StatusOK {
123+
t.Fatalf("status = %d, want %d body=%s", rec.Code, http.StatusOK, rec.Body.String())
124+
}
125+
126+
var payload map[string]map[string]apiKeyUsageEntry
127+
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
128+
t.Fatalf("decode payload: %v", err)
129+
}
130+
131+
if _, exists := payload["openai-compatible-vast"]; exists {
132+
t.Fatalf("unexpected namespaced provider bucket in payload: %#v", payload)
133+
}
134+
vastBucket, exists := payload["vast"]
135+
if !exists {
136+
t.Fatalf("missing compat provider bucket in payload: %#v", payload)
137+
}
138+
vastEntry := vastBucket["https://www.vastnum.com/v1|vast-key"]
139+
if vastEntry.Success != 1 || vastEntry.Failed != 0 {
140+
t.Fatalf("vast totals = %d/%d, want 1/0", vastEntry.Success, vastEntry.Failed)
141+
}
142+
}

0 commit comments

Comments
 (0)