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 bb38a0d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion ah/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"golang.org/x/oauth2"
"io"
"net/http"
"net/http/httputil"
"net/url"
)

Expand Down Expand Up @@ -115,8 +116,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 +147,27 @@ 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))
}

// NewAPIClient returns APIClient instance
Expand Down

0 comments on commit bb38a0d

Please sign in to comment.