more minor fixes.

This commit is contained in:
David Newhall II 2019-01-26 04:25:23 -08:00
parent 4ef35f25f2
commit 94bc27fd28
3 changed files with 16 additions and 14 deletions

View File

@ -28,7 +28,7 @@ func main() {
// Log with log.Printf or make your own interface that accepts (msg, fmt) // Log with log.Printf or make your own interface that accepts (msg, fmt)
uni.ErrorLog = log.Printf uni.ErrorLog = log.Printf
uni.DebugLog = log.Printf uni.DebugLog = log.Printf
clients, err := uni.GetUnifiClients() clients, err := uni.GetClients()
if err != nil { if err != nil {
log.Fatalln("Error:", err) log.Fatalln("Error:", err)
} }
@ -36,7 +36,7 @@ func main() {
for i, client := range clients { for i, client := range clients {
log.Println(i+1, client.ID, client.Hostname, client.IP, client.Name, client.LastSeen) 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 { if err != nil {
log.Fatalln("Error:", err) log.Fatalln("Error:", err)
} }

View File

@ -24,7 +24,7 @@ const (
LoginPath = "/api/login" 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{}) type Logger func(msg string, fmt ...interface{})
// Devices contains a list of all the unifi devices from a controller. // 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. // 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 + `"}` json := `{"username": "` + user + `","password": "` + pass + `"}`
jar, err := cookiejar.New(nil) jar, err := cookiejar.New(nil)
if err != 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. // 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) { func (u *Unifi) UniReq(apiPath string, params string) (req *http.Request, err error) {
if params != "" { if params != "" {
req, err = http.NewRequest("POST", u.baseURL+apiPath, bytes.NewBufferString(params)) req, err = http.NewRequest("POST", u.baseURL+apiPath, bytes.NewBufferString(params))

View File

@ -7,8 +7,8 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
// GetUnifiClients returns a response full of clients' data from the Unifi Controller. // GetClients returns a response full of clients' data from the Unifi Controller.
func (u *Unifi) GetUnifiClients() ([]UCL, error) { func (u *Unifi) GetClients() ([]UCL, error) {
var response struct { var response struct {
Clients []UCL `json:"data"` Clients []UCL `json:"data"`
} }
@ -31,8 +31,8 @@ func (u *Unifi) GetUnifiClients() ([]UCL, error) {
return response.Clients, nil return response.Clients, nil
} }
// GetUnifiDevices returns a response full of devices' data from the Unifi Controller. // GetDevices returns a response full of devices' data from the Unifi Controller.
func (u *Unifi) GetUnifiDevices() (*Devices, error) { func (u *Unifi) GetDevices() (*Devices, error) {
var parsed struct { var parsed struct {
Data []json.RawMessage `json:"data"` Data []json.RawMessage `json:"data"`
} }
@ -52,11 +52,11 @@ func (u *Unifi) GetUnifiDevices() (*Devices, error) {
} else if err = json.Unmarshal(body, &parsed); err != nil { } else if err = json.Unmarshal(body, &parsed); err != nil {
return nil, errors.Wrap(err, "json.Unmarshal([]json.RawMessage)") 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. // parseDevices parses the raw JSON from the Unifi Controller into device structures.
func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices { func (u *Unifi) parseDevices(data []json.RawMessage) *Devices {
devices := new(Devices) devices := new(Devices)
// Loop each item in the raw JSON message, detect its type and unmarshal it. // Loop each item in the raw JSON message, detect its type and unmarshal it.
for i, r := range data { for i, r := range data {
@ -73,7 +73,7 @@ func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices {
if t, ok := obj["type"].(string); ok { if t, ok := obj["type"].(string); ok {
assetType = t assetType = t
} }
u.dLogf("Unmarshalling Device Type:", assetType) u.dLogf("Unmarshalling Device Type: %v", assetType)
// Unmarshal again into the correct type.. // Unmarshal again into the correct type..
switch assetType { switch assetType {
case "uap": case "uap":
@ -95,7 +95,7 @@ func (u *Unifi) parseUnifiDevices(data []json.RawMessage) *Devices {
} }
devices.USWs = append(devices.USWs, usw) devices.USWs = append(devices.USWs, usw)
default: default:
u.dLogf("unknown asset type -", assetType, "- skipping") u.dLogf("unknown asset type - " + assetType + " - skipping")
continue continue
} }
} }