Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/crawlee/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import logging
import re
from collections.abc import Awaitable, Coroutine, MutableMapping, Sequence
from datetime import timedelta

from typing_extensions import NotRequired, Required, Self, Unpack

Expand Down Expand Up @@ -558,6 +559,7 @@ def __call__(
method: HttpMethod = 'GET',
payload: HttpPayload | None = None,
headers: HttpHeaders | dict[str, str] | None = None,
timeout: timedelta | None = None,
) -> Coroutine[None, None, HttpResponse]:
"""Call send request function.

Expand All @@ -566,6 +568,7 @@ def __call__(
method: The HTTP method to use.
headers: The headers to include in the request.
payload: The payload to include in the request.
timeout: Maximum time allowed to process the request.

Returns:
The HTTP response received from the server.
Expand Down
2 changes: 2 additions & 0 deletions src/crawlee/crawlers/_basic/_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ async def send_request(
method: HttpMethod = 'GET',
payload: HttpPayload | None = None,
headers: HttpHeaders | dict[str, str] | None = None,
timeout: timedelta | None = None,
) -> HttpResponse:
return await self._http_client.send_request(
url=url,
Expand All @@ -1329,6 +1330,7 @@ async def send_request(
headers=headers,
session=session,
proxy_info=proxy_info,
timeout=timeout,
)

return send_request
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/crawlers/_basic/test_basic_crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,25 @@ async def handler(context: BasicCrawlingContext) -> None:
assert content_type == 'application/json'


async def test_send_request_respects_timeout(server_url: URL) -> None:
request_timed_out = asyncio.Event()

crawler = BasicCrawler(max_request_retries=3)

@crawler.router.default_handler
async def handler(context: BasicCrawlingContext) -> None:
with pytest.raises(asyncio.TimeoutError):
await context.send_request(
str(server_url / 'slow') + '?delay=2',
timeout=timedelta(milliseconds=100),
)
request_timed_out.set()

await crawler.run(['https://a.placeholder.com'])

assert request_timed_out.is_set()


@dataclass
class AddRequestsTestInput:
start_url: str
Expand Down