2025-02-01 17:38:29 +01:00
|
|
|
package omadaapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.tordarus.net/tordarus/ezhttp"
|
|
|
|
"git.tordarus.net/tordarus/omada-api/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (api *Api) GetClients(siteID model.SiteID) <-chan *model.Client {
|
|
|
|
out := make(chan *model.Client, 1000)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(out)
|
|
|
|
|
|
|
|
for page := 1; ; page++ {
|
|
|
|
resp, err := api.getClients(page, siteID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range resp.Result.Data {
|
|
|
|
out <- &v
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Result.CurrentPage*resp.Result.CurrentSize >= resp.Result.TotalRows {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *Api) getClients(page int, siteID model.SiteID) (*model.PagedResponse[model.Client], error) {
|
2025-02-02 10:54:39 +01:00
|
|
|
api.refreshMutex.RLock()
|
|
|
|
defer api.refreshMutex.RUnlock()
|
|
|
|
|
2025-02-01 17:38:29 +01:00
|
|
|
req := ezhttp.Request(
|
|
|
|
ezhttp.Template(api.tmpl),
|
|
|
|
ezhttp.Method("GET"),
|
|
|
|
ezhttp.AppendPath(fmt.Sprintf("/openapi/v1/%s/sites/%s/clients", api.config.OmadaID, siteID)),
|
|
|
|
ezhttp.Query(
|
|
|
|
"page", strconv.Itoa(page),
|
|
|
|
"pageSize", "100",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
resp, err := ezhttp.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2025-02-02 20:03:47 +01:00
|
|
|
response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Client]](resp)
|
2025-02-01 17:38:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|