Create logic to handle sessions in Unifi API
This commit is contained in:
@@ -15,8 +15,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type UnifiClient struct {
|
type UnifiClient struct {
|
||||||
Client *unifi.Client
|
Client *unifi.Client
|
||||||
SiteID string
|
SiteID string
|
||||||
|
mutex sync.Mutex
|
||||||
|
controller string
|
||||||
|
username string
|
||||||
|
password string
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateUnifiClient() (*UnifiClient, error) {
|
func CreateUnifiClient() (*UnifiClient, error) {
|
||||||
@@ -64,9 +68,42 @@ func CreateUnifiClient() (*UnifiClient, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
unifiClient := &UnifiClient{
|
unifiClient := &UnifiClient{
|
||||||
Client: client,
|
Client: client,
|
||||||
SiteID: siteID,
|
SiteID: siteID,
|
||||||
|
controller: unifiURL,
|
||||||
|
username: username,
|
||||||
|
password: password,
|
||||||
}
|
}
|
||||||
|
|
||||||
return unifiClient, nil
|
return unifiClient, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Session) WithSession(action func(c *unifi.Client) error) error {
|
||||||
|
s.mutex.Lock()
|
||||||
|
defer s.mutex.Unlock()
|
||||||
|
|
||||||
|
err := action(s.client)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsSessionExpired(err) {
|
||||||
|
if loginErr := s.Client.Login(context.Background(), s.username, s.password); loginErr != nil {
|
||||||
|
return fmt.Errorf("re-login to Unifi failed: %w", loginErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
return action(s.Client)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isSessionExpired(err error) bool {
|
||||||
|
if err == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
msg := strings.ToLower(err.Error())
|
||||||
|
return strings.Contains(msg, "unauthorized") ||
|
||||||
|
strings.Contains(msg, "authentication") ||
|
||||||
|
strings.Contains(msg, "login required") ||
|
||||||
|
strings.Contains(msg, "token")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user