Skip to content

Commit 910919f

Browse files
committed
freeze max_retries
1 parent 0515df5 commit 910919f

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

src/crawlee/_request.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class RequestState(IntEnum):
3434
class CrawleeRequestData(BaseModel):
3535
"""Crawlee-specific configuration stored in the `user_data`."""
3636

37-
max_retries: Annotated[int | None, Field(alias='maxRetries')] = None
37+
max_retries: Annotated[int | None, Field(alias='maxRetries', frozen=True)] = None
3838
"""Maximum number of retries for this request. Allows to override the global `max_request_retries` option of
3939
`BasicCrawler`."""
4040

@@ -137,6 +137,8 @@ class RequestOptions(TypedDict):
137137
always_enqueue: NotRequired[bool]
138138
user_data: NotRequired[dict[str, JsonSerializable]]
139139
no_retry: NotRequired[bool]
140+
enqueue_strategy: NotRequired[EnqueueStrategy]
141+
max_retries: NotRequired[int | None]
140142

141143

142144
@docs_group('Storage data')
@@ -251,6 +253,8 @@ def from_url(
251253
keep_url_fragment: bool = False,
252254
use_extended_unique_key: bool = False,
253255
always_enqueue: bool = False,
256+
enqueue_strategy: EnqueueStrategy | None = None,
257+
max_retries: int | None = None,
254258
**kwargs: Any,
255259
) -> Self:
256260
"""Create a new `Request` instance from a URL.
@@ -278,6 +282,9 @@ def from_url(
278282
`unique_key` computation. This is only relevant when `unique_key` is not provided.
279283
always_enqueue: If set to `True`, the request will be enqueued even if it is already present in the queue.
280284
Using this is not allowed when a custom `unique_key` is also provided and will result in a `ValueError`.
285+
enqueue_strategy: The strategy that will be used for enqueuing the request.
286+
max_retries: Maximum number of retries for this request. Allows to override the global `max_request_retries`
287+
option of `BasicCrawler`.
281288
**kwargs: Additional request properties.
282289
"""
283290
if unique_key is not None and always_enqueue:
@@ -302,12 +309,27 @@ def from_url(
302309
if always_enqueue:
303310
unique_key = f'{crypto_random_object_id()}|{unique_key}'
304311

312+
user_data_dict = kwargs.pop('user_data', {}) or {}
313+
crawlee_data_dict = user_data_dict.get('__crawlee', {})
314+
315+
if max_retries is not None:
316+
crawlee_data_dict['maxRetries'] = max_retries
317+
318+
if enqueue_strategy is not None:
319+
crawlee_data_dict['enqueueStrategy'] = enqueue_strategy
320+
321+
crawlee_data = CrawleeRequestData(**crawlee_data_dict)
322+
323+
if crawlee_data:
324+
user_data_dict['__crawlee'] = crawlee_data
325+
305326
request = cls(
306327
url=url,
307328
unique_key=unique_key,
308329
method=method,
309330
headers=headers,
310331
payload=payload,
332+
user_data=user_data_dict,
311333
**kwargs,
312334
)
313335

@@ -366,10 +388,6 @@ def max_retries(self) -> int | None:
366388
"""Crawlee-specific limit on the number of retries of the request."""
367389
return self.crawlee_data.max_retries
368390

369-
@max_retries.setter
370-
def max_retries(self, new_max_retries: int) -> None:
371-
self.crawlee_data.max_retries = new_max_retries
372-
373391
@property
374392
def session_rotation_count(self) -> int | None:
375393
"""Crawlee-specific number of finished session rotations for the request."""

0 commit comments

Comments
 (0)