added additional debug logging

This commit is contained in:
Samuel Lorch 2021-09-02 09:26:16 +02:00
parent 2a08213258
commit 866ca4e307

View file

@ -83,6 +83,7 @@ func (c *Client) newRequest(method, path string, body interface{}) (*http.Reques
return nil, fmt.Errorf("Parsing URL: %w", err)
}
u := c.baseURL.ResolveReference(rel)
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
@ -104,6 +105,15 @@ func (c *Client) newRequest(method, path string, body interface{}) (*http.Reques
req.AddCookie(&c.sessionToken)
req.AddCookie(&c.csrfToken)
// Debugging
c.log("Request URL: %v", req.URL.String())
if c.Debug && body != nil {
data, err := json.Marshal(body)
if err == nil {
c.log("Raw Request: %v", string(data))
}
}
return req, nil
}
@ -127,6 +137,8 @@ func (c *Client) do(ctx context.Context, req *http.Request, v *APIResponse) (*ht
return resp, fmt.Errorf("Error Reading Resopnse Body: %w", err)
}
c.log("Raw Response: %v", string(bodyBytes))
err = json.Unmarshal(bodyBytes, v)
if err != nil {
return resp, fmt.Errorf("Unable to Parse JSON API Response with HTTP Status Code %v: %w", resp.StatusCode, err)