refactor: extract shared logic from Arcjet and ArcjetSync into helper…#60
refactor: extract shared logic from Arcjet and ArcjetSync into helper…#60qw-in wants to merge 1 commit intoarcjet:mainfrom
Conversation
… functions The async Arcjet and sync ArcjetSync classes had near-identical implementations of protect(), with ~300 lines duplicated between them. The arcjet() and arcjet_sync() factory functions were similarly duplicated. Extract 9 shared helper functions that both clients now call: - _validate_ip_config: IP/proxy configuration validation - _prepare_protect_context: context coercion, email/message validation, token-bucket defaulting, extra-field merging, characteristic flattening - _build_decide_request: assemble DecideRequest protobuf - _build_cache_hit_report: build ReportRequest for cache hits - _log_cache_hit_report: debug logging for cache-hit reports - _handle_transport_error: transport error → Decision or raise - _handle_invalid_response: invalid response → Decision or raise - _finalize_decision: wrap response, cache, log timing - _compute_client_kwargs: shared factory function kwargs Each protect() method is reduced from ~300 lines to ~60 lines of unique async/sync-specific code. Add 62 new tests in test_client_helpers.py covering all extracted helpers using real protobuf types (no monkeypatched stubs).
|
Tried to reduce some of the duplication between the sync & async code paths. Not sure if its better but was quite low lift to get something out |
There was a problem hiding this comment.
Pull request overview
Refactors the Arcjet Python SDK client implementation by extracting duplicated async/sync protect() and factory logic into shared internal helper functions, and adds direct unit tests for those helpers using real protobuf types.
Changes:
- Added several shared internal helper functions to
src/arcjet/client.py(context preparation, request/report building, error handling, decision finalization, shared factory kwargs). - Updated
Arcjet.protect(),ArcjetSync.protect(),arcjet(), andarcjet_sync()to call the new helpers instead of duplicating logic. - Added a new
tests/test_client_helpers.pytest module covering the extracted helpers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/arcjet/client.py |
Introduces shared helper functions and rewires async/sync clients + factories to use them. |
tests/test_client_helpers.py |
Adds focused unit tests for the extracted client helper functions using real protobuf types. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| from arcjet.client import ( | ||
| _build_cache_hit_report, | ||
| _build_decide_request, | ||
| _finalize_decision, | ||
| _handle_invalid_response, | ||
| _handle_transport_error, | ||
| _log_cache_hit_report, | ||
| _sdk_stack, | ||
| ) |
There was a problem hiding this comment.
Top-level imports are split into two separate blocks (second block starts mid-file). Ruff is configured to enforce import formatting (isort via rule set "I"), so this will likely fail linting. Please merge these into a single import section at the top of the module (and keep ordering consistent).
Combine ./AGENTS.md and ./arcjet-analyze/AGENTS.md into a single top-level file. Remove stale information (PR #60 merge coordination), add witgen/tools directory layout, document that Ruff F401 rule matches GitHub Python code quality checks, and add arcjet-analyze test suite to the PR checklist. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Combine ./AGENTS.md and ./arcjet-analyze/AGENTS.md into a single top-level file. Remove stale information (PR #60 merge coordination), add witgen/tools directory layout, document that Ruff F401 rule matches GitHub Python code quality checks, and add arcjet-analyze test suite to the PR checklist. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Combine ./AGENTS.md and ./arcjet-analyze/AGENTS.md into a single top-level file. Remove stale information (PR #60 merge coordination), add witgen/tools directory layout, document that Ruff F401 rule matches GitHub Python code quality checks, and add arcjet-analyze test suite to the PR checklist. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
I took too long and don't think its worth it given the code changes |
… functions
The async Arcjet and sync ArcjetSync classes had near-identical implementations of protect(), with ~300 lines duplicated between them. The arcjet() and arcjet_sync() factory functions were similarly duplicated.
Extract 9 shared helper functions that both clients now call:
Each protect() method is reduced from ~300 lines to ~60 lines of unique async/sync-specific code. Add 62 new tests in test_client_helpers.py covering all extracted helpers using real protobuf types (no monkeypatched stubs).