Compare commits

..

No commits in common. "main" and "v0.0.6" have entirely different histories.
main ... v0.0.6

9 changed files with 68 additions and 162 deletions

7
.vscode/launch.json vendored
View File

@ -1,7 +0,0 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}

View File

@ -8,14 +8,9 @@ import (
)
func (api *Api) GetApInfo(siteID model.SiteID, macAddress string) (*model.ApInfo, error) {
resp, err := api.getApInfo(siteID, macAddress)
if err != nil {
return nil, err
}
return &resp.Result, nil
}
api.refreshMutex.RLock()
defer api.refreshMutex.RUnlock()
func (api *Api) getApInfo(siteID model.SiteID, macAddress string) (*model.Response[model.ApInfo], error) {
req := ezhttp.Request(
ezhttp.Template(api.tmpl),
ezhttp.Method("GET"),
@ -26,7 +21,7 @@ func (api *Api) getApInfo(siteID model.SiteID, macAddress string) (*model.Respon
macAddress)),
)
resp, err := api.doRequest(req)
resp, err := ezhttp.Do(req)
if err != nil {
return nil, err
}
@ -37,7 +32,5 @@ func (api *Api) getApInfo(siteID model.SiteID, macAddress string) (*model.Respon
return nil, err
}
return handleResponseErrors(api, response, func() (*model.Response[model.ApInfo], error) {
return api.getApInfo(siteID, macAddress)
})
return &response.Result, nil
}

74
api.go
View File

@ -1,6 +1,7 @@
package omadaapi
import (
"context"
"fmt"
"net/http"
"sync"
@ -39,45 +40,31 @@ func NewApi(config ApiConfig) (*Api, error) {
config: config,
}
if err := api.InitSession(); err != nil {
return nil, err
}
return api, nil
}
func (api *Api) InitSession() error {
api.refreshMutex.Lock()
defer api.refreshMutex.Unlock()
return api.initSessionNoMutexLock()
}
func (api *Api) initSessionNoMutexLock() error {
loginResponse, err := api.Login()
if err != nil {
return fmt.Errorf("login request failed: %w", err)
return nil, fmt.Errorf("login request failed: %w", err)
}
if loginResponse.ErrorCode != 0 {
return fmt.Errorf("login request failed: %s", loginResponse.Message)
return nil, fmt.Errorf("login request failed: %s", loginResponse.Message)
}
authCodeResponse, err := api.AuthCode(loginResponse.Result.CsrfToken, loginResponse.Result.SessionID)
if err != nil {
return fmt.Errorf("auth code request failed: %w", err)
return nil, fmt.Errorf("auth code request failed: %w", err)
}
if authCodeResponse.ErrorCode != 0 {
return fmt.Errorf("auth code request failed: %s", authCodeResponse.Message)
return nil, fmt.Errorf("auth code request failed: %s", authCodeResponse.Message)
}
authTokenResponse, err := api.AuthToken(*authCodeResponse.Result)
if err != nil {
return fmt.Errorf("auth token request failed: %w", err)
return nil, fmt.Errorf("auth token request failed: %w", err)
}
if authTokenResponse.ErrorCode != 0 {
return fmt.Errorf("auth token request failed: %s", authTokenResponse.Message)
return nil, fmt.Errorf("auth token request failed: %s", authTokenResponse.Message)
}
api.expiration = time.Now().Add(time.Duration(authTokenResponse.Result.ExpiresIn) * time.Second)
@ -86,14 +73,16 @@ func (api *Api) initSessionNoMutexLock() error {
api.tmpl = ezhttp.Request(
ezhttp.Template(api.tmpl),
ezhttp.RemoveAllHeaders(),
ezhttp.Auth(api.getAuthHeader),
ezhttp.Headers("Authorization", "AccessToken="+api.accessToken),
)
return nil
return api, nil
}
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,
@ -122,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"),
@ -145,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"),
@ -171,6 +166,25 @@ func (api *Api) AuthToken(authCode string) (*model.AuthTokenResponse, error) {
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()
@ -198,23 +212,9 @@ func (api *Api) Refresh() error {
return err
}
if response.ErrorCode == ErrCodeRefreshTokenExpired {
return api.initSessionNoMutexLock()
}
api.expiration = time.Now().Add(time.Duration(response.Result.ExpiresIn) * time.Second)
api.accessToken = response.Result.AccessToken
api.refreshToken = response.Result.RefreshToken
return nil
}
func (api *Api) doRequest(r *http.Request) (*http.Response, error) {
api.refreshMutex.RLock()
defer api.refreshMutex.RUnlock()
return ezhttp.Do(r)
}
func (api *Api) getAuthHeader() string {
return "AccessToken=" + api.accessToken
}

View File

@ -4,13 +4,12 @@ import (
"fmt"
"strconv"
"git.tordarus.net/tordarus/channel"
"git.tordarus.net/tordarus/ezhttp"
"git.tordarus.net/tordarus/omada-api/model"
)
func (api *Api) GetClients(siteID model.SiteID) <-chan channel.Result[model.Client] {
out := make(chan channel.Result[model.Client], 1000)
func (api *Api) GetClients(siteID model.SiteID) <-chan *model.Client {
out := make(chan *model.Client, 1000)
go func() {
defer close(out)
@ -18,12 +17,11 @@ func (api *Api) GetClients(siteID model.SiteID) <-chan channel.Result[model.Clie
for page := 1; ; page++ {
resp, err := api.getClients(page, siteID)
if err != nil {
out <- channel.ResultOf[model.Client](nil, err)
return
}
for _, v := range resp.Result.Data {
out <- channel.ResultOfValue(v, nil)
out <- &v
}
if resp.Result.CurrentPage*resp.Result.CurrentSize >= resp.Result.TotalRows {
@ -36,6 +34,9 @@ func (api *Api) GetClients(siteID model.SiteID) <-chan channel.Result[model.Clie
}
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"),
@ -46,7 +47,7 @@ func (api *Api) getClients(page int, siteID model.SiteID) (*model.PagedResponse[
),
)
resp, err := api.doRequest(req)
resp, err := ezhttp.Do(req)
if err != nil {
return nil, err
}
@ -57,7 +58,5 @@ func (api *Api) getClients(page int, siteID model.SiteID) (*model.PagedResponse[
return nil, err
}
return handlePagedResponseErrors(api, response, func() (*model.PagedResponse[model.Client], error) {
return api.getClients(page, siteID)
})
return response, nil
}

View File

@ -4,13 +4,12 @@ import (
"fmt"
"strconv"
"git.tordarus.net/tordarus/channel"
"git.tordarus.net/tordarus/ezhttp"
"git.tordarus.net/tordarus/omada-api/model"
)
func (api *Api) GetDevices(siteID model.SiteID) <-chan channel.Result[model.Device] {
out := make(chan channel.Result[model.Device], 1000)
func (api *Api) GetDevices(siteID model.SiteID) <-chan *model.Device {
out := make(chan *model.Device, 1000)
go func() {
defer close(out)
@ -18,12 +17,11 @@ func (api *Api) GetDevices(siteID model.SiteID) <-chan channel.Result[model.Devi
for page := 1; ; page++ {
resp, err := api.getDevices(page, siteID)
if err != nil {
out <- channel.ResultOf[model.Device](nil, err)
return
}
for _, v := range resp.Result.Data {
out <- channel.ResultOfValue(v, nil)
out <- &v
}
if resp.Result.CurrentPage*resp.Result.CurrentSize >= resp.Result.TotalRows {
@ -36,6 +34,9 @@ func (api *Api) GetDevices(siteID model.SiteID) <-chan channel.Result[model.Devi
}
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"),
@ -46,7 +47,7 @@ func (api *Api) getDevices(page int, siteID model.SiteID) (*model.PagedResponse[
),
)
resp, err := api.doRequest(req)
resp, err := ezhttp.Do(req)
if err != nil {
return nil, err
}
@ -57,7 +58,5 @@ func (api *Api) getDevices(page int, siteID model.SiteID) (*model.PagedResponse[
return nil, err
}
return handlePagedResponseErrors(api, response, func() (*model.PagedResponse[model.Device], error) {
return api.getDevices(page, siteID)
})
return response, nil
}

5
go.mod
View File

@ -2,7 +2,4 @@ module git.tordarus.net/tordarus/omada-api
go 1.23.0
require (
git.tordarus.net/tordarus/channel v0.1.19
git.tordarus.net/tordarus/ezhttp v0.0.9
)
require git.tordarus.net/tordarus/ezhttp v0.0.5

6
go.sum
View File

@ -1,4 +1,2 @@
git.tordarus.net/tordarus/channel v0.1.19 h1:d9xnSwFyvBh4B1/82mt0A7Gpm2nIZJTc+9ceJMIOu5Q=
git.tordarus.net/tordarus/channel v0.1.19/go.mod h1:8/dWFTdGO7g4AeSZ7cF6GerkGbe9c4dBVMVDBxOd9m4=
git.tordarus.net/tordarus/ezhttp v0.0.9 h1:YwdQ4YcJwvpMw5CX5NcCEM23XQL+WCz5nWuc2dzX/84=
git.tordarus.net/tordarus/ezhttp v0.0.9/go.mod h1:Zq9o0Hibny61GqSCwJHa0PfGjVoUFv/zt2PjiQHXvmY=
git.tordarus.net/tordarus/ezhttp v0.0.5 h1:pxfEdfDeOHT/ATXYy5OQHmeBIho121SBuFvU4ISQ7w0=
git.tordarus.net/tordarus/ezhttp v0.0.5/go.mod h1:Zq9o0Hibny61GqSCwJHa0PfGjVoUFv/zt2PjiQHXvmY=

17
site.go
View File

@ -4,13 +4,12 @@ import (
"fmt"
"strconv"
"git.tordarus.net/tordarus/channel"
"git.tordarus.net/tordarus/ezhttp"
"git.tordarus.net/tordarus/omada-api/model"
)
func (api *Api) GetSites() <-chan channel.Result[model.Site] {
out := make(chan channel.Result[model.Site], 1000)
func (api *Api) GetSites() <-chan *model.Site {
out := make(chan *model.Site, 1000)
go func() {
defer close(out)
@ -18,12 +17,11 @@ func (api *Api) GetSites() <-chan channel.Result[model.Site] {
for page := 1; ; page++ {
resp, err := api.getSites(page)
if err != nil {
out <- channel.ResultOf[model.Site](nil, err)
return
}
for _, v := range resp.Result.Data {
out <- channel.ResultOfValue(v, nil)
out <- &v
}
if resp.Result.CurrentPage*resp.Result.CurrentSize >= resp.Result.TotalRows {
@ -36,6 +34,9 @@ func (api *Api) GetSites() <-chan channel.Result[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"),
@ -46,7 +47,7 @@ func (api *Api) getSites(page int) (*model.PagedResponse[model.Site], error) {
),
)
resp, err := api.doRequest(req)
resp, err := ezhttp.Do(req)
if err != nil {
return nil, err
}
@ -57,7 +58,5 @@ func (api *Api) getSites(page int) (*model.PagedResponse[model.Site], error) {
return nil, err
}
return handlePagedResponseErrors(api, response, func() (*model.PagedResponse[model.Site], error) {
return api.getSites(page)
})
return response, nil
}

View File

@ -1,72 +0,0 @@
package omadaapi
import (
"fmt"
"git.tordarus.net/tordarus/omada-api/model"
)
const ErrCodeAccessTokenExpired = -44112
const ErrCodeRefreshTokenExpired = -44114
func handleResponseErrors[T any](api *Api, response *model.Response[T], retry func() (*model.Response[T], error)) (*model.Response[T], error) {
switch response.ErrorCode {
case 0:
return response, nil
case ErrCodeAccessTokenExpired:
if err := api.Refresh(); err != nil {
return nil, fmt.Errorf("could not refresh access token: %w", err)
}
newResp, err := retry()
if err != nil {
return nil, err
}
return handleResponseErrors(api, newResp, retry)
case ErrCodeRefreshTokenExpired:
if err := api.InitSession(); err != nil {
return nil, fmt.Errorf("could not initialize new session: %w", err)
}
newResp, err := retry()
if err != nil {
return nil, err
}
return handleResponseErrors(api, newResp, retry)
default:
return nil, fmt.Errorf("invalid error code %d with message: %s", response.ErrorCode, response.Message)
}
}
func handlePagedResponseErrors[T any](api *Api, response *model.PagedResponse[T], retry func() (*model.PagedResponse[T], error)) (*model.PagedResponse[T], error) {
switch response.ErrorCode {
case 0:
return response, nil
case ErrCodeAccessTokenExpired:
if err := api.Refresh(); err != nil {
return nil, fmt.Errorf("could not refresh access token: %w", err)
}
newResp, err := retry()
if err != nil {
return nil, err
}
return handlePagedResponseErrors(api, newResp, retry)
case ErrCodeRefreshTokenExpired:
if err := api.InitSession(); err != nil {
return nil, fmt.Errorf("could not initialize new session: %w", err)
}
newResp, err := retry()
if err != nil {
return nil, err
}
return handlePagedResponseErrors(api, newResp, retry)
default:
return nil, fmt.Errorf("invalid error code %d with message: %s", response.ErrorCode, response.Message)
}
}