From 9ce24ee41c5649ca5a03edf60bef1606dca6a8d3 Mon Sep 17 00:00:00 2001 From: Alexander Kiryukhin Date: Sat, 30 Mar 2019 22:12:37 +0300 Subject: Keyboard builder Logger --- api.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'api.go') diff --git a/api.go b/api.go index 3d727fd..ec45e73 100644 --- a/api.go +++ b/api.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "net/url" "strconv" @@ -22,6 +23,7 @@ type Api struct { url *url.URL timeout int pause int + logging bool } // New TamTam Api object @@ -33,9 +35,14 @@ func New(key string) *Api { version: "1.0.3", timeout: 30, pause: 1, + logging: false, } } +func (a *Api) EnableLogging() { + a.logging = true +} + // region Misc methods func (a *Api) GetMe() (*UserWithPhoto, error) { @@ -464,7 +471,11 @@ func (a *Api) getUpdates(limit int, timeout int, marker int64, types []string) ( return result, err } defer body.Close() - return result, json.NewDecoder(body).Decode(result) + jb, _ := ioutil.ReadAll(body) + if a.logging { + log.Printf("Received: %s", string(jb)) + } + return result, json.Unmarshal(jb, result) } func (a *Api) request(method, path string, query url.Values, body interface{}) (io.ReadCloser, error) { @@ -478,6 +489,9 @@ func (a *Api) request(method, path string, query url.Values, body interface{}) ( if err != nil { return nil, err } + if a.logging { + log.Printf("Sent: [%s %s] Query: %#v Body: %s", method, path, query, string(j)) + } req, err := http.NewRequest(method, u.String(), bytes.NewReader(j)) if err != nil { return nil, err -- cgit v1.2.3