Skip to content

SCR-652: async requests and webhooks (submit/get/wait/callback status, signature verify) - #105

Merged
0xGhostCasper merged 1 commit into
mainfrom
0xGhostCasper/scr-652-async
Nov 18, 2026
Merged

0xGhostCasper merged 1 commit into
mainfrom
0xGhostCasper/scr-652-async

Conversation

@0xGhostCasper

Copy link
Copy Markdown
Member

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.requests

  • submit(path, *, method="GET", params=None, json=None, callback_url=None, wait=None, idempotency_key=None) -> AsyncRequest. It sends Prefer: respond-async[, wait=N], X-Callback-URL and Idempotency-Key.
    • A 202 gives status="running"; a 2xx within wait gives status="completed".
    • wait is validated to 0-120. With a callback_url it defaults to wait=0, sent explicitly so it also works against the current gateway. Otherwise the gateway default applies (25 s).
    • idempotency_key is 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.
    • It sleeps retry_after_seconds or the Retry-After header between polls (at least 1 s, cut to the time left).
    • It raises AsyncRequestTimeoutError, which is both a ScrapeBadgerError and a TimeoutError.
    • An error status is raised at once without status retries, because a stored result can't change.
  • callback_status(request_id) -> CallbackStatus.
  • AsyncRequest adds status, headers, body (decoded JSON for a JSON response, else text) and replayed (Idempotent-Replayed: true). The old running, data, credits_used and result_id fields are kept, and the old constructor still works.

client.account

  • webhook_secret() and rotate_webhook_secret() return WebhookSecret.
  • webhook_deliveries(limit=50) returns WebhookDeliveries (a list of WebhookDelivery).
  • 403 full_access_required now maps to PermissionDeniedError.
  • The generated get_the_webhook_signing_secret and rotate_the_webhook_signing_secret are kept.

scrapebadger.webhooks

  • verify_signature(secret, raw_body, signature, *, sent_at=None, tolerance_s=300) -> bool uses hmac.compare_digest and never raises. It rejects a sent_at more than tolerance_s seconds in the past or future.
  • parse_event(raw_body) -> WebhookEvent covers the request.completed payload from long-running-requests.mdx / callbacks.go.

Exports and docs

  • All of the above is exported from scrapebadger.
  • README section "Async requests and webhooks": a Perplexity submit with callback_url + idempotency_key, and a FastAPI receiver that verifies the signature.
  • CHANGELOG entry.

Retry behaviour (BaseClient._send)

The retry loop moved into _send, which takes per-call headers. _request_with_status and everything else use it unchanged.

  • An idempotent replay is never status-retried.
  • A submit without an idempotency key retries only transport errors raised before the request was sent (ConnectError, ConnectTimeout, PoolTimeout, ProxyError). A lost 202 resent without a key would start and bill a second call.

Codegen interaction

account/client.py used to have the facade marker on its first line, so gen_python.py rewrote the whole module on every codegen-v2 run and would have dropped hand-written methods.

  • The hand-written methods now sit above a BEGIN … END generated block.
  • I dry-ran gen_python.py against this branch, with the spec plus /v1/account/webhook-deliveries and /v1/requests/{id}/callback. The hand-written methods survive and only the block is regenerated.
  • Follow-up needed in the monorepo: once the spec has /v1/requests/{request_id}/callback, codegen-v2 will create a stray, unregistered scrapebadger/requests/ module. client.requests is unaffected. Adding the new methods to sdk/codegen/facade/python-v1-surface.json prevents this; the exact entries are in the coordinator report.

Release notes

  • Version 0.79.0 -> 0.80.0: pyproject.toml, SDK_VERSION, __version__ and uv.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.
  • main also carries the unreleased generated rufus client (Generated v2 SDK from OpenAPI spec #103), which would ship with 0.80.0. The CHANGELOG mentions it.
  • Merging does not publish. publish.yml runs on a GitHub release (or a manual dispatch). Publish only after the gateway's SCR-652 release: until then, Idempotency-Key is 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.
  • Locally: the full suite passes on Python 3.10, 3.12 and 3.14 (1007 tests), and ruff check, ruff format --check and mypy src/ are clean.

🤖 Generated with Claude Code

- 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

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.73913% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/scrapebadger/agent/client.py 85.54% 8 Missing and 4 partials ⚠️
src/scrapebadger/_internal/client.py 77.77% 4 Missing and 2 partials ⚠️
src/scrapebadger/agent/models.py 96.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@0xGhostCasper
0xGhostCasper merged commit 9b63274 into main Nov 18, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants