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

Paging help #94

Closed
abtris opened this issue Oct 16, 2017 · 5 comments
Closed

Paging help #94

abtris opened this issue Oct 16, 2017 · 5 comments

Comments

@abtris
Copy link

abtris commented Oct 16, 2017

Hi,
I need list all incidents in week and I'm over 25 limit. Is easy way how setup in client to get all incidents.

I'm using this:

func GetIncidents(dateFrom string, dateTo string) *pagerduty.ListIncidentsResponse {
	var teamIDs []string
	var timeZone string = "UTC"
	var sortBy string = "incident_number"
	var includes []string
	opts := pagerduty.ListIncidentsOptions{
		TeamIDs:  teamIDs,
		Since:    dateFrom,
		Until:    dateTo,
		TimeZone: timeZone,
		SortBy:   sortBy,
		Includes: includes,
	}
	var c config
	authtoken := c.GetConf().AuthToken
	client := pagerduty.NewClient(authtoken)
	incidentList, err := client.ListIncidents(opts)
	if err != nil {
		panic(err)
	}
	return incidentList
}

but not work over limit 25 (default paging limit in API).

@abtris
Copy link
Author

abtris commented Oct 26, 2017

Resolved by override APIListObject object in opts.

func GetIncidents(dateFrom string, dateTo string) *pagerduty.ListIncidentsResponse {
	var teamIDs []string
	var timeZone string = "UTC"
	var sortBy string
	var includes []string
	var list pagerduty.APIListObject

	list.Limit = 100

	opts := pagerduty.ListIncidentsOptions{
		APIListObject: list,
		TeamIDs:       teamIDs,
		Since:         dateFrom,
		Until:         dateTo,
		TimeZone:      timeZone,
		SortBy:        sortBy,
		Includes:      includes,
	}
	var c config
	authtoken := c.GetConf().AuthToken
	client := pagerduty.NewClient(authtoken)
	incidentList, err := client.ListIncidents(opts)
	if err != nil {
		panic(err)
	}
	return incidentList
}

@abtris abtris closed this as completed Oct 26, 2017
@sevagh
Copy link

sevagh commented Dec 29, 2017

Is there a better way of handling this?

For example:

escalations := client.ListEscalationPolicies()
if (response == 404 not found) {
   fetch_another_page
}

I'm having trouble understanding how to use the APIListObject struct.

@sevagh
Copy link

sevagh commented Dec 29, 2017

Does this make sense?

listOptions := pagerduty.ListEscalationPoliciesOptions{
		Query: policy,
	}

	listOptions.Limit = 100

	for {
		escalationPolicyResp, err := client.ListEscalationPolicies(listOptions)
		if err != nil {
			if strings.Contains(err.Error(), "HTTP response code: 404") {
				if escalationPolicyResp.More == true {
					listOptions.Offset = escalationPolicyResp.Offset
					continue
				}
			}
			return []pagerduty.EscalationPolicy{}, err
		}
	}

@therin
Copy link

therin commented May 30, 2018

how did you guys ended solving it? I feel that overriding APIListObject is a bit dirty

@therin
Copy link

therin commented May 31, 2018

ended up solving like this:

func GetPagerDutyUsers() []pagerduty.User {
	var Users []pagerduty.User
	var APIList pagerduty.APIListObject

	opts := pagerduty.ListUsersOptions{}

	client := pagerduty.NewClient(tools.EnvironmentVariables.PagerDutyApiKey)

	for {

		usr, err := client.ListUsers(opts)
		fmt.Printf("%+v\n", usr.APIListObject)
		if err != nil {
			panic(err)
		}

		Users = append(Users, usr.Users...)
		APIList.Offset += 25
		opts = pagerduty.ListUsersOptions{APIListObject: APIList}

		if usr.APIListObject.More != true {
			fmt.Println("Users Extracted")

			return Users

		}

	}
	return Users

}

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

3 participants