From 94bc27fd285c8e18a6e1a01f413e600f5ec0cd1c Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sat, 26 Jan 2019 04:25:23 -0800 Subject: [PATCH] more minor fixes. --- core/unifi/README.md | 4 ++-- core/unifi/unidev.go | 8 +++++--- core/unifi/unifi.go | 18 +++++++++--------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/core/unifi/README.md b/core/unifi/README.md index 58daebf3..8b8c4a30 100644 --- a/core/unifi/README.md +++ b/core/unifi/README.md @@ -28,7 +28,7 @@ func main() { // Log with log.Printf or make your own interface that accepts (msg, fmt) uni.ErrorLog = log.Printf uni.DebugLog = log.Printf - clients, err := uni.GetUnifiClients() + clients, err := uni.GetClients() if err != nil { log.Fatalln("Error:", err) } @@ -36,7 +36,7 @@ func main() { for i, client := range clients { log.Println(i+1, client.ID, client.Hostname, client.IP, client.Name, client.LastSeen) } - devices, err := uni.GetUnifiDevices() + devices, err := uni.GetDevices() if err != nil { log.Fatalln("Error:", err) } diff --git a/core/unifi/unidev.go b/core/unifi/unidev.go index 520be0a5..ba28b895 100644 --- a/core/unifi/unidev.go +++ b/core/unifi/unidev.go @@ -24,7 +24,7 @@ const ( LoginPath = "/api/login" ) -// Logger is a base type to deal with changing log outs. +// Logger is a base type to deal with changing log outputs. type Logger func(msg string, fmt ...interface{}) // Devices contains a list of all the unifi devices from a controller. @@ -69,9 +69,9 @@ func (f *FlexInt) UnmarshalJSON(b []byte) error { } } -// AuthController creates a http.Client with authenticated cookies. +// GetController creates a http.Client with authenticated cookies. // Used to make additional, authenticated requests to the APIs. -func AuthController(user, pass, url string, verifySSL bool) (*Unifi, error) { +func GetController(user, pass, url string, verifySSL bool) (*Unifi, error) { json := `{"username": "` + user + `","password": "` + pass + `"}` jar, err := cookiejar.New(nil) if err != nil { @@ -103,6 +103,8 @@ func AuthController(user, pass, url string, verifySSL bool) (*Unifi, error) { } // UniReq is a small helper function that adds an Accept header. +// Use this if you're unmarshalling Unifi data into custom types. +// And you're doing that... sumbut a pull request with your new struct. :) 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)) diff --git a/core/unifi/unifi.go b/core/unifi/unifi.go index 92893292..5c2645af 100644 --- a/core/unifi/unifi.go +++ b/core/unifi/unifi.go @@ -7,8 +7,8 @@ import ( "github.com/pkg/errors" ) -// GetUnifiClients returns a response full of clients' data from the Unifi Controller. -func (u *Unifi) GetUnifiClients() ([]UCL, error) { +// GetClients returns a response full of clients' data from the Unifi Controller. +func (u *Unifi) GetClients() ([]UCL, error) { var response struct { Clients []UCL `json:"data"` } @@ -31,8 +31,8 @@ func (u *Unifi) GetUnifiClients() ([]UCL, error) { return response.Clients, nil } -// GetUnifiDevices returns a response full of devices' data from the Unifi Controller. -func (u *Unifi) GetUnifiDevices() (*Devices, error) { +// GetDevices returns a response full of devices' data from the Unifi Controller. +func (u *Unifi) GetDevices() (*Devices, error) { var parsed struct { Data []json.RawMessage `json:"data"` } @@ -52,11 +52,11 @@ func (u *Unifi) GetUnifiDevices() (*Devices, error) { } else if err = json.Unmarshal(body, &parsed); err != nil { return nil, errors.Wrap(err, "json.Unmarshal([]json.RawMessage)") } - return u.parseUnifiDevices(parsed.Data), nil + return u.parseDevices(parsed.Data), nil } -// parseUnifiDevices parses the raw JSON from the Unifi Controller into device structures. -func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices { +// parseDevices parses the raw JSON from the Unifi Controller into device structures. +func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { devices := new(Devices) // Loop each item in the raw JSON message, detect its type and unmarshal it. for i, r := range data { @@ -73,7 +73,7 @@ func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices { if t, ok := obj["type"].(string); ok { assetType = t } - u.dLogf("Unmarshalling Device Type:", assetType) + u.dLogf("Unmarshalling Device Type: %v", assetType) // Unmarshal again into the correct type.. switch assetType { case "uap": @@ -95,7 +95,7 @@ func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices { } devices.USWs = append(devices.USWs, usw) default: - u.dLogf("unknown asset type -", assetType, "- skipping") + u.dLogf("unknown asset type - " + assetType + " - skipping") continue } }