Skip to content

feat!: Make HTTP client pluggable with abstract base classes#641

Merged
vdusek merged 5 commits intomasterfrom
feature/pluggable-http-client
Feb 26, 2026
Merged

feat!: Make HTTP client pluggable with abstract base classes#641
vdusek merged 5 commits intomasterfrom
feature/pluggable-http-client

Conversation

@vdusek
Copy link
Contributor

@vdusek vdusek commented Feb 24, 2026

Summary

  • Introduce HttpClient and HttpClientAsync ABCs that users can extend to provide custom HTTP client implementations via ApifyClient(http_client=...) and ApifyClientAsync(http_client=...)
  • Factor impit-specific logic into ImpitHttpClient/ImpitHttpClientAsync, keeping _base.py free of any impit dependency
  • Add HttpResponse protocol with full streaming support (iter_bytes, aiter_bytes, read, aread, close, aclose)
  • Export all public types (HttpClient, HttpClientAsync, HttpResponse, ImpitHttpClient, ImpitHttpClientAsync) from apify_client

Issue

Test plan

  • CI passes

@vdusek vdusek added adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. labels Feb 24, 2026
@vdusek vdusek self-assigned this Feb 24, 2026
@vdusek vdusek requested a review from Pijukatel February 24, 2026 10:08
@github-actions github-actions bot added this to the 135th sprint - Tooling team milestone Feb 24, 2026
@github-actions github-actions bot added the tested Temporary label used only programatically for some analytics. label Feb 24, 2026
@codecov
Copy link

codecov bot commented Feb 24, 2026

Codecov Report

❌ Patch coverage is 91.26214% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.59%. Comparing base (230bb64) to head (17f504b).
⚠️ Report is 5 commits behind head on master.

Files with missing lines Patch % Lines
src/apify_client/_apify_client.py 90.69% 4 Missing ⚠️
src/apify_client/_utils.py 62.50% 3 Missing ⚠️
src/apify_client/_http_clients/_base.py 93.93% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #641      +/-   ##
==========================================
- Coverage   96.62%   96.59%   -0.04%     
==========================================
  Files          45       46       +1     
  Lines        4271     4433     +162     
==========================================
+ Hits         4127     4282     +155     
- Misses        144      151       +7     
Flag Coverage Δ
integration 94.76% <82.52%> (+0.08%) ⬆️
unit 76.51% <91.26%> (+0.91%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vdusek vdusek changed the title refactor!: Make HTTP client pluggable with abstract base classes feat!: Make HTTP client pluggable with abstract base classes Feb 24, 2026
@vdusek vdusek requested a review from janbuchar February 24, 2026 10:20
@Pijukatel Pijukatel requested a review from Copilot February 25, 2026 08:16
Copy link

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

This PR makes the Apify Python client’s HTTP transport layer pluggable by introducing public abstract base classes (HttpClient, HttpClientAsync) and an HttpResponse protocol, while moving the default impit-backed implementation into ImpitHttpClient / ImpitHttpClientAsync. It also wires the new http_client=... injection into ApifyClient / ApifyClientAsync and updates type hints and tests accordingly.

Changes:

  • Add HttpResponse protocol plus HttpClient / HttpClientAsync ABCs, and move impit implementation into ImpitHttpClient / ImpitHttpClientAsync.
  • Allow passing a custom http_client into ApifyClient and ApifyClientAsync, and export the new public HTTP types from apify_client.
  • Update resource-client type hints and expand unit tests to cover custom client behavior and streaming.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/unit/test_pluggable_http_client.py New unit tests validating ABC/protocol conformance and custom http_client injection behavior (sync + async).
tests/unit/test_http_clients.py Updates tests to target ImpitHttpClient* for retry logic and adds concrete ABC implementations for base-helper testing.
tests/unit/test_client_timeouts.py Switches tests to use ImpitHttpClient* (since HttpClient* are now abstract).
tests/unit/test_client_headers.py Switches tests to use ImpitHttpClient* for header behavior.
tests/unit/test_client_errors.py Switches tests to use ImpitHttpClient* and keeps streamed-error coverage.
src/apify_client/errors.py Generalizes error types to accept HttpResponse instead of impit.Response.
src/apify_client/_utils.py Generalizes response-parsing helpers to HttpResponse and updates docstrings accordingly.
src/apify_client/_resource_clients/task.py Docstring formatting adjustment (no functional behavior change).
src/apify_client/_resource_clients/log.py Generalizes streaming return types to HttpResponse.
src/apify_client/_resource_clients/key_value_store.py Generalizes record parsing function signature to HttpResponse.
src/apify_client/_resource_clients/dataset.py Generalizes streaming item types to HttpResponse.
src/apify_client/_http_clients/_impit.py Introduces ImpitHttpClient* and factors retryability logic; keeps default impit implementation.
src/apify_client/_http_clients/_base.py Adds HttpResponse protocol and HttpClient* ABCs; keeps shared request-prep helpers in _HttpClientBase.
src/apify_client/_http_clients/init.py Re-exports HttpClient*, HttpResponse, and default ImpitHttpClient*.
src/apify_client/_apify_client.py Adds http_client parameter, defaults to ImpitHttpClient*, and exposes http_client property.
src/apify_client/init.py Exports the new HTTP types from the public apify_client package.
Comments suppressed due to low confidence (2)

src/apify_client/_http_clients/_impit.py:16

  • _impit.py imports HttpClient/HttpClientAsync from the package (apify_client._http_clients), but that package’s __init__ also imports _impit, creating an avoidable circular import that can become fragile depending on import order. Import these symbols directly from ._base (and HttpResponse from ._base under TYPE_CHECKING) to break the cycle.
    src/apify_client/_http_clients/_impit.py:44
  • There are now two sources of truth for retryability (apify_client._utils.is_retryable_error and _http_clients._impit._is_retryable_error) with identical logic. This duplication risks the rules drifting over time; consider reusing the existing helper from _utils here, or otherwise removing/consolidating one of them so tests and the impit client validate the same implementation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@Pijukatel Pijukatel left a comment

Choose a reason for hiding this comment

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

Nice. The only drawback I see is the initialization with conflicting arguments. Maybe we can figure out something better.

@vdusek vdusek force-pushed the feature/pluggable-http-client branch 2 times, most recently from 0b8ab15 to 46693a8 Compare February 25, 2026 10:17
- Introduce `HttpClient` and `HttpClientAsync` ABCs that users can extend to provide custom HTTP client implementations via `ApifyClient(http_client=...)` and `ApifyClientAsync(http_client=...)`
- Factor impit-specific logic into `ImpitHttpClient`/`ImpitHttpClientAsync`, keeping `_base.py` free of any `impit` dependency
- Add `HttpResponse` protocol with full streaming support (`iter_bytes`, `aiter_bytes`, `read`, `aread`, `close`, `aclose`)
- Export all public types (`HttpClient`, `HttpClientAsync`, `HttpResponse`, `ImpitHttpClient`, `ImpitHttpClientAsync`) from `apify_client`
@vdusek vdusek force-pushed the feature/pluggable-http-client branch from 5e574a3 to fe5c92f Compare February 25, 2026 14:21
@vdusek vdusek requested a review from Pijukatel February 25, 2026 14:21
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vdusek vdusek merged commit 5ae33a0 into master Feb 26, 2026
24 of 26 checks passed
@vdusek vdusek deleted the feature/pluggable-http-client branch February 26, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

adhoc Ad-hoc unplanned task added during the sprint. t-tooling Issues with this label are in the ownership of the tooling team. tested Temporary label used only programatically for some analytics.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow to pass own http_client to ApifyClient and ApifyClientAsync

4 participants