Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
3949597e01 | |||
f732d0ed44 | |||
b320c395df | |||
921138f450 |
@ -8,6 +8,9 @@ import (
|
||||
)
|
||||
|
||||
func (api *Api) GetApInfo(siteID model.SiteID, macAddress string) (*model.ApInfo, error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("GET"),
|
||||
@ -24,7 +27,7 @@ func (api *Api) GetApInfo(siteID model.SiteID, macAddress string) (*model.ApInfo
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.Response[model.ApInfo]](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.Response[model.ApInfo]](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
75
api.go
75
api.go
@ -1,8 +1,11 @@
|
||||
package omadaapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.tordarus.net/tordarus/ezhttp"
|
||||
"git.tordarus.net/tordarus/omada-api/model"
|
||||
@ -15,6 +18,9 @@ type Api struct {
|
||||
|
||||
accessToken string
|
||||
refreshToken string
|
||||
|
||||
expiration time.Time
|
||||
refreshMutex sync.RWMutex
|
||||
}
|
||||
|
||||
type ApiConfig struct {
|
||||
@ -61,6 +67,7 @@ func NewApi(config ApiConfig) (*Api, error) {
|
||||
return nil, fmt.Errorf("auth token request failed: %s", authTokenResponse.Message)
|
||||
}
|
||||
|
||||
api.expiration = time.Now().Add(time.Duration(authTokenResponse.Result.ExpiresIn) * time.Second)
|
||||
api.accessToken = authTokenResponse.Result.AccessToken
|
||||
api.refreshToken = authTokenResponse.Result.RefreshToken
|
||||
|
||||
@ -73,6 +80,9 @@ func NewApi(config ApiConfig) (*Api, error) {
|
||||
}
|
||||
|
||||
func (api *Api) Login() (*model.LoginResponse, error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
reqBody := model.LoginRequest{
|
||||
Username: api.config.Username,
|
||||
Password: api.config.Password,
|
||||
@ -92,7 +102,7 @@ func (api *Api) Login() (*model.LoginResponse, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.LoginResponse](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.LoginResponse](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -101,6 +111,9 @@ func (api *Api) Login() (*model.LoginResponse, error) {
|
||||
}
|
||||
|
||||
func (api *Api) AuthCode(csrfToken, sessionID string) (*model.AuthCodeResponse, error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("POST"),
|
||||
@ -115,7 +128,7 @@ func (api *Api) AuthCode(csrfToken, sessionID string) (*model.AuthCodeResponse,
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthCodeResponse](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthCodeResponse](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -124,6 +137,9 @@ func (api *Api) AuthCode(csrfToken, sessionID string) (*model.AuthCodeResponse,
|
||||
}
|
||||
|
||||
func (api *Api) AuthToken(authCode string) (*model.AuthTokenResponse, error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("POST"),
|
||||
@ -142,10 +158,63 @@ func (api *Api) AuthToken(authCode string) (*model.AuthTokenResponse, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthTokenResponse](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthTokenResponse](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (api *Api) MustAutoRefresh(ctx context.Context, refreshBeforeExpiration time.Duration) {
|
||||
if err := api.AutoRefresh(ctx, refreshBeforeExpiration); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (api *Api) AutoRefresh(ctx context.Context, refreshBeforeExpiration time.Duration) error {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-time.After(time.Until(api.expiration.Add(-refreshBeforeExpiration))):
|
||||
if err := api.Refresh(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (api *Api) Refresh() error {
|
||||
api.refreshMutex.Lock()
|
||||
defer api.refreshMutex.Unlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("POST"),
|
||||
ezhttp.AppendPath("/openapi/authorize/token"),
|
||||
ezhttp.Query(
|
||||
"client_id", api.config.ClientID,
|
||||
"client_secret", api.config.ClientSecret,
|
||||
"refresh_token", api.refreshToken,
|
||||
"grant_type", "refresh_token",
|
||||
),
|
||||
)
|
||||
|
||||
resp, err := ezhttp.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthTokenResponse](resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api.expiration = time.Now().Add(time.Duration(response.Result.ExpiresIn) * time.Second)
|
||||
api.accessToken = response.Result.AccessToken
|
||||
api.refreshToken = response.Result.RefreshToken
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -34,6 +34,9 @@ func (api *Api) GetClients(siteID model.SiteID) <-chan *model.Client {
|
||||
}
|
||||
|
||||
func (api *Api) getClients(page int, siteID model.SiteID) (*model.PagedResponse[model.Client], error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("GET"),
|
||||
@ -50,7 +53,7 @@ func (api *Api) getClients(page int, siteID model.SiteID) (*model.PagedResponse[
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Client]](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Client]](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -34,6 +34,9 @@ func (api *Api) GetDevices(siteID model.SiteID) <-chan *model.Device {
|
||||
}
|
||||
|
||||
func (api *Api) getDevices(page int, siteID model.SiteID) (*model.PagedResponse[model.Device], error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("GET"),
|
||||
@ -50,7 +53,7 @@ func (api *Api) getDevices(page int, siteID model.SiteID) (*model.PagedResponse[
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Device]](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Device]](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module git.tordarus.net/tordarus/omada-api
|
||||
|
||||
go 1.23.0
|
||||
|
||||
require git.tordarus.net/tordarus/ezhttp v0.0.3
|
||||
require git.tordarus.net/tordarus/ezhttp v0.0.4
|
||||
|
4
go.sum
4
go.sum
@ -1,2 +1,2 @@
|
||||
git.tordarus.net/tordarus/ezhttp v0.0.3 h1:K6IlLmqkAFUF68HJsOTKcP3ejco7qfm+MuEagohoouo=
|
||||
git.tordarus.net/tordarus/ezhttp v0.0.3/go.mod h1:Zq9o0Hibny61GqSCwJHa0PfGjVoUFv/zt2PjiQHXvmY=
|
||||
git.tordarus.net/tordarus/ezhttp v0.0.4 h1:wm7rhol9MHZTZXOzH5I6Q4idjCiFfA4XwAMISZhTcCs=
|
||||
git.tordarus.net/tordarus/ezhttp v0.0.4/go.mod h1:Zq9o0Hibny61GqSCwJHa0PfGjVoUFv/zt2PjiQHXvmY=
|
||||
|
5
site.go
5
site.go
@ -34,6 +34,9 @@ func (api *Api) GetSites() <-chan *model.Site {
|
||||
}
|
||||
|
||||
func (api *Api) getSites(page int) (*model.PagedResponse[model.Site], error) {
|
||||
api.refreshMutex.RLock()
|
||||
defer api.refreshMutex.RUnlock()
|
||||
|
||||
req := ezhttp.Request(
|
||||
ezhttp.Template(api.tmpl),
|
||||
ezhttp.Method("GET"),
|
||||
@ -50,7 +53,7 @@ func (api *Api) getSites(page int) (*model.PagedResponse[model.Site], error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Site]](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Site]](resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user