Skip to content

Commit

Permalink
chore(tmc): add TMC_API_DEBUG env var for TMC debug. (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
i4k-tm committed Oct 20, 2023
2 parents 025d613 + 0c95945 commit 6ed6efa
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"path"
"strconv"

Expand Down Expand Up @@ -68,6 +70,14 @@ type (
}
)

var debugAPIRequests bool

func init() {
if d := os.Getenv("TMC_API_DEBUG"); d == "1" || d == "true" {
debugAPIRequests = true
}
}

// Users retrieves the user details for the signed in user.
func (c *Client) Users(ctx context.Context) (user User, err error) {
return Get[User](ctx, c, UsersPath)
Expand Down Expand Up @@ -218,12 +228,22 @@ func Request[T Resource](ctx context.Context, c *Client, method string, resource
return entity, err
}

if debugAPIRequests {
data, _ := httputil.DumpRequestOut(req, true)
fmt.Printf(">>> %s\n\n", data)
}

client := c.httpClient()
resp, err := client.Do(req)
if err != nil {
return entity, err
}

if debugAPIRequests {
data, _ := httputil.DumpResponse(resp, true)
fmt.Printf("<<< %s\n\n", data)
}

defer func() {
err = errors.L(err, resp.Body.Close()).AsError()
}()
Expand Down

0 comments on commit 6ed6efa

Please sign in to comment.