package omadaapi 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) go func() { defer close(out) 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) } if resp.Result.CurrentPage*resp.Result.CurrentSize >= resp.Result.TotalRows { break } } }() return out } func (api *Api) getSites(page int) (*model.PagedResponse[model.Site], error) { api.refreshMutex.RLock() req := ezhttp.Request( ezhttp.Template(api.tmpl), ezhttp.Method("GET"), ezhttp.AppendPath(fmt.Sprintf("/openapi/v1/%s/sites", api.config.OmadaID)), ezhttp.Query( "page", strconv.Itoa(page), "pageSize", "100", ), ) resp, err := ezhttp.Do(req) if err != nil { api.refreshMutex.RUnlock() return nil, err } defer resp.Body.Close() api.refreshMutex.RUnlock() response, err := ezhttp.ParseJsonResponse[model.PagedResponse[model.Site]](resp) if err != nil { return nil, err } return handlePagedResponseErrors(api, response, func() (*model.PagedResponse[model.Site], error) { return api.getSites(page) }) }