Reputation: 67
I've used the JIRA API a few timees, but only for small extracts so I've never had to use pagniation to return more results than the max result.
Here's the documentation for the API: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#expansion
I have a query to the API:
let
Source = Json.Document(Web.Contents("*connection URL*,
[Headers=[Accept="application/json"]])),
#"Converted to Table" = Table.FromRecords({Source}),
>Expand the scheme, and define the field data types etc
in
#"Changed Type"
I can see form the documentation I need to use something like this:
{
"startAt" : 0,
"maxResults" : 10,
"total": 200,
"isLast": false,
"values": [
{ /* result 0 */ },
{ /* result 1 */ },
{ /* result 2 */ }
]
}
I'm not sure where the pagination would fit into this? Can anyone help me? I need to return 1500 results.
Thanks
Upvotes: 1
Views: 3056
Reputation: 821
below is sample python script to call JQL Filter API and loop through paginated results.
Each list call returns three fields
'startAt': 0, 'maxResults': 50, 'total': 141,
You have to call API until you reach total.
import json
import requests
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization" : "Bearer Personal-access-token"
}
BASE_URL = 'https://my.company.com'
jql_url = f'{BASE_URL}/rest/api/latest/search'
query = {
'jql' : 'Your JQL Filter query',
'startAt' : 0,
}
def my_operation_function(issue_data):
issue_key = issue_data['key']
issue_fields = issue_data['fields']
print(f'issue_key\t:\t{issue_key}')
response = requests.request("GET", jql_url, headers=headers, params=query)
data=json.loads(response.text)
'''
>>> data
{'expand': 'schema,names', 'startAt': 0, 'maxResults': 50, 'total': 141, 'issues': [{'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '27825703', 'self': 'https://myjira.site.com/rest/api/latest/issue/27825703', 'key': 'ABC-123', 'fields': {'customfield_21400': None, 'customfield_19200': None, 'customfield_18111': None, 'customfield_19201': None, 'customfield_18112': None, 'customfield_18113': None, 'customfield_19202': None, 'customfield_19203': None, 'customfield_18114': None, 'customfield_18110': None,
'''
issues=data['issues']
for issue in issues:
find_keywords(issue)
while query['startAt'] < data['total']:
query['startAt'] += 50
print(f"\nstartAt = {query['startAt']}")
response = requests.request("GET", jql_url, headers=headers, params=query)
data = json.loads(response.text)
issues=data['issues']
for issue in issues:
my_operation_function(issue)
Upvotes: 0
Reputation: 16806
You'll need to make multiple REST requests that iterate through all of the results one "page" at a time:
maxResults
and startAt
in the response.maxResults
to startAt
, update your REST API call startAt
parameter, and call the same API endpoint again.startAt
is greater than or equal to total
, or until isLast
is true.A while
loop is a natural fit for an operation like this. To minimize API calls, you should use a larger value for maxResults
so you receive more data in each loop.
Upvotes: 3