From b6a562baec5ee28e9ec830e6f13cb52c88f7e8f5 Mon Sep 17 00:00:00 2001 From: Alex Zorin Date: Mon, 29 Jul 2019 11:14:36 +1000 Subject: [PATCH] Allow debug printing of raw responses from Authy. To enable, run with environment variable: AUTHY_DEBUG=1 This will hopefully make it easier to investigate issues like #1. --- authy.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/authy.go b/authy.go index 6419f55..18513f0 100644 --- a/authy.go +++ b/authy.go @@ -1,6 +1,7 @@ package authy import ( + "bytes" "context" "encoding/hex" "encoding/json" @@ -8,6 +9,7 @@ import ( "io" "net/http" "net/url" + "os" "strconv" "strings" "time" @@ -66,7 +68,17 @@ func (c Client) doRequest(ctx context.Context, method, url string, body io.Reade } defer resp.Body.Close() - return json.NewDecoder(resp.Body).Decode(&dest) + var r io.Reader = resp.Body + if os.Getenv("AUTHY_DEBUG") == "1" { + var debugBuf bytes.Buffer + r = io.TeeReader(resp.Body, &debugBuf) + defer func() { + fmt.Fprintf(os.Stderr, "[AUTHY_DEBUG] Sent request to: %s, got response: %s\n", + req.URL.String(), debugBuf.String()) + }() + } + + return json.NewDecoder(r).Decode(&dest) } // QueryUser fetches the status of an Authy user account.