@@ -204,39 +204,50 @@ func (c *Client) RevokeToken(token string) error {
204204 return nil
205205}
206206
207- // ListApplications returns all applications for the authenticated user.
207+ // ListApplications returns all applications for the authenticated user,
208+ // following pagination to fetch every page.
208209func (c * Client ) ListApplications (accessToken string ) ([]Application , error ) {
209- req , err := http .NewRequest (http .MethodGet , c .APIURL + "/1/applications" , nil )
210- if err != nil {
211- return nil , err
212- }
213- c .setAPIHeaders (req , accessToken )
210+ var allApps []Application
211+ page := 1
212+
213+ for {
214+ endpoint := fmt .Sprintf ("%s/1/applications?page=%d" , c .APIURL , page )
215+ req , err := http .NewRequest (http .MethodGet , endpoint , nil )
216+ if err != nil {
217+ return nil , err
218+ }
219+ c .setAPIHeaders (req , accessToken )
214220
215- resp , err := c .client .Do (req )
216- if err != nil {
217- return nil , fmt .Errorf ("list applications request failed: %w" , err )
218- }
219- defer resp .Body .Close ()
221+ resp , err := c .client .Do (req )
222+ if err != nil {
223+ return nil , fmt .Errorf ("list applications request failed: %w" , err )
224+ }
225+ defer resp .Body .Close ()
220226
221- if resp .StatusCode == http .StatusUnauthorized {
222- return nil , ErrSessionExpired
223- }
227+ if resp .StatusCode == http .StatusUnauthorized {
228+ return nil , ErrSessionExpired
229+ }
224230
225- if resp .StatusCode != http .StatusOK {
226- return nil , fmt .Errorf ("list applications failed with status: %d" , resp .StatusCode )
227- }
231+ if resp .StatusCode != http .StatusOK {
232+ return nil , fmt .Errorf ("list applications failed with status: %d" , resp .StatusCode )
233+ }
228234
229- var appsResp ApplicationsResponse
230- if err := json .NewDecoder (resp .Body ).Decode (& appsResp ); err != nil {
231- return nil , fmt .Errorf ("failed to parse applications response: %w" , err )
232- }
235+ var appsResp ApplicationsResponse
236+ if err := json .NewDecoder (resp .Body ).Decode (& appsResp ); err != nil {
237+ return nil , fmt .Errorf ("failed to parse applications response: %w" , err )
238+ }
233239
234- apps := make ([]Application , len (appsResp .Data ))
235- for i := range appsResp .Data {
236- apps [i ] = appsResp .Data [i ].toApplication ()
240+ for i := range appsResp .Data {
241+ allApps = append (allApps , appsResp .Data [i ].toApplication ())
242+ }
243+
244+ if appsResp .Meta .CurrentPage >= appsResp .Meta .TotalPages {
245+ break
246+ }
247+ page ++
237248 }
238249
239- return apps , nil
250+ return allApps , nil
240251}
241252
242253// GetApplication returns a single application by its ID.
0 commit comments