Bug fix on FlexInt=nil/null, remove errors pkg, minor cleanup.

This commit is contained in:
davidnewhall2 2019-08-24 01:32:12 -07:00
parent 48b9fb701e
commit 39c6f6801e
5 changed files with 32 additions and 66 deletions

25
core/unifi/Gopkg.lock generated
View File

@ -19,15 +19,7 @@
"v2", "v2",
] ]
pruneopts = "UT" pruneopts = "UT"
revision = "8ff2fc3824fcb533795f9a2f233275f0bb18d6c5" revision = "fc22c7df067eefd070157f157893fbce961d6359"
[[projects]]
digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b"
name = "github.com/pkg/errors"
packages = ["."]
pruneopts = "UT"
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
version = "v0.8.1"
[[projects]] [[projects]]
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe" digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
@ -38,19 +30,26 @@
version = "v1.0.0" version = "v1.0.0"
[[projects]] [[projects]]
digest = "1:972c2427413d41a1e06ca4897e8528e5a1622894050e2f527b38ddf0f343f759" digest = "1:8548c309c65a85933a625be5e7d52b6ac927ca30c56869fae58123b8a77a75e1"
name = "github.com/stretchr/testify" name = "github.com/stretchr/testify"
packages = ["assert"] packages = ["assert"]
pruneopts = "UT" pruneopts = "UT"
revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" revision = "221dbe5ed46703ee255b1da0dec05086f5035f62"
version = "v1.3.0" version = "v1.4.0"
[[projects]]
digest = "1:4d2e5a73dc1500038e504a8d78b986630e3626dc027bc030ba5c75da257cdb96"
name = "gopkg.in/yaml.v2"
packages = ["."]
pruneopts = "UT"
revision = "51d6538a90f86fe93ac480b35f37b2be17fef232"
version = "v2.2.2"
[solve-meta] [solve-meta]
analyzer-name = "dep" analyzer-name = "dep"
analyzer-version = 1 analyzer-version = 1
input-imports = [ input-imports = [
"github.com/influxdata/influxdb1-client/v2", "github.com/influxdata/influxdb1-client/v2",
"github.com/pkg/errors",
"github.com/stretchr/testify/assert", "github.com/stretchr/testify/assert",
] ]
solver-name = "gps-cdcl" solver-name = "gps-cdcl"

View File

@ -1,42 +1,7 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]] [[constraint]]
branch = "master" branch = "master"
name = "github.com/influxdata/influxdb1-client" name = "github.com/influxdata/influxdb1-client"
[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.1"
[[constraint]]
name = "github.com/stretchr/testify"
version = "1.3.0"
[prune] [prune]
go-tests = true go-tests = true
unused-packages = true unused-packages = true

View File

@ -8,7 +8,6 @@ import (
"time" "time"
influx "github.com/influxdata/influxdb1-client/v2" influx "github.com/influxdata/influxdb1-client/v2"
"github.com/pkg/errors"
) )
// IDSList contains a list that contains all of the IDS Events on a controller. // IDSList contains a list that contains all of the IDS Events on a controller.
@ -124,7 +123,7 @@ func (u *Unifi) GetSiteIDS(site *Site, from, to time.Time) ([]*IDS, error) {
return nil, err return nil, err
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("invalid status code from server %s", resp.Status) return nil, fmt.Errorf("invalid status code from server %s", resp.Status)
} }
if err := json.Unmarshal(body, &response); err != nil { if err := json.Unmarshal(body, &response); err != nil {
return nil, err return nil, err

View File

@ -2,11 +2,10 @@ package unifi
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"github.com/pkg/errors"
) )
// This is a list of unifi API paths. // This is a list of unifi API paths.
@ -90,8 +89,12 @@ func (f *FlexInt) UnmarshalJSON(b []byte) error {
f.Txt = i f.Txt = i
f.Val, _ = strconv.ParseFloat(i, 64) f.Val, _ = strconv.ParseFloat(i, 64)
return nil return nil
case nil:
f.Txt = "0"
f.Val = 0
return nil
default: default:
return errors.New("Cannot unmarshal to FlexInt") return fmt.Errorf("cannot unmarshal to FlexInt: %s", b)
} }
} }

View File

@ -16,8 +16,6 @@ import (
"net/http" "net/http"
"net/http/cookiejar" "net/http/cookiejar"
"strings" "strings"
"github.com/pkg/errors"
) )
// NewUnifi creates a http.Client with authenticated cookies. // NewUnifi creates a http.Client with authenticated cookies.
@ -26,7 +24,7 @@ import (
func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) { func NewUnifi(user, pass, url string, verifySSL bool) (*Unifi, error) {
jar, err := cookiejar.New(nil) jar, err := cookiejar.New(nil)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "cookiejar.New(nil)") return nil, err
} }
u := &Unifi{baseURL: strings.TrimRight(url, "/"), u := &Unifi{baseURL: strings.TrimRight(url, "/"),
Client: &http.Client{ Client: &http.Client{
@ -44,21 +42,24 @@ func (u *Unifi) getController(user, pass string) error {
// magic login. // magic login.
req, err := u.UniReq(LoginPath, fmt.Sprintf(`{"username":"%s","password":"%s"}`, user, pass)) req, err := u.UniReq(LoginPath, fmt.Sprintf(`{"username":"%s","password":"%s"}`, user, pass))
if err != nil { if err != nil {
return errors.Wrap(err, "UniReq(LoginPath, json)") return err
} }
resp, err := u.Do(req) resp, err := u.Do(req)
if err != nil { if err != nil {
return errors.Wrap(err, "authReq.Do(req)") return err
} }
defer func() { defer func() {
_, _ = io.Copy(ioutil.Discard, resp.Body) // avoid leaking. _, _ = io.Copy(ioutil.Discard, resp.Body) // avoid leaking.
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return errors.Errorf("authentication failed (user: %s): %s (status: %s)", return fmt.Errorf("authentication failed (user: %s): %s (status: %s)",
user, u.baseURL+LoginPath, resp.Status) user, u.baseURL+LoginPath, resp.Status)
} }
return errors.Wrap(u.getServer(), "unable to get server version") if err := u.getServer(); err != nil {
return fmt.Errorf("unable to get server version: %v", err)
}
return nil
} }
// getServer sets the controller's version and UUID. // getServer sets the controller's version and UUID.
@ -139,8 +140,7 @@ func (u *Unifi) GetData(methodPath string, v interface{}) error {
if err != nil { if err != nil {
return err return err
} }
err = json.Unmarshal(body, v) return json.Unmarshal(body, v)
return errors.Wrapf(err, "json.Unmarshal(%s)", methodPath)
} }
// UniReq is a small helper function that adds an Accept header. // UniReq is a small helper function that adds an Accept header.
@ -164,21 +164,21 @@ func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err er
func (u *Unifi) GetJSON(apiPath string) ([]byte, error) { func (u *Unifi) GetJSON(apiPath string) ([]byte, error) {
req, err := u.UniReq(apiPath, "") req, err := u.UniReq(apiPath, "")
if err != nil { if err != nil {
return []byte{}, errors.Wrapf(err, "c.UniReq(%s)", apiPath) return []byte{}, err
} }
resp, err := u.Do(req) resp, err := u.Do(req)
if err != nil { if err != nil {
return []byte{}, errors.Wrapf(err, "c.Do(%s)", apiPath) return []byte{}, err
} }
defer func() { defer func() {
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return body, errors.Wrapf(err, "ioutil.ReadAll(%s)", apiPath) return body, err
} }
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
err = errors.Errorf("invalid status code from server %s", resp.Status) err = fmt.Errorf("invalid status code from server %s", resp.Status)
} }
return body, err return body, err
} }