Skip to content

Commit

Permalink
chore: ADD MOAR DEBUG
Browse files Browse the repository at this point in the history
  • Loading branch information
kashalls committed Sep 16, 2024
1 parent 9bff600 commit 73255d1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/unifi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
Expand Down Expand Up @@ -94,7 +95,11 @@ func (c *httpClient) login() error {

// doRequest makes an HTTP request to the UniFi controller.
func (c *httpClient) doRequest(method, path string, body io.Reader) (*http.Response, error) {
log.Debug(fmt.Sprintf("making %s request to %s", method, path))
log.With(
zap.String("req_method", method),
zap.String("req_path", path),
zap.Any("req_body", body),
).Debug("Creating Request")

req, err := http.NewRequest(method, path, body)
if err != nil {
Expand All @@ -112,7 +117,14 @@ func (c *httpClient) doRequest(method, path string, body io.Reader) (*http.Respo
c.csrf = csrf
}

log.Debug(fmt.Sprintf("response code from %s request to %s: %d", method, path, resp.StatusCode))
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error("Failed to read response body", zap.Error(err))
return resp, nil
}
bodyString := string(bodyBytes)

log.With(zap.String("req_method", method), zap.String("req_path", path), zap.Int("req_code", resp.StatusCode), zap.String("req_body", bodyString)).Debug("Returned Request")

// If the status code is 401, re-login and retry the request
if resp.StatusCode == http.StatusUnauthorized {
Expand Down

0 comments on commit 73255d1

Please sign in to comment.