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)
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)
}

View File

@ -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))

View File

@ -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
}
}