Skip to content

Commit 3445ff7

Browse files
authored
feat: add filter and cursor parameters to list_requests method (#743)
add support for endpoint parameters and new pagination method added in apify/apify-core#12115 There is a new parameter `filter` for the _request queue -> list requests_ endpoint. To properly support pagination, we also added proper cursor-based pagination, with opaque cursor provided by the server.
1 parent 53736ef commit 3445ff7

1 file changed

Lines changed: 47 additions & 7 deletions

File tree

src/apify_client/_resource_clients/request_queue.py

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import warnings
66
from collections.abc import Iterable
77
from queue import Queue
8-
from typing import TYPE_CHECKING, Any
8+
from typing import TYPE_CHECKING, Any, Literal
99

1010
from more_itertools import constrained_batches
1111

@@ -495,19 +495,39 @@ def list_requests(
495495
self,
496496
*,
497497
limit: int | None = None,
498-
exclusive_start_id: str | None = None,
498+
filter: list[Literal['pending', 'locked']] | None = None, # noqa: A002
499499
timeout: Timeout = 'medium',
500+
cursor: str | None = None,
501+
exclusive_start_id: str | None = None,
500502
) -> ListOfRequests:
501503
"""List requests in the queue.
502504
503505
https://docs.apify.com/api/v2#/reference/request-queues/request-collection/list-requests
504506
505507
Args:
506508
limit: How many requests to retrieve.
507-
exclusive_start_id: All requests up to this one (including) are skipped from the result.
509+
filter: List of request states to use as a filter. Multiple values mean union of the given filters.
508510
timeout: Timeout for the API HTTP request.
511+
cursor: A token returned in previous API response, to continue listing next page of requests
512+
exclusive_start_id: (deprecated) All requests up to this one (including) are skipped from the result.
509513
"""
510-
request_params = self._build_params(limit=limit, exclusiveStartId=exclusive_start_id, clientKey=self.client_key)
514+
if exclusive_start_id and cursor:
515+
raise ValueError('Cannot use both `exclusive_start_id` and `cursor` for paginating requests.')
516+
517+
if exclusive_start_id is not None:
518+
warnings.warn(
519+
'`exclusive_start_id` is deprecated for paginating requests. Use pagination using `cursor` instead.',
520+
DeprecationWarning,
521+
stacklevel=2,
522+
)
523+
524+
request_params = self._build_params(
525+
limit=limit,
526+
filter=','.join(filter) if filter else None,
527+
clientKey=self.client_key,
528+
exclusiveStartId=exclusive_start_id,
529+
cursor=cursor,
530+
)
511531

512532
response = self._http_client.call(
513533
url=self._build_url('requests'),
@@ -1033,19 +1053,39 @@ async def list_requests(
10331053
self,
10341054
*,
10351055
limit: int | None = None,
1036-
exclusive_start_id: str | None = None,
1056+
filter: list[Literal['pending', 'locked']] | None = None, # noqa: A002
10371057
timeout: Timeout = 'medium',
1058+
cursor: str | None = None,
1059+
exclusive_start_id: str | None = None,
10381060
) -> ListOfRequests:
10391061
"""List requests in the queue.
10401062
10411063
https://docs.apify.com/api/v2#/reference/request-queues/request-collection/list-requests
10421064
10431065
Args:
10441066
limit: How many requests to retrieve.
1045-
exclusive_start_id: All requests up to this one (including) are skipped from the result.
1067+
filter: List of request states to use as a filter. Multiple values mean union of the given filters.
10461068
timeout: Timeout for the API HTTP request.
1069+
cursor: A token returned in previous API response, to continue listing next page of requests
1070+
exclusive_start_id: (deprecated) All requests up to this one (including) are skipped from the result.
10471071
"""
1048-
request_params = self._build_params(limit=limit, exclusiveStartId=exclusive_start_id, clientKey=self.client_key)
1072+
if exclusive_start_id and cursor:
1073+
raise ValueError('Cannot use both `exclusive_start_id` and `cursor` for paginating requests.')
1074+
1075+
if exclusive_start_id is not None:
1076+
warnings.warn(
1077+
'`exclusive_start_id` is deprecated for paginating requests. Use pagination using `cursor` instead.',
1078+
DeprecationWarning,
1079+
stacklevel=2,
1080+
)
1081+
1082+
request_params = self._build_params(
1083+
limit=limit,
1084+
filter=','.join(filter) if filter else None,
1085+
clientKey=self.client_key,
1086+
exclusiveStartId=exclusive_start_id,
1087+
cursor=cursor,
1088+
)
10491089

10501090
response = await self._http_client.call(
10511091
url=self._build_url('requests'),

0 commit comments

Comments
 (0)