From 9ca669fe59ef77e7caae433deebcf90b3fb93270 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Tue, 18 Jun 2019 23:38:18 -0700 Subject: [PATCH 1/3] Return error on error reply. --- core/unifi/unifi.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 39a08282..e41348ff 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -157,6 +157,9 @@ func (u *Unifi) GetData(methodPath string, v interface{}) error { } else if err = json.Unmarshal(body, v); err != nil { return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) } + if resp.StatusCode != http.StatusOK { + return errors.Errorf("invalid status code from server %v %v", resp.StatusCode, resp.Status) + } return nil } From a5724ab60100c26b2fcb0a066b6fc7fadaf6ae8f Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 00:31:40 -0700 Subject: [PATCH 2/3] make another method --- core/unifi/unifi.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index e41348ff..ec76cf7a 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -141,25 +141,11 @@ func (u *Unifi) GetSites() (Sites, error) { // GetData makes a unifi request and unmarshal the response into a provided pointer. func (u *Unifi) GetData(methodPath string, v interface{}) error { - req, err := u.UniReq(methodPath, "") - if err != nil { - return errors.Wrapf(err, "c.UniReq(%s)", methodPath) - } - resp, err := u.Do(req) - if err != nil { - return errors.Wrapf(err, "c.Do(%s)", methodPath) - } - defer func() { - _ = resp.Body.Close() - }() - if body, err := ioutil.ReadAll(resp.Body); err != nil { + if body, err := u.GetJSON(methodPath); err != nil { return errors.Wrapf(err, "ioutil.ReadAll(%s)", methodPath) } else if err = json.Unmarshal(body, v); err != nil { return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) } - if resp.StatusCode != http.StatusOK { - return errors.Errorf("invalid status code from server %v %v", resp.StatusCode, resp.Status) - } return nil } @@ -178,3 +164,26 @@ func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err er } return } + +// GetJSON returns the raw JSON from a path. This is useful for debugging. +func (u *Unifi) GetJSON(apiPath string) ([]byte, error) { + req, err := u.UniReq(apiPath, "") + if err != nil { + return []byte{}, errors.Wrapf(err, "c.UniReq(%s)", apiPath) + } + resp, err := u.Do(req) + if err != nil { + return []byte{}, errors.Wrapf(err, "c.Do(%s)", apiPath) + } + defer func() { + _ = resp.Body.Close() + }() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return body, err + } + if resp.StatusCode != http.StatusOK { + err = errors.Errorf("invalid status code from server %v %v", resp.StatusCode, resp.Status) + } + return body, err +} From 4e56ad4d2b276e2ae8a6d1e1c13e133d694a9c92 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 00:32:57 -0700 Subject: [PATCH 3/3] fix errors --- core/unifi/unifi.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index ec76cf7a..2324256f 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -142,7 +142,7 @@ func (u *Unifi) GetSites() (Sites, error) { // GetData makes a unifi request and unmarshal the response into a provided pointer. func (u *Unifi) GetData(methodPath string, v interface{}) error { if body, err := u.GetJSON(methodPath); err != nil { - return errors.Wrapf(err, "ioutil.ReadAll(%s)", methodPath) + return err } else if err = json.Unmarshal(body, v); err != nil { return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) } @@ -180,7 +180,7 @@ func (u *Unifi) GetJSON(apiPath string) ([]byte, error) { }() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return body, err + return body, errors.Wrapf(err, "ioutil.ReadAll(%s)", apiPath) } if resp.StatusCode != http.StatusOK { err = errors.Errorf("invalid status code from server %v %v", resp.StatusCode, resp.Status)