Skip to content

Commit

Permalink
Merge pull request #86 from unpoller/mock-plug-in-status-and-logging
Browse files Browse the repository at this point in the history
plug in mock status response and improved logging
  • Loading branch information
platinummonkey committed Jul 21, 2023
2 parents a624631 + b044de1 commit 7f053ce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
6 changes: 3 additions & 3 deletions mocks/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,16 @@ func (m *MockUnifi) Logout() error {

// GetServerData sets the controller's version and UUID. Only call this if you
// previously called Login and suspect the controller version has changed.
func (m *MockUnifi) GetServerData() error {
func (m *MockUnifi) GetServerData() (*unifi.ServerStatus, error) {
var response struct {
Data unifi.ServerStatus `json:"meta"`
}

err := m.faker.Struct(&response)
if err != nil {
return err
return nil, err
}
return nil
return &response.Data, nil
}

// GetUsers returns a response full of clients that connected to the UDM within the provided amount of time
Expand Down
9 changes: 7 additions & 2 deletions mocks/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mocks
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"regexp"
Expand Down Expand Up @@ -54,6 +55,7 @@ var (
)

func respondResultOrErr(w http.ResponseWriter, v any, err error) {
log.Printf("[DEBUG] Answering mock response err=%+v value=%+v\n", err, v)
if err != nil {
b, _ := json.Marshal(err)
w.WriteHeader(500)
Expand All @@ -67,14 +69,16 @@ func respondResultOrErr(w http.ResponseWriter, v any, err error) {

func (m *MockHTTPTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
p := strings.TrimSpace(r.URL.Path)
log.Printf("[DEBUG] Received mock request path=%s\n", p)
switch {
case apiRogueAP.MatchString(p):
aps, err := m.mocked.GetRogueAPs(nil)
respondResultOrErr(w, aps, err)
return
case apiStatusPath.MatchString(p):

// todo
s, err := m.mocked.GetServerData()
respondResultOrErr(w, s, err)
return
case apiEventPath.MatchString(p):
events, err := m.mocked.GetEvents(nil, time.Hour)
respondResultOrErr(w, events, err)
Expand Down Expand Up @@ -141,6 +145,7 @@ func (m *MockHTTPTestServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(501)
return
default:
log.Println("[DEBUG] Answering mock response err=404 not found")
http.NotFoundHandler().ServeHTTP(w, r)
return
}
Expand Down
2 changes: 1 addition & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ type UnifiClient interface {
Logout() error
// GetServerData sets the controller's version and UUID. Only call this if you
// previously called Login and suspect the controller version has changed.
GetServerData() error
GetServerData() (*ServerStatus, error)
// GetUsers returns a response full of clients that connected to the UDM within the provided amount of time
// using the insight historical connection data set.
GetUsers(sites []*Site, hours int) ([]*User, error)
Expand Down
11 changes: 8 additions & 3 deletions unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewUnifi(config *Config) (*Unifi, error) {
return u, err
}

if err := u.GetServerData(); err != nil {
if _, err := u.GetServerData(); err != nil {
return u, fmt.Errorf("unable to get server version: %w", err)
}

Expand Down Expand Up @@ -201,14 +201,19 @@ func (u *Unifi) checkNewStyleAPI() error {

// GetServerData sets the controller's version and UUID. Only call this if you
// previously called Login and suspect the controller version has changed.
func (u *Unifi) GetServerData() error {
func (u *Unifi) GetServerData() (*ServerStatus, error) {
var response struct {
Data ServerStatus `json:"meta"`
}

err := u.GetData(APIStatusPath, &response)
if err != nil {
return nil, err
}

u.ServerStatus = &response.Data

return u.GetData(APIStatusPath, &response)
return u.ServerStatus, nil
}

// GetData makes a unifi request and unmarshals the response into a provided pointer.
Expand Down

0 comments on commit 7f053ce

Please sign in to comment.