ListApplications returns all applications for the authenticated user, following pagination to fetch every page.
(accessToken string)
| 224 | // ListApplications returns all applications for the authenticated user, |
| 225 | // following pagination to fetch every page. |
| 226 | func (c *Client) ListApplications(accessToken string) ([]Application, error) { |
| 227 | var allApps []Application |
| 228 | page := 1 |
| 229 | |
| 230 | for { |
| 231 | endpoint := fmt.Sprintf("%s/1/applications?page=%d", c.APIURL, page) |
| 232 | req, err := http.NewRequest(http.MethodGet, endpoint, nil) |
| 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | c.setAPIHeaders(req, accessToken) |
| 237 | |
| 238 | resp, err := c.client.Do(req) |
| 239 | if err != nil { |
| 240 | return nil, fmt.Errorf("list applications request failed: %w", err) |
| 241 | } |
| 242 | defer resp.Body.Close() |
| 243 | |
| 244 | if resp.StatusCode == http.StatusUnauthorized { |
| 245 | return nil, ErrSessionExpired |
| 246 | } |
| 247 | |
| 248 | if resp.StatusCode != http.StatusOK { |
| 249 | return nil, fmt.Errorf("list applications failed with status: %d", resp.StatusCode) |
| 250 | } |
| 251 | |
| 252 | var appsResp ApplicationsResponse |
| 253 | if err := json.NewDecoder(resp.Body).Decode(&appsResp); err != nil { |
| 254 | return nil, fmt.Errorf("failed to parse applications response: %w", err) |
| 255 | } |
| 256 | |
| 257 | for i := range appsResp.Data { |
| 258 | allApps = append(allApps, appsResp.Data[i].toApplication()) |
| 259 | } |
| 260 | |
| 261 | if appsResp.Meta.CurrentPage >= appsResp.Meta.TotalPages { |
| 262 | break |
| 263 | } |
| 264 | page++ |
| 265 | } |
| 266 | |
| 267 | return allApps, nil |
| 268 | } |
| 269 | |
| 270 | // GetApplication returns a single application by its ID. |
| 271 | func (c *Client) GetApplication(accessToken, appID string) (*Application, error) { |