Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ListIncidents pagination #238

Closed
atmosx opened this issue Sep 1, 2020 · 2 comments
Closed

ListIncidents pagination #238

atmosx opened this issue Sep 1, 2020 · 2 comments

Comments

@atmosx
Copy link

atmosx commented Sep 1, 2020

Hello! I need to fetch alerts since the beginning of the year. I can't handle pagination. Keeping the default limit of 25 results per page, here's a code sample:

package main

import (
	"github.com/PagerDuty/go-pagerduty"
	"os"
	"strings"
	"fmt"
)

var authtoken = os.Getenv("TOKEN")

func main() {
	var opts pagerduty.ListIncidentsOptions
	opts.Since = "2020-01-01T00:00:00.00-0000"
	client := pagerduty.NewClient(authtoken)
	alerts, err := client.ListIncidents(opts)
	if err != nil {
		panic(err)
	}
	fmt.Printf("More %v\n", alerts.More)
	fmt.Printf("Limit %v\n", alerts.Limit)
	for _, i := range alerts.Incidents {
		desc := strings.Fields(i.Description)[1]
		fmt.Println(desc)
		// fmt.Println(i.Status)
	}
	fmt.Printf("Total %v\n", alerts.Total) // it's always 0!
	fmt.Printf("Offset %v\n", alerts.Offset) // it's always 0!
}

According to the documentation there should a pagination_token method, but there is no such method. However, the available methods are: APIListObject, Incidents, Limit, More, Offset, Total. The Moremethods shows that there's _more_ pages to fetch, however theOffset. method and the Total` method return both zero.

Any hints on how to handle pagination in this case?

@atmosx
Copy link
Author

atmosx commented Sep 1, 2020

According to the docs:

The more field will always be true or false, indicating whether there are more resources matching the query than present in the response (because of the request's limit). If true, you may want to make subsequent requests with a different offset until more is false in order to retrieve all of the resources.

I can use opts.More.

@atmosx atmosx closed this as completed Sep 1, 2020
@atmosx
Copy link
Author

atmosx commented Sep 1, 2020

PoC for anyone looking for examples:

package main

import (
	"github.com/PagerDuty/go-pagerduty"
	"os"
	"strings"
	"fmt"
)

var authtoken = os.Getenv("TOKEN")

func main() {
	var opts pagerduty.ListIncidentsOptions
	opts.Since = "2020-01-01T00:00:00.00-0000"
	opts.Offset = uint(1)
	opts.Limit = 100
	client := pagerduty.NewClient(authtoken)
	for {
		alerts, err := client.ListIncidents(opts)
		if err != nil {
			panic(err)
		}
		for _, i := range alerts.Incidents {
			desc := strings.Fields(i.Description)[1]
			fmt.Println(desc)
		}
		if !alerts.More {
			break
		}
		opts.Offset += 1
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant