From 638e16f2b05612b15970d18c6ab811a9aa0c31ff Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 00:40:25 -0700 Subject: [PATCH 1/5] less is more --- core/unifi/unifi.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 2324256f..c8db87d7 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -141,12 +141,12 @@ 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 { + body, err := u.GetJSON(methodPath) + if err != nil { return err - } else if err = json.Unmarshal(body, v); err != nil { - return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) } - return nil + err = json.Unmarshal(body, v) + return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath) } // UniReq is a small helper function that adds an Accept header. From 3a8fcda20ac04d458a17837eb6355ca78f8eb316 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 00:55:19 -0700 Subject: [PATCH 2/5] more cleanup --- core/unifi/unifi.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index c8db87d7..3b408db7 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -39,7 +39,7 @@ func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) { // getController is a helper method to make testsing a bit easier. func (u *Unifi) getController(user, pass string) error { // magic login. - req, err := u.UniReq(LoginPath, `{"username": "`+user+`","password": "`+pass+`"}`) + req, err := u.UniReq(LoginPath, fmt.Sprintf(`{"username":"%s","password":"%s"}`, user, pass)) if err != nil { return errors.Wrap(err, "UniReq(LoginPath, json)") } @@ -52,8 +52,8 @@ func (u *Unifi) getController(user, pass string) error { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { - return errors.Errorf("authentication failed (user: %s): %s (status: %d/%s)", - user, u.baseURL+LoginPath, resp.StatusCode, resp.Status) + return errors.Errorf("authentication failed (user: %s): %s (status: %s)", + user, u.baseURL+LoginPath, resp.Status) } return nil } @@ -154,10 +154,11 @@ func (u *Unifi) GetData(methodPath string, v interface{}) error { // And if you're doing that... sumbut a pull request with your new struct. :) // This is a helper method that is exposed for convenience. func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err error) { - if params != "" { - req, err = http.NewRequest("POST", u.baseURL+apiPath, bytes.NewBufferString(params)) - } else { - req, err = http.NewRequest("GET", u.baseURL+apiPath, nil) + switch path := u.baseURL + apiPath; { + case params == "": + req, err = http.NewRequest("GET", path, nil) + default: + req, err = http.NewRequest("POST", path, bytes.NewBufferString(params)) } if err == nil { req.Header.Add("Accept", "application/json") @@ -183,7 +184,7 @@ func (u *Unifi) GetJSON(apiPath string) ([]byte, error) { 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) + err = errors.Errorf("invalid status code from server %s", resp.Status) } return body, err } From a423a8aebe1a136b4acdf23d53909af286d70734 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 01:26:21 -0700 Subject: [PATCH 3/5] Get rid of intermediate logger. --- core/unifi/{parsers.go => devices.go} | 14 +++++++------- core/unifi/types.go | 15 +++------------ core/unifi/unifi.go | 6 ++++-- 3 files changed, 14 insertions(+), 21 deletions(-) rename core/unifi/{parsers.go => devices.go} (75%) diff --git a/core/unifi/parsers.go b/core/unifi/devices.go similarity index 75% rename from core/unifi/parsers.go rename to core/unifi/devices.go index a2b97ce5..20a6a580 100644 --- a/core/unifi/parsers.go +++ b/core/unifi/devices.go @@ -13,7 +13,7 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { } else if t, ok := o["type"].(string); ok { assetType = t } - u.dLogf("Unmarshalling Device Type: %v, site %s ", assetType, siteName) + u.DebugLog("Unmarshalling Device Type: %v, site %s ", assetType, siteName) // Choose which type to unmarshal into based on the "type" json key. switch assetType { // Unmarshal again into the correct type.. case "uap": @@ -32,7 +32,7 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { devices.USWs = append(devices.USWs, usw) } default: - u.eLogf("unknown asset type - %v - skipping", assetType) + u.ErrorLog("unknown asset type - %v - skipping", assetType) } } return devices @@ -41,12 +41,12 @@ func (u *Unifi) parseDevices(data []json.RawMessage, siteName string) *Devices { // unmarshalDevice handles logging for the unmarshal operations in parseDevices(). func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) { if err = json.Unmarshal(data, v); err != nil { - u.eLogf("json.Unmarshal(%v): %v", dev, err) - u.eLogf("Enable Debug Logging to output the failed payload.") + u.ErrorLog("json.Unmarshal(%v): %v", dev, err) + u.ErrorLog("Enable Debug Logging to output the failed payload.") json, err := data.MarshalJSON() - u.dLogf("Failed Payload: %s (marshal err: %v)", json, err) - u.dLogf("The above payload can prove useful during torubleshooting when you open an Issue:") - u.dLogf("==- https://github.com/golift/unifi/issues/new -==") + u.DebugLog("Failed Payload: %s (marshal err: %v)", json, err) + u.DebugLog("The above payload can prove useful during torubleshooting when you open an Issue:") + u.DebugLog("==- https://github.com/golift/unifi/issues/new -==") } return err } diff --git a/core/unifi/types.go b/core/unifi/types.go index 77be80e7..a1782e83 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -32,18 +32,9 @@ const ( // that matches this interface to capture debug and error logs. type Logger func(msg string, fmt ...interface{}) -// dLogf logs a debug message. -func (u *Unifi) dLogf(msg string, v ...interface{}) { - if u.DebugLog != nil { - u.DebugLog("[DEBUG] "+msg, v...) - } -} - -// eLogf logs an error message. -func (u *Unifi) eLogf(msg string, v ...interface{}) { - if u.ErrorLog != nil { - u.ErrorLog("[ERROR] "+msg, v...) - } +// DiscardLogs is the default logger. +func DiscardLogs(msg string, v ...interface{}) { + // do nothing. } // Devices contains a list of all the unifi devices from a controller. diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 3b408db7..cac82978 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -32,6 +32,8 @@ func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) { Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}}, Jar: jar, }, + ErrorLog: DiscardLogs, + DebugLog: DiscardLogs, } return u, u.getController(user, pass) } @@ -74,7 +76,7 @@ func (u *Unifi) GetClients(sites []Site) (Clients, error) { var response struct { Data []Client `json:"data"` } - u.dLogf("Polling Controller, retreiving Unifi Clients, site %s (%s) ", site.Name, site.Desc) + u.DebugLog("Polling Controller, retreiving Unifi Clients, site %s (%s) ", site.Name, site.Desc) clientPath := fmt.Sprintf(ClientPath, site.Name) if err := u.GetData(clientPath, &response); err != nil { return nil, err @@ -135,7 +137,7 @@ func (u *Unifi) GetSites() (Sites, error) { response.Data[i].SiteName = response.Data[i].Desc + " (" + response.Data[i].Name + ")" sites = append(sites, response.Data[i].Name) } - u.dLogf("Found %d site(s): %s", len(sites), strings.Join(sites, ",")) + u.DebugLog("Found %d site(s): %s", len(sites), strings.Join(sites, ",")) return response.Data, nil } From 254b2e5e568da5a4ff9db148b9eaf0adfa8a6f18 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 01:36:28 -0700 Subject: [PATCH 4/5] better error log default. --- core/unifi/types.go | 2 +- core/unifi/unifi.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/unifi/types.go b/core/unifi/types.go index a1782e83..9330318e 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -32,7 +32,7 @@ const ( // that matches this interface to capture debug and error logs. type Logger func(msg string, fmt ...interface{}) -// DiscardLogs is the default logger. +// DiscardLogs is the default debug logger. func DiscardLogs(msg string, v ...interface{}) { // do nothing. } diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index cac82978..ccf9e36b 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -12,6 +12,7 @@ import ( "fmt" "io" "io/ioutil" + "log" "net/http" "net/http/cookiejar" "strings" @@ -32,7 +33,7 @@ func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) { Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !verifySSL}}, Jar: jar, }, - ErrorLog: DiscardLogs, + ErrorLog: log.Printf, DebugLog: DiscardLogs, } return u, u.getController(user, pass) From b0eba73ffae5501e1df3ae14dfe27b54982e6ed5 Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 19 Jun 2019 01:37:39 -0700 Subject: [PATCH 5/5] update comment --- core/unifi/types.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/unifi/types.go b/core/unifi/types.go index 9330318e..4c3920f0 100644 --- a/core/unifi/types.go +++ b/core/unifi/types.go @@ -47,7 +47,8 @@ type Devices struct { // Unifi is what you get in return for providing a password! Unifi represents // a controller that you can make authenticated requests to. Use this to make -// additional requests for devices, clients or other custom data. +// additional requests for devices, clients or other custom data. Do not set +// the loggers to nil. Set them to DiscardLogs if you want no logs. type Unifi struct { *http.Client baseURL string