chore(go): audit root package

Share one request, decode, and error path across the client, public
endpoints, and export download; align types with the Backend contract
and split types_api.go.

Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
This commit is contained in:
雷电芽衣 committed 2026-09-03 01:06:42 -04:00
1 parent f5341ed557
commit 27b720ca69
11 files changed
+528 -663

No files matched your search

+18 -105
View File
@@ -1,116 +1,29 @@
package elydora
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
)
import "net/http"
// Health checks the health of the Elydora API. This is a public endpoint
// that does not require authentication.
// Health checks the public health endpoint.
func Health(baseURL string) (*HealthResponse, error) {
if baseURL == "" {
baseURL = defaultBaseURL
}
baseURL = strings.TrimRight(baseURL, "/")
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest(http.MethodGet, baseURL+"/v1/health", nil)
if err != nil {
return nil, fmt.Errorf("elydora: create request: %w", err)
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("elydora: http request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 10<<20))
if err != nil {
return nil, fmt.Errorf("elydora: read response body: %w", err)
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var result HealthResponse
if err := json.Unmarshal(respBody, &result); err != nil {
return nil, fmt.Errorf("elydora: unmarshal response: %w", err)
}
return &result, nil
}
var errResp errorResponse
if err := json.Unmarshal(respBody, &errResp); err == nil && errResp.Error.Code != "" {
return nil, &ElydoraError{
StatusCode: resp.StatusCode,
Code: errResp.Error.Code,
Message: errResp.Error.Message,
RequestID: errResp.Error.RequestID,
Details: errResp.Error.Details,
}
}
return nil, &ElydoraError{
StatusCode: resp.StatusCode,
Code: ErrorCodeInternalError,
Message: string(respBody),
var result HealthResponse
if err := publicRequest(http.MethodGet, normalizeBaseURL(baseURL)+"/v1/health", nil, &result); err != nil {
return nil, err
}
return &result, nil
}
// DeepHealth checks the deep health of the Elydora API, including its backend
// dependencies (D1, R2, KV). This is a public endpoint that does not require
// authentication.
// DeepHealth checks backend dependencies; 503 reports a degraded API.
func DeepHealth(baseURL string) (*DeepHealthResponse, error) {
if baseURL == "" {
baseURL = defaultBaseURL
}
baseURL = strings.TrimRight(baseURL, "/")
client := &http.Client{Timeout: 30 * time.Second}
req, err := http.NewRequest(http.MethodGet, baseURL+"/v1/health/deep", nil)
var result DeepHealthResponse
err := publicRequest(
http.MethodGet,
normalizeBaseURL(baseURL)+"/v1/health/deep",
nil,
&result,
http.StatusOK,
http.StatusServiceUnavailable,
)
if err != nil {
return nil, fmt.Errorf("elydora: create request: %w", err)
}
req.Header.Set("Accept", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("elydora: http request: %w", err)
}
defer resp.Body.Close()
respBody, err := io.ReadAll(io.LimitReader(resp.Body, 10<<20))
if err != nil {
return nil, fmt.Errorf("elydora: read response body: %w", err)
}
// Accept both 200 (healthy) and 503 (degraded) as valid deep-health responses.
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusServiceUnavailable {
var result DeepHealthResponse
if err := json.Unmarshal(respBody, &result); err != nil {
return nil, fmt.Errorf("elydora: unmarshal response: %w", err)
}
return &result, nil
}
var errResp errorResponse
if err := json.Unmarshal(respBody, &errResp); err == nil && errResp.Error.Code != "" {
return nil, &ElydoraError{
StatusCode: resp.StatusCode,
Code: errResp.Error.Code,
Message: errResp.Error.Message,
RequestID: errResp.Error.RequestID,
Details: errResp.Error.Details,
}
}
return nil, &ElydoraError{
StatusCode: resp.StatusCode,
Code: ErrorCodeInternalError,
Message: string(respBody),
return nil, err
}
return &result, nil
}