apify-client 3.0.0-beta.1 → 3.0.0-beta.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -5
- package/dist/apify_api_error.d.ts +64 -22
- package/dist/apify_api_error.js +100 -30
- package/dist/apify_client.d.ts +150 -35
- package/dist/apify_client.js +198 -118
- package/dist/apify_client_error.d.ts +10 -0
- package/dist/apify_client_error.js +10 -0
- package/dist/base/api_client.d.ts +9 -9
- package/dist/base/api_client.js +41 -45
- package/dist/base/resource_client.d.ts +24 -10
- package/dist/base/resource_client.js +78 -48
- package/dist/base/resource_collection_client.d.ts +15 -6
- package/dist/base/resource_collection_client.js +40 -21
- package/dist/body_parser.d.ts +3 -4
- package/dist/body_parser.js +20 -32
- package/dist/bundle.js +47 -52
- package/dist/bundle.js.map +1 -1
- package/dist/generated/api.d.ts +3363 -1185
- package/dist/generated/api.js +1 -2
- package/dist/generated/schemas.d.ts +8146 -0
- package/dist/generated/schemas.js +1866 -0
- package/dist/http_clients/axios.d.ts +61 -0
- package/dist/http_clients/axios.js +119 -0
- package/dist/http_clients/base.d.ts +391 -0
- package/dist/http_clients/base.js +650 -0
- package/dist/http_clients/index.d.ts +4 -0
- package/dist/http_clients/index.js +2 -0
- package/dist/http_compressors/base.d.ts +40 -0
- package/dist/http_compressors/base.js +9 -0
- package/dist/http_compressors/brotli.d.ts +33 -0
- package/dist/http_compressors/brotli.js +39 -0
- package/dist/http_compressors/gzip.d.ts +33 -0
- package/dist/http_compressors/gzip.js +39 -0
- package/dist/http_compressors/index.d.ts +8 -0
- package/dist/http_compressors/index.js +3 -0
- package/dist/http_compressors/resolve.d.ts +24 -0
- package/dist/http_compressors/resolve.js +30 -0
- package/dist/http_compressors/zstd.d.ts +34 -0
- package/dist/http_compressors/zstd.js +43 -0
- package/dist/index.d.ts +38 -32
- package/dist/index.js +36 -36
- package/dist/invalid_response_body_error.d.ts +14 -0
- package/dist/invalid_response_body_error.js +18 -0
- package/dist/lazy_schema.d.ts +8 -0
- package/dist/lazy_schema.js +11 -0
- package/dist/models.d.ts +196 -278
- package/dist/models.js +14 -15
- package/dist/resource_clients/actor.d.ts +115 -45
- package/dist/resource_clients/actor.js +187 -116
- package/dist/resource_clients/actor_collection.d.ts +21 -17
- package/dist/resource_clients/actor_collection.js +26 -21
- package/dist/resource_clients/actor_env_var.d.ts +13 -6
- package/dist/resource_clients/actor_env_var.js +21 -14
- package/dist/resource_clients/actor_env_var_collection.d.ts +18 -22
- package/dist/resource_clients/actor_env_var_collection.js +23 -20
- package/dist/resource_clients/actor_server.d.ts +149 -0
- package/dist/resource_clients/actor_server.js +199 -0
- package/dist/resource_clients/actor_version.d.ts +30 -10
- package/dist/resource_clients/actor_version.js +29 -23
- package/dist/resource_clients/actor_version_collection.d.ts +20 -21
- package/dist/resource_clients/actor_version_collection.js +25 -20
- package/dist/resource_clients/build.d.ts +24 -11
- package/dist/resource_clients/build.js +43 -26
- package/dist/resource_clients/build_collection.d.ts +10 -8
- package/dist/resource_clients/build_collection.js +13 -13
- package/dist/resource_clients/dataset.d.ts +87 -40
- package/dist/resource_clients/dataset.js +159 -175
- package/dist/resource_clients/dataset_collection.d.ts +13 -9
- package/dist/resource_clients/dataset_collection.js +29 -21
- package/dist/resource_clients/key_value_store.d.ts +110 -28
- package/dist/resource_clients/key_value_store.js +177 -99
- package/dist/resource_clients/key_value_store_collection.d.ts +13 -9
- package/dist/resource_clients/key_value_store_collection.js +29 -21
- package/dist/resource_clients/log.d.ts +42 -23
- package/dist/resource_clients/log.js +156 -85
- package/dist/resource_clients/request_queue.d.ts +77 -38
- package/dist/resource_clients/request_queue.js +251 -172
- package/dist/resource_clients/request_queue_collection.d.ts +12 -8
- package/dist/resource_clients/request_queue_collection.js +23 -20
- package/dist/resource_clients/run.d.ts +134 -34
- package/dist/resource_clients/run.js +225 -84
- package/dist/resource_clients/run_collection.d.ts +8 -6
- package/dist/resource_clients/run_collection.js +18 -18
- package/dist/resource_clients/schedule.d.ts +20 -11
- package/dist/resource_clients/schedule.js +36 -30
- package/dist/resource_clients/schedule_collection.d.ts +10 -6
- package/dist/resource_clients/schedule_collection.js +20 -17
- package/dist/resource_clients/store_collection.d.ts +9 -7
- package/dist/resource_clients/store_collection.js +18 -18
- package/dist/resource_clients/task.d.ts +44 -21
- package/dist/resource_clients/task.js +116 -86
- package/dist/resource_clients/task_collection.d.ts +13 -9
- package/dist/resource_clients/task_collection.js +19 -16
- package/dist/resource_clients/user.d.ts +22 -13
- package/dist/resource_clients/user.js +46 -47
- package/dist/resource_clients/webhook.d.ts +21 -12
- package/dist/resource_clients/webhook.js +35 -30
- package/dist/resource_clients/webhook_collection.d.ts +11 -7
- package/dist/resource_clients/webhook_collection.js +20 -17
- package/dist/resource_clients/webhook_dispatch.d.ts +9 -6
- package/dist/resource_clients/webhook_dispatch.js +11 -10
- package/dist/resource_clients/webhook_dispatch_collection.d.ts +7 -5
- package/dist/resource_clients/webhook_dispatch_collection.js +13 -13
- package/dist/response_validation_error.d.ts +27 -0
- package/dist/response_validation_error.js +38 -0
- package/dist/runtime/node.d.ts +6 -0
- package/dist/runtime/node.js +87 -0
- package/dist/runtime/types.d.ts +60 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/web.d.ts +5 -0
- package/dist/runtime/web.js +21 -0
- package/dist/schemas.d.ts +381 -0
- package/dist/schemas.js +37 -0
- package/dist/statistics.d.ts +1 -6
- package/dist/statistics.js +7 -11
- package/dist/timeouts.d.ts +84 -0
- package/dist/timeouts.js +37 -0
- package/dist/timezones.d.ts +1 -1
- package/dist/timezones.js +1 -2
- package/dist/utils.d.ts +99 -70
- package/dist/utils.js +251 -229
- package/dist/wait_for_resources.d.ts +8 -0
- package/dist/wait_for_resources.js +39 -0
- package/package.json +48 -47
- package/dist/argument_validation_error.d.ts +0 -17
- package/dist/argument_validation_error.js +0 -157
- package/dist/http_client.d.ts +0 -88
- package/dist/http_client.js +0 -260
- package/dist/index.mjs +0 -42
- package/dist/interceptors.d.ts +0 -19
- package/dist/interceptors.js +0 -110
|
@@ -1,59 +1,56 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const utils_1 = require("../utils");
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { MAX_PAYLOAD_SIZE_BYTES, REQUEST_QUEUE_MAX_REQUESTS_PER_BATCH_OPERATION } from '@apify/consts';
|
|
3
|
+
import log from '@apify/log';
|
|
4
|
+
import { ResourceClient } from '../base/resource_client.js';
|
|
5
|
+
import { ResponseValidationError } from '../response_validation_error.js';
|
|
6
|
+
import * as schemas from '../schemas.js';
|
|
7
|
+
import { optionalSignalSchema, optionalTimeoutSchema, timeoutOptionsSchema, timeoutOptionsShape } from '../timeouts.js';
|
|
8
|
+
import { anyObjectSchema, catchNotFoundOrThrow, isNonArrayObject, minForLimitParam, parseArgument, parseResponse, RequestQueuePaginationIterator, sleep, splitIntoJsonArrayBatches, utf8ByteLength, } from '../utils.js';
|
|
10
9
|
const DEFAULT_PARALLEL_BATCH_ADD_REQUESTS = 5;
|
|
11
10
|
const DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS = 3;
|
|
12
11
|
const DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS = 500;
|
|
13
12
|
const DEFAULT_REQUEST_QUEUE_REQUEST_PAGE_LIMIT = 1000;
|
|
14
13
|
const SAFETY_BUFFER_PERCENT = 0.01 / 100; // 0.01%
|
|
15
|
-
const listHeadOptionsSchema =
|
|
16
|
-
const listAndLockHeadOptionsSchema =
|
|
17
|
-
lockSecs:
|
|
18
|
-
limit:
|
|
14
|
+
const listHeadOptionsSchema = z.strictObject({ limit: z.number().min(0).optional(), ...timeoutOptionsShape });
|
|
15
|
+
const listAndLockHeadOptionsSchema = z.strictObject({
|
|
16
|
+
lockSecs: z.number(),
|
|
17
|
+
limit: z.number().min(0).optional(),
|
|
18
|
+
...timeoutOptionsShape,
|
|
19
19
|
});
|
|
20
20
|
// Predicates, not `z.looseObject` arms: these run over a whole batch, and an object arm would copy
|
|
21
21
|
// every key of every request. `id` is assigned by the API, so a new request must not carry one.
|
|
22
|
-
const newRequestSchema =
|
|
23
|
-
const forefrontOptionsSchema =
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
const batchDeleteRequestsSchema = zod_1.z
|
|
22
|
+
const newRequestSchema = z.custom((value) => isNonArrayObject(value) && value.id === undefined, 'Expected a request object without an `id`');
|
|
23
|
+
const forefrontOptionsSchema = z.strictObject({ forefront: z.boolean().optional(), ...timeoutOptionsShape });
|
|
24
|
+
const batchAddRequestsWithRetriesSchema = z.array(newRequestSchema).min(1);
|
|
25
|
+
const optionalBooleanSchema = z.boolean().optional();
|
|
26
|
+
const optionalNumberSchema = z.number().optional();
|
|
27
|
+
const requestToDeleteSchema = z.custom((value) => isNonArrayObject(value) && (typeof value.id === 'string' || typeof value.uniqueKey === 'string'), 'Expected a request object with an `id` or a `uniqueKey`');
|
|
28
|
+
const batchDeleteRequestsSchema = z
|
|
30
29
|
.array(requestToDeleteSchema)
|
|
31
30
|
.min(1)
|
|
32
|
-
.max(
|
|
33
|
-
const requestIdSchema =
|
|
34
|
-
const existingRequestSchema =
|
|
35
|
-
const prolongRequestLockOptionsSchema =
|
|
36
|
-
lockSecs:
|
|
37
|
-
forefront:
|
|
31
|
+
.max(REQUEST_QUEUE_MAX_REQUESTS_PER_BATCH_OPERATION);
|
|
32
|
+
const requestIdSchema = z.string();
|
|
33
|
+
const existingRequestSchema = z.custom((value) => isNonArrayObject(value) && typeof value.id === 'string', 'Expected a request object with an `id`');
|
|
34
|
+
const prolongRequestLockOptionsSchema = z.strictObject({
|
|
35
|
+
lockSecs: z.number(),
|
|
36
|
+
forefront: z.boolean().optional(),
|
|
37
|
+
...timeoutOptionsShape,
|
|
38
38
|
});
|
|
39
|
-
const requestFilterSchema =
|
|
40
|
-
const listRequestsOptionsSchema =
|
|
41
|
-
.
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
cursor: zod_1.z.string().optional(),
|
|
39
|
+
const requestFilterSchema = z.array(z.enum(['locked', 'pending'])).min(1);
|
|
40
|
+
const listRequestsOptionsSchema = z.strictObject({
|
|
41
|
+
limit: z.number().min(0).optional(),
|
|
42
|
+
chunkSize: z.number().positive().optional(),
|
|
43
|
+
cursor: z.string().optional(),
|
|
45
44
|
filter: requestFilterSchema.optional(),
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const paginateRequestsOptionsSchema =
|
|
49
|
-
.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
exclusiveStartId: zod_1.z.string().optional(),
|
|
53
|
-
cursor: zod_1.z.string().optional(),
|
|
45
|
+
...timeoutOptionsShape,
|
|
46
|
+
});
|
|
47
|
+
const paginateRequestsOptionsSchema = z.strictObject({
|
|
48
|
+
limit: z.number().min(0).optional(),
|
|
49
|
+
maxPageLimit: z.number().default(DEFAULT_REQUEST_QUEUE_REQUEST_PAGE_LIMIT),
|
|
50
|
+
cursor: z.string().optional(),
|
|
54
51
|
filter: requestFilterSchema.optional(),
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
...timeoutOptionsShape,
|
|
53
|
+
});
|
|
57
54
|
/**
|
|
58
55
|
* Client for managing a specific Request queue.
|
|
59
56
|
*
|
|
@@ -84,9 +81,9 @@ const paginateRequestsOptionsSchema = zod_1.z
|
|
|
84
81
|
*
|
|
85
82
|
* @see https://docs.apify.com/platform/storage/request-queue
|
|
86
83
|
*/
|
|
87
|
-
class RequestQueueClient extends
|
|
88
|
-
clientKey;
|
|
89
|
-
|
|
84
|
+
export class RequestQueueClient extends ResourceClient {
|
|
85
|
+
#clientKey;
|
|
86
|
+
#timeoutSecs;
|
|
90
87
|
/**
|
|
91
88
|
* @hidden
|
|
92
89
|
*/
|
|
@@ -95,36 +92,57 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
95
92
|
resourcePath: 'request-queues',
|
|
96
93
|
...options,
|
|
97
94
|
});
|
|
98
|
-
this
|
|
99
|
-
this
|
|
95
|
+
this.#clientKey = userOptions.clientKey;
|
|
96
|
+
this.#timeoutSecs = userOptions.timeoutSecs;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Picks the timeout of a request: an explicit per-call `timeoutSecs` as is, otherwise the method's default
|
|
100
|
+
* tier, capped at the queue-wide `timeoutSecs` when one was given.
|
|
101
|
+
*/
|
|
102
|
+
#resolveTimeout(timeoutSecs, defaultTier) {
|
|
103
|
+
if (timeoutSecs !== undefined)
|
|
104
|
+
return timeoutSecs;
|
|
105
|
+
if (this.#timeoutSecs === undefined)
|
|
106
|
+
return defaultTier;
|
|
107
|
+
const tierSecs = this.httpClient.timeoutMillis[defaultTier] / 1000;
|
|
108
|
+
return tierSecs <= this.#timeoutSecs ? defaultTier : this.#timeoutSecs;
|
|
100
109
|
}
|
|
101
110
|
/**
|
|
102
111
|
* Gets the Request queue object from the Apify API.
|
|
103
112
|
*
|
|
113
|
+
* @param options - Request options
|
|
114
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
104
115
|
* @returns The RequestQueue object, or `undefined` if it does not exist
|
|
105
116
|
* @see https://docs.apify.com/api/v2/request-queue-get
|
|
106
117
|
*/
|
|
107
|
-
async get() {
|
|
108
|
-
|
|
118
|
+
async get(options = {}) {
|
|
119
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
120
|
+
return this.getResource(schemas.RequestQueueResource(), {}, this.#resolveTimeout(timeoutSecs, 'short'), signal);
|
|
109
121
|
}
|
|
110
122
|
/**
|
|
111
123
|
* Updates the Request queue with specified fields.
|
|
112
124
|
*
|
|
113
125
|
* @param newFields - Fields to update in the Request queue
|
|
126
|
+
* @param options - Request options
|
|
127
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
114
128
|
* @returns The updated RequestQueue object
|
|
115
129
|
* @see https://docs.apify.com/api/v2/request-queue-put
|
|
116
130
|
*/
|
|
117
|
-
async update(newFields) {
|
|
118
|
-
|
|
119
|
-
|
|
131
|
+
async update(newFields, options = {}) {
|
|
132
|
+
parseArgument(newFields, anyObjectSchema);
|
|
133
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
134
|
+
return this.updateResource(schemas.RequestQueueResource(), newFields, this.#resolveTimeout(timeoutSecs, 'short'), signal);
|
|
120
135
|
}
|
|
121
136
|
/**
|
|
122
137
|
* Deletes the Request queue.
|
|
123
138
|
*
|
|
139
|
+
* @param options - Request options
|
|
140
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
124
141
|
* @see https://docs.apify.com/api/v2/request-queue-delete
|
|
125
142
|
*/
|
|
126
|
-
async delete() {
|
|
127
|
-
|
|
143
|
+
async delete(options = {}) {
|
|
144
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
145
|
+
return this.deleteResource(this.#resolveTimeout(timeoutSecs, 'short'), signal);
|
|
128
146
|
}
|
|
129
147
|
/**
|
|
130
148
|
* Lists requests from the beginning of the queue (head).
|
|
@@ -133,21 +151,24 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
133
151
|
* inspecting what requests are waiting to be processed.
|
|
134
152
|
*
|
|
135
153
|
* @param options - Options for listing (e.g., limit)
|
|
154
|
+
* @param options.limit - Maximum number of requests to return.
|
|
155
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
136
156
|
* @returns List of requests from the queue head
|
|
137
157
|
* @see https://docs.apify.com/api/v2/request-queue-head-get
|
|
138
158
|
*/
|
|
139
159
|
async listHead(options = {}) {
|
|
140
|
-
const parsed =
|
|
160
|
+
const parsed = parseArgument(options, listHeadOptionsSchema, 'RequestQueueClientListHeadOptions');
|
|
141
161
|
const response = await this.httpClient.call({
|
|
142
|
-
url: this.
|
|
162
|
+
url: this.buildUrl('head'),
|
|
143
163
|
method: 'GET',
|
|
144
|
-
|
|
145
|
-
|
|
164
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
|
|
165
|
+
signal: parsed.signal,
|
|
166
|
+
params: this.buildParams({
|
|
146
167
|
limit: parsed.limit,
|
|
147
|
-
clientKey: this
|
|
168
|
+
clientKey: this.#clientKey,
|
|
148
169
|
}),
|
|
149
170
|
});
|
|
150
|
-
return (
|
|
171
|
+
return parseResponse(response, schemas.RequestQueueHead());
|
|
151
172
|
}
|
|
152
173
|
/**
|
|
153
174
|
* Gets and locks the next requests from the queue head for processing.
|
|
@@ -161,6 +182,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
161
182
|
* @param options - Lock configuration
|
|
162
183
|
* @param options.lockSecs - **Required.** Duration in seconds to lock the requests. After this time, the locks expire and requests can be retrieved by other clients.
|
|
163
184
|
* @param options.limit - Maximum number of requests to return. Default is 25.
|
|
185
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
|
|
164
186
|
* @returns Object containing `items` (locked requests), `queueModifiedAt`, `hadMultipleClients`, and lock information
|
|
165
187
|
* @see https://docs.apify.com/api/v2/request-queue-head-lock-post
|
|
166
188
|
*
|
|
@@ -183,18 +205,19 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
183
205
|
* @since Added in 2.4.1
|
|
184
206
|
*/
|
|
185
207
|
async listAndLockHead(options) {
|
|
186
|
-
const parsed =
|
|
208
|
+
const parsed = parseArgument(options, listAndLockHeadOptionsSchema, 'RequestQueueClientListAndLockHeadOptions');
|
|
187
209
|
const response = await this.httpClient.call({
|
|
188
|
-
url: this.
|
|
210
|
+
url: this.buildUrl('head/lock'),
|
|
189
211
|
method: 'POST',
|
|
190
|
-
|
|
191
|
-
|
|
212
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
|
|
213
|
+
signal: parsed.signal,
|
|
214
|
+
params: this.buildParams({
|
|
192
215
|
limit: parsed.limit,
|
|
193
216
|
lockSecs: parsed.lockSecs,
|
|
194
|
-
clientKey: this
|
|
217
|
+
clientKey: this.#clientKey,
|
|
195
218
|
}),
|
|
196
219
|
});
|
|
197
|
-
return (
|
|
220
|
+
return parseResponse(response, schemas.LockedRequestQueueHead());
|
|
198
221
|
}
|
|
199
222
|
/**
|
|
200
223
|
* Adds a single request to the queue.
|
|
@@ -212,6 +235,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
212
235
|
* @param request.payload - HTTP payload for POST/PUT requests (string).
|
|
213
236
|
* @param options - Additional options
|
|
214
237
|
* @param options.forefront - If `true`, adds the request to the beginning of the queue. Default is `false` (adds to the end).
|
|
238
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
215
239
|
* @returns Object with `requestId`, `wasAlreadyPresent`, and `wasAlreadyHandled` flags
|
|
216
240
|
* @see https://docs.apify.com/api/v2/request-queue-requests-post
|
|
217
241
|
*
|
|
@@ -235,42 +259,46 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
235
259
|
* ```
|
|
236
260
|
*/
|
|
237
261
|
async addRequest(request, options = {}) {
|
|
238
|
-
|
|
239
|
-
const parsed =
|
|
262
|
+
parseArgument(request, newRequestSchema);
|
|
263
|
+
const parsed = parseArgument(options, forefrontOptionsSchema, 'RequestQueueClientAddRequestOptions');
|
|
240
264
|
const response = await this.httpClient.call({
|
|
241
|
-
url: this.
|
|
265
|
+
url: this.buildUrl('requests'),
|
|
242
266
|
method: 'POST',
|
|
243
|
-
|
|
267
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
|
|
268
|
+
signal: parsed.signal,
|
|
244
269
|
data: request,
|
|
245
|
-
params: this.
|
|
270
|
+
params: this.buildParams({
|
|
246
271
|
forefront: parsed.forefront,
|
|
247
|
-
clientKey: this
|
|
272
|
+
clientKey: this.#clientKey,
|
|
248
273
|
}),
|
|
249
274
|
});
|
|
250
|
-
return (
|
|
275
|
+
return parseResponse(response, schemas.RequestRegistration());
|
|
251
276
|
}
|
|
252
277
|
/**
|
|
253
278
|
* Writes requests to Request queue in batch.
|
|
254
279
|
*
|
|
255
280
|
* @private
|
|
256
281
|
*/
|
|
257
|
-
async
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
url: this._url('requests/batch'),
|
|
282
|
+
async addRequestBatch(requests, options = {}) {
|
|
283
|
+
const parsed = parseArgument(options, forefrontOptionsSchema, 'RequestQueueClientAddRequestOptions');
|
|
284
|
+
const response = await this.httpClient.call({
|
|
285
|
+
url: this.buildUrl('requests/batch'),
|
|
262
286
|
method: 'POST',
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
287
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
|
|
288
|
+
signal: parsed.signal,
|
|
289
|
+
// The body is assembled from the requests as `batchAddRequests` serialized them; the explicit content type
|
|
290
|
+
// makes the request interceptor send the string as it is instead of serializing the requests again.
|
|
291
|
+
headers: { 'content-type': 'application/json' },
|
|
292
|
+
data: `[${requests.map(({ json }) => json).join(',')}]`,
|
|
293
|
+
params: this.buildParams({
|
|
266
294
|
forefront: parsed.forefront,
|
|
267
|
-
clientKey: this
|
|
295
|
+
clientKey: this.#clientKey,
|
|
268
296
|
}),
|
|
269
297
|
});
|
|
270
|
-
return (
|
|
298
|
+
return parseResponse(response, schemas.BatchAddResult());
|
|
271
299
|
}
|
|
272
|
-
async
|
|
273
|
-
const { forefront, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
|
|
300
|
+
async addRequestBatchWithRetries(requests, options = {}) {
|
|
301
|
+
const { forefront, timeoutSecs, signal, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
|
|
274
302
|
// Keep track of the requests that remain to be processed (in parameter format)
|
|
275
303
|
let remainingRequests = requests;
|
|
276
304
|
// Keep track of the requests that have been processed (in api format)
|
|
@@ -280,8 +308,10 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
280
308
|
let unprocessedRequests = [];
|
|
281
309
|
for (let i = 0; i < 1 + maxUnprocessedRequestsRetries; i++) {
|
|
282
310
|
try {
|
|
283
|
-
const response = await this.
|
|
311
|
+
const response = await this.addRequestBatch(remainingRequests, {
|
|
284
312
|
forefront,
|
|
313
|
+
timeoutSecs,
|
|
314
|
+
signal,
|
|
285
315
|
});
|
|
286
316
|
processedRequests.push(...response.processedRequests);
|
|
287
317
|
unprocessedRequests = response.unprocessedRequests;
|
|
@@ -293,30 +323,34 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
293
323
|
// Get unique keys of all requests processed so far
|
|
294
324
|
const processedRequestsUniqueKeys = processedRequests.map(({ uniqueKey }) => uniqueKey);
|
|
295
325
|
// Requests remaining to be processed are the all that remain
|
|
296
|
-
remainingRequests = requests.filter(({
|
|
326
|
+
remainingRequests = requests.filter(({ request }) => !processedRequestsUniqueKeys.includes(request.uniqueKey));
|
|
297
327
|
// Stop if all requests have been processed
|
|
298
328
|
if (remainingRequests.length === 0) {
|
|
299
329
|
break;
|
|
300
330
|
}
|
|
301
331
|
}
|
|
302
332
|
catch (err) {
|
|
303
|
-
|
|
333
|
+
// An abort rejects the whole call, since the caller asked to stop and not for a partial result.
|
|
334
|
+
signal?.throwIfAborted();
|
|
335
|
+
// A response the specification does not describe is reported, as everywhere else: the server may well
|
|
336
|
+
// have added the batch, so calling it unprocessed would hide the mismatch behind a wrong answer.
|
|
337
|
+
if (err instanceof ResponseValidationError)
|
|
338
|
+
throw err;
|
|
339
|
+
log.exception(err, 'Request batch insert failed');
|
|
304
340
|
// When something fails and http client does not retry, the remaining requests are treated as unprocessed.
|
|
305
341
|
// This ensures that this method does not throw and keeps the signature.
|
|
306
342
|
const processedRequestsUniqueKeys = processedRequests.map(({ uniqueKey }) => uniqueKey);
|
|
307
343
|
unprocessedRequests = requests
|
|
308
|
-
.filter(({
|
|
309
|
-
.map(({ method, uniqueKey, url }) => ({ method, uniqueKey, url }));
|
|
344
|
+
.filter(({ request }) => !processedRequestsUniqueKeys.includes(request.uniqueKey))
|
|
345
|
+
.map(({ request: { method, uniqueKey, url } }) => ({ method, uniqueKey, url }));
|
|
310
346
|
break;
|
|
311
347
|
}
|
|
312
348
|
// Exponential backoff
|
|
313
349
|
const delayMillis = Math.floor((1 + Math.random()) * 2 ** i * minDelayBetweenUnprocessedRequestsRetriesMillis);
|
|
314
|
-
await
|
|
315
|
-
|
|
316
|
-
});
|
|
350
|
+
await sleep(delayMillis, signal);
|
|
351
|
+
signal?.throwIfAborted();
|
|
317
352
|
}
|
|
318
|
-
|
|
319
|
-
return (0, utils_1.cast)((0, utils_1.parseDateFields)(result));
|
|
353
|
+
return { processedRequests, unprocessedRequests };
|
|
320
354
|
}
|
|
321
355
|
/**
|
|
322
356
|
* Adds multiple requests to the queue in a single operation.
|
|
@@ -333,6 +367,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
333
367
|
* @param options.maxUnprocessedRequestsRetries - Maximum number of retry attempts for rate-limited requests. Default is 3.
|
|
334
368
|
* @param options.maxParallel - Maximum number of parallel batch API calls. Default is 5.
|
|
335
369
|
* @param options.minDelayBetweenUnprocessedRequestsRetriesMillis - Minimum delay before retrying rate-limited requests. Default is 500ms.
|
|
370
|
+
* @param options.timeoutSecs - Timeout for each batch API request. Default is `'medium'`.
|
|
336
371
|
* @returns Object with `processedRequests` (successfully added) and `unprocessedRequests` (failed after all retries)
|
|
337
372
|
* @see https://docs.apify.com/api/v2/request-queue-requests-batch-post
|
|
338
373
|
*
|
|
@@ -357,30 +392,46 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
357
392
|
* @since Added in 2.1.0
|
|
358
393
|
*/
|
|
359
394
|
async batchAddRequests(requests, options = {}) {
|
|
360
|
-
const { forefront, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, maxParallel = DEFAULT_PARALLEL_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
395
|
+
const { forefront, timeoutSecs, signal, maxUnprocessedRequestsRetries = DEFAULT_UNPROCESSED_RETRIES_BATCH_ADD_REQUESTS, maxParallel = DEFAULT_PARALLEL_BATCH_ADD_REQUESTS, minDelayBetweenUnprocessedRequestsRetriesMillis = DEFAULT_MIN_DELAY_BETWEEN_UNPROCESSED_REQUESTS_RETRIES_MILLIS, } = options;
|
|
396
|
+
parseArgument(requests, batchAddRequestsWithRetriesSchema);
|
|
397
|
+
parseArgument(forefront, optionalBooleanSchema);
|
|
398
|
+
parseArgument(timeoutSecs, optionalTimeoutSchema);
|
|
399
|
+
parseArgument(signal, optionalSignalSchema);
|
|
400
|
+
parseArgument(maxUnprocessedRequestsRetries, optionalNumberSchema);
|
|
401
|
+
parseArgument(maxParallel, optionalNumberSchema);
|
|
402
|
+
parseArgument(minDelayBetweenUnprocessedRequestsRetriesMillis, optionalNumberSchema);
|
|
366
403
|
const executingRequests = new Set();
|
|
367
404
|
const individualResults = [];
|
|
368
|
-
const payloadSizeLimitBytes =
|
|
405
|
+
const payloadSizeLimitBytes = MAX_PAYLOAD_SIZE_BYTES - Math.ceil(MAX_PAYLOAD_SIZE_BYTES * SAFETY_BUFFER_PERCENT);
|
|
406
|
+
// Serialize every request once: the byte lengths decide the batch boundaries, and the same strings are joined
|
|
407
|
+
// into the batch bodies, so nothing is stringified a second time when it is sent.
|
|
408
|
+
const serializedRequests = requests.map((request, index) => {
|
|
409
|
+
const json = JSON.stringify(request);
|
|
410
|
+
const byteLength = utf8ByteLength(json);
|
|
411
|
+
// Two more bytes for the brackets, which even a batch of one request carries.
|
|
412
|
+
if (byteLength + 2 > payloadSizeLimitBytes) {
|
|
413
|
+
throw new Error(`RequestQueueClient.batchAddRequests: The size of the request with index: ${index} ` +
|
|
414
|
+
`exceeds the maximum allowed size (${payloadSizeLimitBytes} bytes).`);
|
|
415
|
+
}
|
|
416
|
+
return { request, json, byteLength };
|
|
417
|
+
});
|
|
418
|
+
const batches = splitIntoJsonArrayBatches(serializedRequests, {
|
|
419
|
+
maxCount: REQUEST_QUEUE_MAX_REQUESTS_PER_BATCH_OPERATION,
|
|
420
|
+
maxByteLength: payloadSizeLimitBytes,
|
|
421
|
+
});
|
|
369
422
|
// Keep a pool of up to `maxParallel` requests running at once
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
const slicedRequests = requests.slice(i, i + consts_1.REQUEST_QUEUE_MAX_REQUESTS_PER_BATCH_OPERATION);
|
|
373
|
-
const requestsInBatch = (0, utils_1.sliceArrayByByteLength)(slicedRequests, payloadSizeLimitBytes, i);
|
|
374
|
-
const requestPromise = this._batchAddRequestsWithRetries(requestsInBatch, options);
|
|
423
|
+
for (const batch of batches) {
|
|
424
|
+
const requestPromise = this.addRequestBatchWithRetries(batch, options);
|
|
375
425
|
executingRequests.add(requestPromise);
|
|
426
|
+
// A rejection reaches the caller through the awaits below; this bookkeeping chain only has to avoid
|
|
427
|
+
// turning it into an unhandled one of its own.
|
|
376
428
|
void requestPromise.then((batchAddResult) => {
|
|
377
429
|
executingRequests.delete(requestPromise);
|
|
378
430
|
individualResults.push(batchAddResult);
|
|
379
|
-
});
|
|
431
|
+
}, () => undefined);
|
|
380
432
|
if (executingRequests.size >= maxParallel) {
|
|
381
433
|
await Promise.race(executingRequests);
|
|
382
434
|
}
|
|
383
|
-
i += requestsInBatch.length;
|
|
384
435
|
}
|
|
385
436
|
// Get results from remaining operations
|
|
386
437
|
await Promise.all(executingRequests);
|
|
@@ -401,44 +452,52 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
401
452
|
* Requests can be identified by either their ID or unique key.
|
|
402
453
|
*
|
|
403
454
|
* @param requests - Array of requests to delete (by id or uniqueKey)
|
|
455
|
+
* @param options - Request options
|
|
456
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
404
457
|
* @returns Result containing processed and unprocessed requests
|
|
405
458
|
* @see https://docs.apify.com/api/v2/request-queue-requests-batch-delete
|
|
406
459
|
* @since Added in 2.3.0
|
|
407
460
|
*/
|
|
408
|
-
async batchDeleteRequests(requests) {
|
|
409
|
-
|
|
410
|
-
const {
|
|
411
|
-
|
|
461
|
+
async batchDeleteRequests(requests, options = {}) {
|
|
462
|
+
parseArgument(requests, batchDeleteRequestsSchema);
|
|
463
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
464
|
+
const response = await this.httpClient.call({
|
|
465
|
+
url: this.buildUrl('requests/batch'),
|
|
412
466
|
method: 'DELETE',
|
|
413
|
-
|
|
467
|
+
timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
|
|
468
|
+
signal,
|
|
414
469
|
data: requests,
|
|
415
|
-
params: this.
|
|
416
|
-
clientKey: this
|
|
470
|
+
params: this.buildParams({
|
|
471
|
+
clientKey: this.#clientKey,
|
|
417
472
|
}),
|
|
418
473
|
});
|
|
419
|
-
return (
|
|
474
|
+
return parseResponse(response, schemas.BatchDeleteResult());
|
|
420
475
|
}
|
|
421
476
|
/**
|
|
422
477
|
* Gets a specific request from the queue by its ID.
|
|
423
478
|
*
|
|
424
479
|
* @param id - Request ID
|
|
480
|
+
* @param options - Request options
|
|
481
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
425
482
|
* @returns The request object, or `undefined` if not found
|
|
426
483
|
* @see https://docs.apify.com/api/v2/request-queue-request-get
|
|
427
484
|
*/
|
|
428
|
-
async getRequest(id) {
|
|
429
|
-
|
|
485
|
+
async getRequest(id, options = {}) {
|
|
486
|
+
parseArgument(id, requestIdSchema);
|
|
487
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
430
488
|
const requestOpts = {
|
|
431
|
-
url: this.
|
|
489
|
+
url: this.buildUrl(['requests', id]),
|
|
432
490
|
method: 'GET',
|
|
433
|
-
|
|
434
|
-
|
|
491
|
+
timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
|
|
492
|
+
signal,
|
|
493
|
+
params: this.buildParams(),
|
|
435
494
|
};
|
|
436
495
|
try {
|
|
437
496
|
const response = await this.httpClient.call(requestOpts);
|
|
438
|
-
return (
|
|
497
|
+
return parseResponse(response, schemas.RequestResource());
|
|
439
498
|
}
|
|
440
499
|
catch (err) {
|
|
441
|
-
|
|
500
|
+
catchNotFoundOrThrow(err);
|
|
442
501
|
}
|
|
443
502
|
return undefined;
|
|
444
503
|
}
|
|
@@ -447,37 +506,44 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
447
506
|
*
|
|
448
507
|
* @param request - The updated request object (must include id)
|
|
449
508
|
* @param options - Update options such as whether to move to front
|
|
509
|
+
* @param options.forefront - If `true`, moves the request to the beginning of the queue. Default is `false`.
|
|
510
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
|
|
450
511
|
* @returns Information about the updated request
|
|
451
512
|
* @see https://docs.apify.com/api/v2/request-queue-request-put
|
|
452
513
|
*/
|
|
453
514
|
async updateRequest(request, options = {}) {
|
|
454
|
-
|
|
455
|
-
const parsed =
|
|
515
|
+
parseArgument(request, existingRequestSchema);
|
|
516
|
+
const parsed = parseArgument(options, forefrontOptionsSchema, 'RequestQueueClientAddRequestOptions');
|
|
456
517
|
const response = await this.httpClient.call({
|
|
457
|
-
url: this.
|
|
518
|
+
url: this.buildUrl(['requests', request.id]),
|
|
458
519
|
method: 'PUT',
|
|
459
|
-
|
|
520
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
|
|
521
|
+
signal: parsed.signal,
|
|
460
522
|
data: request,
|
|
461
|
-
params: this.
|
|
523
|
+
params: this.buildParams({
|
|
462
524
|
forefront: parsed.forefront,
|
|
463
|
-
clientKey: this
|
|
525
|
+
clientKey: this.#clientKey,
|
|
464
526
|
}),
|
|
465
527
|
});
|
|
466
|
-
return (
|
|
528
|
+
return parseResponse(response, schemas.RequestRegistration());
|
|
467
529
|
}
|
|
468
530
|
/**
|
|
469
531
|
* Deletes a specific request from the queue.
|
|
470
532
|
*
|
|
471
533
|
* @param id - Request ID
|
|
534
|
+
* @param options - Request options
|
|
535
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
472
536
|
*/
|
|
473
|
-
async deleteRequest(id) {
|
|
474
|
-
|
|
537
|
+
async deleteRequest(id, options = {}) {
|
|
538
|
+
parseArgument(id, requestIdSchema);
|
|
539
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
475
540
|
await this.httpClient.call({
|
|
476
|
-
url: this.
|
|
541
|
+
url: this.buildUrl(['requests', id]),
|
|
477
542
|
method: 'DELETE',
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
543
|
+
timeoutSecs: this.#resolveTimeout(timeoutSecs, 'short'),
|
|
544
|
+
signal,
|
|
545
|
+
params: this.buildParams({
|
|
546
|
+
clientKey: this.#clientKey,
|
|
481
547
|
}),
|
|
482
548
|
});
|
|
483
549
|
}
|
|
@@ -492,6 +558,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
492
558
|
* @param options - Lock extension options
|
|
493
559
|
* @param options.lockSecs - **Required.** New lock duration in seconds from now.
|
|
494
560
|
* @param options.forefront - If `true`, moves the request to the beginning of the queue when the lock expires. Default is `false`.
|
|
561
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'medium'`.
|
|
495
562
|
* @returns Object with new `lockExpiresAt` timestamp
|
|
496
563
|
* @see https://docs.apify.com/api/v2/request-queue-request-lock-put
|
|
497
564
|
*
|
|
@@ -507,19 +574,20 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
507
574
|
* @since Added in 2.4.1
|
|
508
575
|
*/
|
|
509
576
|
async prolongRequestLock(id, options) {
|
|
510
|
-
|
|
511
|
-
const parsed =
|
|
577
|
+
parseArgument(id, requestIdSchema);
|
|
578
|
+
const parsed = parseArgument(options, prolongRequestLockOptionsSchema, 'RequestQueueClientProlongRequestLockOptions');
|
|
512
579
|
const response = await this.httpClient.call({
|
|
513
|
-
url: this.
|
|
580
|
+
url: this.buildUrl(['requests', id, 'lock']),
|
|
514
581
|
method: 'PUT',
|
|
515
|
-
|
|
516
|
-
|
|
582
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'medium'),
|
|
583
|
+
signal: parsed.signal,
|
|
584
|
+
params: this.buildParams({
|
|
517
585
|
forefront: parsed.forefront,
|
|
518
586
|
lockSecs: parsed.lockSecs,
|
|
519
|
-
clientKey: this
|
|
587
|
+
clientKey: this.#clientKey,
|
|
520
588
|
}),
|
|
521
589
|
});
|
|
522
|
-
return (
|
|
590
|
+
return parseResponse(response, schemas.RequestLockInfo());
|
|
523
591
|
}
|
|
524
592
|
/**
|
|
525
593
|
* Releases the lock on a request, allowing other clients to process it.
|
|
@@ -529,19 +597,22 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
529
597
|
*
|
|
530
598
|
* @param id - Request ID
|
|
531
599
|
* @param options - Options such as whether to move to front
|
|
600
|
+
* @param options.forefront - If `true`, moves the request to the beginning of the queue. Default is `false`.
|
|
601
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'short'`.
|
|
532
602
|
* @see https://docs.apify.com/api/v2/request-queue-request-lock-delete
|
|
533
603
|
* @since Added in 2.4.1
|
|
534
604
|
*/
|
|
535
605
|
async deleteRequestLock(id, options = {}) {
|
|
536
|
-
|
|
537
|
-
const parsed =
|
|
606
|
+
parseArgument(id, requestIdSchema);
|
|
607
|
+
const parsed = parseArgument(options, forefrontOptionsSchema, 'RequestQueueClientDeleteRequestLockOptions');
|
|
538
608
|
await this.httpClient.call({
|
|
539
|
-
url: this.
|
|
609
|
+
url: this.buildUrl(['requests', id, 'lock']),
|
|
540
610
|
method: 'DELETE',
|
|
541
|
-
|
|
542
|
-
|
|
611
|
+
timeoutSecs: this.#resolveTimeout(parsed.timeoutSecs, 'short'),
|
|
612
|
+
signal: parsed.signal,
|
|
613
|
+
params: this.buildParams({
|
|
543
614
|
forefront: parsed.forefront,
|
|
544
|
-
clientKey: this
|
|
615
|
+
clientKey: this.#clientKey,
|
|
545
616
|
}),
|
|
546
617
|
});
|
|
547
618
|
}
|
|
@@ -552,26 +623,33 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
552
623
|
* queue contents.
|
|
553
624
|
*
|
|
554
625
|
* @param options - Pagination options
|
|
626
|
+
* @param options.chunkSize - Maximum number of requests fetched per API call when iterating across pages. `limit`
|
|
627
|
+
* then caps the total across all pages.
|
|
628
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
555
629
|
* @returns List of requests with pagination information
|
|
556
630
|
* @see https://docs.apify.com/api/v2/request-queue-requests-get
|
|
557
631
|
* @since Added in 2.5.1
|
|
558
632
|
*/
|
|
559
633
|
listRequests(options = {}) {
|
|
560
|
-
|
|
634
|
+
// `chunkSize` only sizes the page requests, and `timeoutSecs` and `signal` apply to every one of them; none of
|
|
635
|
+
// them is an API parameter, so they must not reach the query string.
|
|
636
|
+
const { chunkSize, timeoutSecs, signal, ...parsed } = parseArgument(options, listRequestsOptionsSchema, 'RequestQueueClientListRequestsOptions');
|
|
637
|
+
const pageTimeout = this.#resolveTimeout(timeoutSecs, 'medium');
|
|
561
638
|
const getPaginatedList = async (rqListOptions = {}) => {
|
|
562
639
|
const response = await this.httpClient.call({
|
|
563
|
-
url: this.
|
|
640
|
+
url: this.buildUrl('requests'),
|
|
564
641
|
method: 'GET',
|
|
565
|
-
|
|
566
|
-
|
|
642
|
+
timeoutSecs: pageTimeout,
|
|
643
|
+
signal,
|
|
644
|
+
params: this.buildParams({
|
|
567
645
|
...rqListOptions,
|
|
568
646
|
filter: rqListOptions.filter ? rqListOptions.filter.join(',') : undefined,
|
|
569
|
-
clientKey: this
|
|
647
|
+
clientKey: this.#clientKey,
|
|
570
648
|
}),
|
|
571
649
|
});
|
|
572
|
-
return (
|
|
650
|
+
return parseResponse(response, schemas.ListOfRequests());
|
|
573
651
|
};
|
|
574
|
-
const paginatedListPromise = getPaginatedList(parsed);
|
|
652
|
+
const paginatedListPromise = getPaginatedList({ ...parsed, limit: minForLimitParam(parsed.limit, chunkSize) });
|
|
575
653
|
async function* asyncGenerator() {
|
|
576
654
|
let currentPage = await paginatedListPromise;
|
|
577
655
|
yield* currentPage.items;
|
|
@@ -584,9 +662,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
584
662
|
) {
|
|
585
663
|
const newOptions = {
|
|
586
664
|
...parsed,
|
|
587
|
-
limit: remainingItems,
|
|
588
|
-
// remove original exclusiveStartId, if there was any, and use cursor-based pagination
|
|
589
|
-
exclusiveStartId: undefined,
|
|
665
|
+
limit: minForLimitParam(remainingItems, chunkSize),
|
|
590
666
|
cursor: currentPage.nextCursor,
|
|
591
667
|
};
|
|
592
668
|
currentPage = await getPaginatedList(newOptions);
|
|
@@ -606,20 +682,24 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
606
682
|
* This is useful for releasing all locks at once, for example when shutting down
|
|
607
683
|
* a crawler gracefully.
|
|
608
684
|
*
|
|
685
|
+
* @param options - Request options
|
|
686
|
+
* @param options.timeoutSecs - Timeout for the API request. Default is `'long'`.
|
|
609
687
|
* @returns Number of requests that were unlocked
|
|
610
688
|
* @see https://docs.apify.com/api/v2/request-queue-requests-unlock-post
|
|
611
689
|
* @since Added in 2.12.5
|
|
612
690
|
*/
|
|
613
|
-
async unlockRequests() {
|
|
691
|
+
async unlockRequests(options = {}) {
|
|
692
|
+
const { timeoutSecs, signal } = parseArgument(options, timeoutOptionsSchema, 'TimeoutOptions');
|
|
614
693
|
const response = await this.httpClient.call({
|
|
615
|
-
url: this.
|
|
694
|
+
url: this.buildUrl('requests/unlock'),
|
|
616
695
|
method: 'POST',
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
696
|
+
timeoutSecs: this.#resolveTimeout(timeoutSecs, 'long'),
|
|
697
|
+
signal,
|
|
698
|
+
params: this.buildParams({
|
|
699
|
+
clientKey: this.#clientKey,
|
|
620
700
|
}),
|
|
621
701
|
});
|
|
622
|
-
return (
|
|
702
|
+
return parseResponse(response, schemas.UnlockRequestsResult());
|
|
623
703
|
}
|
|
624
704
|
/**
|
|
625
705
|
* Returns an async iterable for paginating through all requests in the queue.
|
|
@@ -628,6 +708,7 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
628
708
|
* automatically handling pagination behind the scenes.
|
|
629
709
|
*
|
|
630
710
|
* @param options - Pagination options
|
|
711
|
+
* @param options.timeoutSecs - Timeout for each API request. Default is `'medium'`.
|
|
631
712
|
* @returns An async iterable of request pages
|
|
632
713
|
* @see https://docs.apify.com/api/v2/request-queue-requests-get
|
|
633
714
|
*
|
|
@@ -640,14 +721,12 @@ class RequestQueueClient extends resource_client_1.ResourceClient {
|
|
|
640
721
|
* @since Added in 2.5.1
|
|
641
722
|
*/
|
|
642
723
|
paginateRequests(options = {}) {
|
|
643
|
-
const { limit,
|
|
644
|
-
return new
|
|
645
|
-
getPage: async (pageOptions) => this.listRequests({ ...pageOptions, filter }),
|
|
724
|
+
const { limit, cursor, filter, maxPageLimit, timeoutSecs, signal } = parseArgument(options, paginateRequestsOptionsSchema, 'RequestQueueClientPaginateRequestsOptions');
|
|
725
|
+
return new RequestQueuePaginationIterator({
|
|
726
|
+
getPage: async (pageOptions) => this.listRequests({ ...pageOptions, filter, timeoutSecs, signal }),
|
|
646
727
|
limit,
|
|
647
|
-
exclusiveStartId,
|
|
648
728
|
cursor,
|
|
649
729
|
maxPageLimit,
|
|
650
730
|
});
|
|
651
731
|
}
|
|
652
732
|
}
|
|
653
|
-
exports.RequestQueueClient = RequestQueueClient;
|