Reauthenticate method plus workaround for delete

This commit is contained in:
2025-04-10 13:45:51 +02:00
parent 1306939d58
commit a1ed82258c
3 changed files with 73 additions and 11 deletions

View File

@@ -7,11 +7,11 @@ import (
"crypto/tls"
"errors"
"fmt"
"sync"
"strings"
"net/http"
"net/http/cookiejar"
"os"
"strings"
"sync"
"github.com/vegardengen/go-unifi/unifi"
)
@@ -99,6 +99,20 @@ func (s *UnifiClient) WithSession(action func(c *unifi.Client) error) error {
return err
}
func (uClient *UnifiClient) Reauthenticate() error {
_, err := uClient.Client.ListSites(context.Background())
if err == nil {
return nil
}
if IsSessionExpired(err) {
if loginErr := uClient.Client.Login(context.Background(), uClient.username, uClient.password); loginErr != nil {
return fmt.Errorf("re-login to Unifi failed: %w", loginErr)
}
}
return nil
}
func IsSessionExpired(err error) bool {
if err == nil {
return false