Repository navigation
SCR-652: async requests and webhooks (submit/get/wait/callback status, signature verify) - #105
Merged
Merged
Conversation
- requests.submit (Prefer / X-Callback-URL / Idempotency-Key), requests.wait (honours Retry-After, raises AsyncRequestTimeoutError), requests.callback_status. AsyncRequest gains status, headers, body and replayed; running, data, credits_used and result_id are kept, and requests.get keeps its locked signature. - account.webhook_secret, rotate_webhook_secret and webhook_deliveries with typed models; 403 full_access_required maps to PermissionDeniedError. - scrapebadger.webhooks: verify_signature (constant time, optional sent_at tolerance) and parse_event -> WebhookEvent. - BaseClient._send: the shared retry loop with per-call headers. A submit without an idempotency key retries only transport errors raised before sending; an idempotent replay and a wait() poll never retry an error status. - account/client.py: the handwritten methods sit above the generated block, so sdk/codegen/facade rewrites only that block instead of the whole module. - Version 0.80.0 in pyproject, SDK_VERSION, __version__ (was stale at 0.77.0) and uv.lock; CHANGELOG and a README "Async requests and webhooks" section. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
4 tasks done
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.
Python SDK part of SCR-652 (system-wide async API). It follows
research/scr-652-async-api/CONTRACT.md("SDKs (Python)"). Other SCR-652 PRs: gateway, docs, Node SDK and MCP are separate tracks; the coordinator will cross-link them.What's new (0.80.0)
client.requestssubmit(path, *, method="GET", params=None, json=None, callback_url=None, wait=None, idempotency_key=None) -> AsyncRequest. It sendsPrefer: respond-async[, wait=N],X-Callback-URLandIdempotency-Key.status="running"; a 2xx withinwaitgivesstatus="completed".waitis validated to 0-120. With acallback_urlit defaults towait=0, sent explicitly so it also works against the current gateway. Otherwise the gateway default applies (25 s).idempotency_keyis validated to 1-200 visible ASCII characters.get(request_id) -> AsyncRequest. The signature is unchanged; it is in the surface lock.wait(request_id, *, timeout=600.0) -> AsyncRequest.retry_after_secondsor theRetry-Afterheader between polls (at least 1 s, cut to the time left).AsyncRequestTimeoutError, which is both aScrapeBadgerErrorand aTimeoutError.callback_status(request_id) -> CallbackStatus.AsyncRequestaddsstatus,headers,body(decoded JSON for a JSON response, else text) andreplayed(Idempotent-Replayed: true). The oldrunning,data,credits_usedandresult_idfields are kept, and the old constructor still works.client.accountwebhook_secret()androtate_webhook_secret()returnWebhookSecret.webhook_deliveries(limit=50)returnsWebhookDeliveries(a list ofWebhookDelivery).full_access_requirednow maps toPermissionDeniedError.get_the_webhook_signing_secretandrotate_the_webhook_signing_secretare kept.scrapebadger.webhooksverify_signature(secret, raw_body, signature, *, sent_at=None, tolerance_s=300) -> booluseshmac.compare_digestand never raises. It rejects asent_atmore thantolerance_sseconds in the past or future.parse_event(raw_body) -> WebhookEventcovers therequest.completedpayload fromlong-running-requests.mdx/callbacks.go.Exports and docs
scrapebadger.callback_url+idempotency_key, and a FastAPI receiver that verifies the signature.Retry behaviour (
BaseClient._send)The retry loop moved into
_send, which takes per-call headers._request_with_statusand everything else use it unchanged.ConnectError,ConnectTimeout,PoolTimeout,ProxyError). A lost 202 resent without a key would start and bill a second call.Codegen interaction
account/client.pyused to have the facade marker on its first line, sogen_python.pyrewrote the whole module on every codegen-v2 run and would have dropped hand-written methods.BEGIN … END generatedblock.gen_python.pyagainst this branch, with the spec plus/v1/account/webhook-deliveriesand/v1/requests/{id}/callback. The hand-written methods survive and only the block is regenerated./v1/requests/{request_id}/callback, codegen-v2 will create a stray, unregisteredscrapebadger/requests/module.client.requestsis unaffected. Adding the new methods tosdk/codegen/facade/python-v1-surface.jsonprevents this; the exact entries are in the coordinator report.Release notes
pyproject.toml,SDK_VERSION,__version__anduv.lock.__version__had been stale at 0.77.0. PyPI is at 0.79.0, so the contract's "0.55.0 -> 0.56.0" was outdated, and this is the equivalent minor bump.rufusclient (Generated v2 SDK from OpenAPI spec #103), which would ship with 0.80.0. The CHANGELOG mentions it.publish.ymlruns on a GitHub release (or a manual dispatch). Publish only after the gateway's SCR-652 release: until then,Idempotency-Keyis ignored by the API.Tests
tests/test_async_requests.py: 62 tests (respx). They cover submit 202 / inline 200 / POST body / headers, replays, 409 and 400 refusals, the transport-retry rules, get completed / running / text body, wait polling with Retry-After, timeout and the no-retry error path, callback_status, the account methods and 403 mapping, and verify_signature (valid, invalid, malformed, stale, future, constant-time) and parse_event.ruff check,ruff format --checkandmypy src/are clean.🤖 Generated with Claude Code