Skip to content

Commit

Permalink
Debug for requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Zarechenskyi committed Jul 10, 2024
1 parent ca039a4 commit 6a99dfd
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion ah/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"golang.org/x/oauth2"
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)

var (
Expand Down Expand Up @@ -115,8 +117,11 @@ func (c *APIClient) list(ctx context.Context, path string, options *ListOptions,
// Do sends an API request
func (c *APIClient) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
req = req.WithContext(ctx)
resp, err := c.client.Do(req)

// Log the request details
c.logRequest(req)

resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -143,7 +148,59 @@ func (c *APIClient) Do(ctx context.Context, req *http.Request, v interface{}) (*
return nil, err
}
return resp, nil
}

func (c *APIClient) logRequest(req *http.Request) {
fmt.Printf("Raw Request: %+v\n", *req)

// Clone the request body
var bodyBytes []byte
if req.Body != nil {
bodyBytes, _ = io.ReadAll(req.Body)
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) // Restore the body for actual request
}

// Dump the request
dump, err := httputil.DumpRequestOut(req, true)
if err != nil {
fmt.Printf("Failed to dump request: %v\n", err)
return
}

// Log the request
fmt.Printf("Request: %s\n", string(dump))
}

func (c *APIClient) generateCurlCommand(req *http.Request) string {

Check failure on line 174 in ah/api.go

View workflow job for this annotation

GitHub Actions / golangci

func `(*APIClient).generateCurlCommand` is unused (unused)
var curlCommand strings.Builder

curlCommand.WriteString("curl -X ")
curlCommand.WriteString(req.Method)
curlCommand.WriteString(" '")
curlCommand.WriteString(req.URL.String())
curlCommand.WriteString("'")

for name, values := range req.Header {
for _, value := range values {
curlCommand.WriteString(" -H '")
curlCommand.WriteString(name)
curlCommand.WriteString(": ")
curlCommand.WriteString(value)
curlCommand.WriteString("'")
}
}

if req.Body != nil {
bodyBytes, _ := io.ReadAll(req.Body)
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
if len(bodyBytes) > 0 {
curlCommand.WriteString(" --data '")
curlCommand.WriteString(string(bodyBytes))
curlCommand.WriteString("'")
}
}

return curlCommand.String()
}

// NewAPIClient returns APIClient instance
Expand Down

0 comments on commit 6a99dfd

Please sign in to comment.