v2 REST API - Incident pagination issue

rest-api
incidents

(Mike) #1

I am using Python in order to query the pagerduty API for historic incidents. I have no issues receiving a response containing incidents, however, paging is not functioning as expected.

For testing purposes I have limited the number of cases returned & set the limit 2. Below it is clear each page is returns 1 new case when 2 new cases are expected. I can set the limit to 5 or 100 & a single new case will be returned, followed by the previously returned cases -1. Is this a known issue? Is it possible to workaround the issues in some way?

Python code:

import requests
import json
import time

def get_incidents(offset):
url = ‘https://api.pagerduty.com/incidents?since=2018-05-18%2000%3A00&until=2018-05-19%2000%3A00&statuses[]=triggered&statuses[]=acknowledged&statuses[]=resolved&service_ids[]=<some_service>&time_zone=America%2FChicago&limit=2&total=true&offset=’+str(offset)
headers = {“Accept”: “application/vnd.pagerduty+json;version=2”, “Authorization”: “Token token=<some_token>”}
response = requests.get(url, headers=headers)
page = response.json()[‘more’]
#print response.json()
incidents = response.json()[‘incidents’]
for incident in incidents:
#print “%s,%s,%s,%s” % (incident[‘created_at’], incident[‘incident_number’] , incident[‘description’], incident[‘html_url’])
print “%s,%s” % (incident[‘created_at’], incident[‘incident_number’])
return page

offset = 0
page = get_incidents(offset)
while page == True:
print “Offset:”,str(offset)
print “More:”,str(page)
offset = offset + 1
time.sleep(4)
page = get_incidents(offset)
else:
print page
exit

Results:

2018-05-18T00:16:37-05:00,360080
2018-05-18T02:32:23-05:00,360110
Offset: 0
More: True
2018-05-18T02:32:23-05:00,360110
2018-05-18T10:04:27-05:00,360242
Offset: 1
More: True
2018-05-18T10:04:27-05:00,360242
2018-05-18T10:04:33-05:00,360243
Offset: 2
More: True
2018-05-18T10:04:33-05:00,360243
2018-05-18T10:39:13-05:00,360259
Offset: 3
More: True
2018-05-18T10:39:13-05:00,360259
2018-05-18T11:07:21-05:00,360267
Offset: 4
More: True
2018-05-18T11:07:21-05:00,360267
2018-05-18T12:04:38-05:00,360286
Offset: 5
More: True
2018-05-18T12:04:38-05:00,360286
2018-05-18T12:35:47-05:00,360305
Offset: 6
More: True
2018-05-18T12:35:47-05:00,360305
2018-05-18T12:40:52-05:00,360307
Offset: 7
More: True
2018-05-18T12:40:52-05:00,360307
2018-05-18T14:15:11-05:00,360362
Offset: 8
More: True
2018-05-18T14:15:11-05:00,360362
2018-05-18T16:43:05-05:00,360431
Offset: 9
More: True
2018-05-18T16:43:05-05:00,360431
2018-05-18T16:44:53-05:00,360432
False


(Mike) #2

Case had been opened for this issue.

http://tickets.pagerduty.com/hc/requests/141823


(Mike) #3

Misunderstanding on my part, rookie mistake. Offset does NOT equal a “page” of data. Offset must be increased by the limit.


(system) #4