Forum Discussion
Pagination code returns first page forever instead of properly paginating
- 3 years ago
Thanks ams1 -
I'm not able to share the API documentation directly, but here's a sample response object they provided:{ "responses": [ { "id": "5e22085d5379215be800002b", "survey_id": "6e22085d5379215be800002b", "created_at": "2020-09-24T00:20:10", "conversation_id": "7e22085d5379215be800002b", "answers": [ { "question": { "id": "5e22085d5379215be800002b", "value": "Choose one option", "type": "multichoice | multiselect | range | nps | singleline" }, "answer": { "value": "'yes' OR ['Red', 'Blue']" } } ], "snapshot_data": { "device": { "id": "55fbe2e775bf3c760b1000bc", "carrier": "Verizon", "custom_data": { "key": "CustomKey", "value": "CustomValue" }, "manufacturer": "Samsung", "model": "S22", "os_api_level": "3.5.9", "os_name": "Android", "os_version": "5.2.0" }, "person": { "id": "95fbe2e775bf3c760b1000bc", "custom_data": { "key": "CustomKey", "value": "CustomValue" }, "email": "[email protected]", "facebook_id": "1184928249824", "mparticle_id": "-29482958295729572", "name": "Jack Daniels" }, "app_release": { "cf_bundle_version": "78", "cf_bundle_short_version_string": "4.5.11", "sdk_version": "4.5.11", "sdk_platform": "iOS", "sdk_distribution": "source", "sdk_distribution_version": "4.5.11" } } } ], "ends_with": "6e22085d5379215be800002b", "page_size": 250, "has_more": true }And these are the sample params:
I've tried your sample code and I think I just need to make sure the result is the `"responses": [` portion and the `ends_with`, `page_size`, and `has_more` just used for cursor/paging.
Hi,
I won't go into many details, but one of the issues is that you should be using previous page ends_with as the next page starts_with -> I haven't seen you using ends_with anywhere in your code.
I'm not sure what is the relevant link for the API documentation (?) - I had a quick look at https://learn.apptentive.com/knowledge-base/apptentive-api-beta/#paging
BUT I could not see there starts_with - instead I saw starts_after.
Anyway, please give the below code a try - it's based on your code, I have NOT tested it:
(app_id as text, survey_id as text) =>
let
getNextPage = (lastPage) =>
let
getPage = (starts_with) =>
let
query =
if starts_with = null then
[page_size = "100", order = "asc"]
else
[page_size = "100", order = "asc", starts_with = starts_with],
result = Json.Document(
Web.Contents(
"https://data.apptentive.com/raw/v2/apps/",
[
Headers = [
Accept = "application/json",
#"x-api-key" = "MY-KEY-HERE"
],
RelativePath = (app_id) & "/surveys/" & (survey_id) & "/responses",
Query = query
]
)
)
in
result,
nextPage =
if lastPage = null then
getPage(null)
else if lastPage[has_more] = true then
getPage(lastPage[ends_with])
else
null
in
nextPage,
pages = List.Generate(
// initial
() => getNextPage(null),
// condition
(lastPage) => lastPage <> null,
//next
(lastPage) => getNextPage(lastPage)
)
in
pages
If the above code does NOT work, please reply AND also please give us the relevant link to the API documentation.
Finally, don't forget to mark this reply as answer if it helped.
Thanks ams1 -
I'm not able to share the API documentation directly, but here's a sample response object they provided:
{
"responses": [
{
"id": "5e22085d5379215be800002b",
"survey_id": "6e22085d5379215be800002b",
"created_at": "2020-09-24T00:20:10",
"conversation_id": "7e22085d5379215be800002b",
"answers": [
{
"question": {
"id": "5e22085d5379215be800002b",
"value": "Choose one option",
"type": "multichoice | multiselect | range | nps | singleline"
},
"answer": {
"value": "'yes' OR ['Red', 'Blue']"
}
}
],
"snapshot_data": {
"device": {
"id": "55fbe2e775bf3c760b1000bc",
"carrier": "Verizon",
"custom_data": {
"key": "CustomKey",
"value": "CustomValue"
},
"manufacturer": "Samsung",
"model": "S22",
"os_api_level": "3.5.9",
"os_name": "Android",
"os_version": "5.2.0"
},
"person": {
"id": "95fbe2e775bf3c760b1000bc",
"custom_data": {
"key": "CustomKey",
"value": "CustomValue"
},
"email": "[email protected]",
"facebook_id": "1184928249824",
"mparticle_id": "-29482958295729572",
"name": "Jack Daniels"
},
"app_release": {
"cf_bundle_version": "78",
"cf_bundle_short_version_string": "4.5.11",
"sdk_version": "4.5.11",
"sdk_platform": "iOS",
"sdk_distribution": "source",
"sdk_distribution_version": "4.5.11"
}
}
}
],
"ends_with": "6e22085d5379215be800002b",
"page_size": 250,
"has_more": true
}And these are the sample params:
I've tried your sample code and I think I just need to make sure the result is the `"responses": [` portion and the `ends_with`, `page_size`, and `has_more` just used for cursor/paging.
- ams13 years agoResponsive Resident
Exactly - you just need to extract "responses" from "pages" - you should be able to do that using the 4th parameter of List.Generate (the selector).
Don't forget to mark as ANSWER the reply if it helped.
- mrsalta3 years agoNew Member
ams1 Awesome! Syntax-wise, what would that fourth parameter look like?
- pages[responses]
- getNextPage[responses]
- lastPage[responses]
- None of the above, silly
?
- ams13 years agoResponsive Resident
Hi,
Somehow I've missed your reply - sorry for that.
If you haven't figured it out (but I think you did), it can be something like:
... pages = List.Generate( // initial () => getNextPage(null), // condition (lastPage) => lastPage <> null, //next (lastPage) => getNextPage(lastPage), // selector (lastPage) => lastPage[responses] // <--------- ) ...Thanks for the "thumbs-up" (aka kudo), but please also mark as ANSWER the most relevant replies -> so that the question status is answered. 😊