Add request logging capabilities to API handlers and update .gitignore
docker-image / docker (push) Has been cancelled
goreleaser / goreleaser (push) Has been cancelled

Enhance API response handling by storing responses in context and updating request logger to include API responses
This commit is contained in:
Luis Pater
2025-08-16 05:03:10 +08:00
parent 4155805ad6
commit fcadf08921
10 changed files with 191 additions and 26 deletions
+12 -1
View File
@@ -18,6 +18,7 @@ import (
"sync"
"time"
"github.com/gin-gonic/gin"
geminiAuth "github.com/luispater/CLIProxyAPI/internal/auth/gemini"
"github.com/luispater/CLIProxyAPI/internal/config"
log "github.com/sirupsen/logrus"
@@ -196,8 +197,10 @@ func (c *GeminiClient) SetupUser(ctx context.Context, email, projectID string) e
// makeAPIRequest handles making requests to the CLI API endpoints.
func (c *GeminiClient) makeAPIRequest(ctx context.Context, endpoint, method string, body interface{}, result interface{}) error {
var reqBody io.Reader
var jsonBody []byte
var err error
if body != nil {
jsonBody, err := json.Marshal(body)
jsonBody, err = json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal request body: %w", err)
}
@@ -227,6 +230,10 @@ func (c *GeminiClient) makeAPIRequest(ctx context.Context, endpoint, method stri
req.Header.Set("Client-Metadata", metadataStr)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken))
if ginContext, ok := ctx.Value("gin").(*gin.Context); ok {
ginContext.Set("API_REQUEST", jsonBody)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return fmt.Errorf("failed to execute request: %w", err)
@@ -324,6 +331,10 @@ func (c *GeminiClient) APIRequest(ctx context.Context, endpoint string, body int
req.Header.Set("x-goog-api-key", c.glAPIKey)
}
if ginContext, ok := ctx.Value("gin").(*gin.Context); ok {
ginContext.Set("API_REQUEST", jsonBody)
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, &ErrorMessage{500, fmt.Errorf("failed to execute request: %v", err)}