Skip to content

refactor: extract shared logic from Arcjet and ArcjetSync into helper…#60

Closed
qw-in wants to merge 1 commit intoarcjet:mainfrom
qw-in:quinn/extract-shared-client-helpers
Closed

refactor: extract shared logic from Arcjet and ArcjetSync into helper…#60
qw-in wants to merge 1 commit intoarcjet:mainfrom
qw-in:quinn/extract-shared-client-helpers

Conversation

@qw-in
Copy link
Copy Markdown
Member

@qw-in qw-in commented Mar 4, 2026

… 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).

… 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).
@qw-in qw-in self-assigned this Mar 4, 2026
Copilot AI review requested due to automatic review settings March 4, 2026 21:42
@qw-in
Copy link
Copy Markdown
Member Author

qw-in commented Mar 4, 2026

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

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(), and arcjet_sync() to call the new helpers instead of duplicating logic.
  • Added a new tests/test_client_helpers.py test 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.

Comment on lines +429 to +437
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,
)
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copilot uses AI. Check for mistakes.
arcjet-rei added a commit that referenced this pull request Mar 13, 2026
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>
arcjet-rei added a commit that referenced this pull request Mar 13, 2026
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>
arcjet-rei added a commit that referenced this pull request Mar 16, 2026
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>
@qw-in qw-in closed this Mar 30, 2026
@qw-in
Copy link
Copy Markdown
Member Author

qw-in commented Mar 30, 2026

I took too long and don't think its worth it given the code changes

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.

3 participants