Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
3949597e01 | |||
f732d0ed44 | |||
b320c395df |
@ -27,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
|
||||
}
|
||||
|
24
api.go
24
api.go
@ -67,7 +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-1) * time.Second)
|
||||
api.expiration = time.Now().Add(time.Duration(authTokenResponse.Result.ExpiresIn) * time.Second)
|
||||
api.accessToken = authTokenResponse.Result.AccessToken
|
||||
api.refreshToken = authTokenResponse.Result.RefreshToken
|
||||
|
||||
@ -102,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
|
||||
}
|
||||
@ -128,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
|
||||
}
|
||||
@ -158,7 +158,7 @@ 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
|
||||
}
|
||||
@ -166,12 +166,18 @@ func (api *Api) AuthToken(authCode string) (*model.AuthTokenResponse, error) {
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func (api *Api) AutoRefresh(ctx context.Context) error {
|
||||
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)):
|
||||
case <-time.After(time.Until(api.expiration.Add(-refreshBeforeExpiration))):
|
||||
if err := api.Refresh(); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -180,7 +186,6 @@ func (api *Api) AutoRefresh(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (api *Api) Refresh() error {
|
||||
fmt.Println("refresh")
|
||||
api.refreshMutex.Lock()
|
||||
defer api.refreshMutex.Unlock()
|
||||
|
||||
@ -202,15 +207,14 @@ func (api *Api) Refresh() error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthTokenResponse](resp.Body)
|
||||
response, err := ezhttp.ParseJsonResponse[model.AuthTokenResponse](resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
api.expiration = time.Now().Add(time.Duration(response.Result.ExpiresIn-1) * time.Second)
|
||||
api.expiration = time.Now().Add(time.Duration(response.Result.ExpiresIn) * time.Second)
|
||||
api.accessToken = response.Result.AccessToken
|
||||
api.refreshToken = response.Result.RefreshToken
|
||||
|
||||
fmt.Println("refresh successful")
|
||||
return nil
|
||||
}
|
||||
|
@ -53,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
|
||||
}
|
||||
|
@ -53,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=
|
||||
|
2
site.go
2
site.go
@ -53,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