feat: support timeouts in the send_request context helper - #2174
Open
Battleplus wants to merge 1 commit into
Open
feat: support timeouts in the send_request context helper#2174Battleplus wants to merge 1 commit into
send_request context helper#2174Battleplus wants to merge 1 commit into
Conversation
Expose the HTTP clients' existing per-request `timeout` support through `SendRequestFunction` so request handlers can bound how long an extra HTTP call may take. Threads the value through `context.send_request()` into `HttpClient.send_request`. Also fixes `PlaywrightHttpClient` which passed `timedelta.total_seconds()` to Playwright's `APIRequestContext.fetch`, which expects milliseconds — previously any timeout would be ~1000x too short. Adds regression tests for both the forwarding and the unit conversion. Closes apify#2138 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2138
Summary
SendRequestFunction(used viacontext.send_request()in request handlers) only acceptedurl,method,payloadandheaders, so a handler could not bound how long an extra HTTP call may take. All four HTTP client implementations (HttpxHttpClient,CurlImpersonateHttpClient,ImpitHttpClient,PlaywrightHttpClient) already support a per-requesttimeout: timedelta | None— this PR just exposes it through the public contract.Changes
src/crawlee/_types.py: addtimeout: timedelta | None = Noneto theSendRequestFunctionprotocol + docstring.src/crawlee/crawlers/_basic/_basic_crawler.py: threadtimeoutthrough the_prepare_send_request_functionclosure intoHttpClient.send_request.src/crawlee/crawlers/_playwright/_playwright_http_client.py: fix —PlaywrightHttpClientpassedtimeout.total_seconds()to Playwright'sAPIRequestContext.fetch, which expects milliseconds. Without this fix, any timeout passed viacontext.send_request()on a Playwright crawler would be ~1000x too short (e.g.timedelta(seconds=12)→ 12ms).tests/unit/crawlers/_basic/test_basic_crawler.py: regression test —context.send_request('/slow?delay=2', timeout=timedelta(milliseconds=100))raisesasyncio.TimeoutError; plus a unit test assertingtimeoutis forwarded toHttpClient.send_request.tests/unit/crawlers/_playwright/test_playwright_http_client.py: regression test asserting the seconds→milliseconds conversion.Validation
pytest tests/unit/crawlers/_basic/test_basic_crawler.py→ 112 passed, 1 skippedruff check+ruff format --checkclean on all touched filesNote: no new dependency;
timeoutuses the existingtimedeltaconvention across the Python HTTP clients (the JS implementation names ittimeoutMillis, but the Python clients are timedelta-based).