Skip to content

Commit 5a0ccba

Browse files
Merge pull request #4 from sil-org/improve-paginated-request
Improve paginated request
2 parents 0e5691f + 358a4da commit 5a0ccba

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

paginated_request.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export default defineComponent({
22
name: "Paginated HTTP Request",
33
description: "Creates a HTTP Request until end of paginated data or timeout is reached",
44
key: "paginated_http_request",
5-
version: "0.0.1",
5+
version: "0.0.2",
66
type: "action",
77

88
props: {
@@ -46,22 +46,25 @@ export default defineComponent({
4646
}
4747
},
4848
methods: {
49-
get_pagination_index() {
50-
for (const key in this.http_request.params) {
51-
if (this.http_request.params[key].name == this.paginator) {
52-
return key
53-
}
49+
getPaginationIndex() {
50+
let i = this.http_request.params.findIndex(p => p.name == this.paginator)
51+
if (i < 0) {
52+
i = this.http_request.params.length
53+
this.http_request.params.push({
54+
"name": this.paginator,
55+
"value": "0"
56+
})
5457
}
55-
return null
58+
return i
5659
},
57-
get_response_data(resp) {
60+
getResponseData(resp) {
5861
let data = resp
5962
for (const field of this.data_field.split(".")) {
6063
data = data[field]
6164
}
6265
return data
6366
},
64-
handle_timeout(start) {
67+
handleTimeout(start) {
6568
const duration = Date.now() - start
6669
if (this.timeout && duration > this.timeout * 1000) {
6770
const msg = `Timeout reached at ${duration / 1000} seconds`
@@ -75,18 +78,14 @@ export default defineComponent({
7578
}
7679
},
7780
async run({ $ }) {
78-
const i = await this.get_pagination_index()
79-
if (i === null) {
80-
throw new Error("Pagination parameter not set")
81-
}
82-
81+
const i = await this.getPaginationIndex()
8382
const start = Date.now()
8483
const results = []
8584
let data = []
8685
let count = 1
8786
do {
8887
const resp = await this.http_request.execute()
89-
data = await this.get_response_data(resp)
88+
data = await this.getResponseData(resp)
9089

9190
if (!Array.isArray(data)) {
9291
throw new Error(`Response data is not an array: ${typeof data}`);
@@ -98,7 +97,7 @@ export default defineComponent({
9897

9998
results.push(...data)
10099

101-
if (await this.handle_timeout(start)) {
100+
if (await this.handleTimeout(start)) {
102101
break
103102
}
104103
count++

0 commit comments

Comments
 (0)