|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import asyncio |
3 | 4 | import json as jsonlib |
4 | 5 | from dataclasses import dataclass, field |
| 6 | +from datetime import timedelta |
| 7 | +from http.client import HTTPConnection |
5 | 8 | from typing import TYPE_CHECKING, Any |
| 9 | +from urllib.parse import urlsplit |
6 | 10 |
|
7 | 11 | import impit |
8 | 12 | import pytest |
@@ -142,6 +146,73 @@ async def call( |
142 | 146 | return _make_fake_response() |
143 | 147 |
|
144 | 148 |
|
| 149 | +def _stdlib_fetch( |
| 150 | + *, |
| 151 | + method: str, |
| 152 | + url: str, |
| 153 | + headers: dict[str, str], |
| 154 | + content: bytes | None, |
| 155 | + timeout: float | None, |
| 156 | +) -> FakeResponse: |
| 157 | + """Send one request over `http.client` and adapt the result to the `HttpResponse` protocol.""" |
| 158 | + parts = urlsplit(url) |
| 159 | + connection = HTTPConnection(parts.hostname or 'localhost', parts.port, timeout=timeout) |
| 160 | + try: |
| 161 | + path = f'{parts.path}?{parts.query}' if parts.query else parts.path |
| 162 | + connection.request(method, path, body=content, headers=headers) |
| 163 | + raw = connection.getresponse() |
| 164 | + body = raw.read() |
| 165 | + try: |
| 166 | + parsed = jsonlib.loads(body) |
| 167 | + except ValueError: |
| 168 | + parsed = None |
| 169 | + return FakeResponse( |
| 170 | + status_code=raw.status, |
| 171 | + text=body.decode(), |
| 172 | + content=body, |
| 173 | + headers={key.lower(): value for key, value in raw.getheaders()}, |
| 174 | + _json=parsed, |
| 175 | + ) |
| 176 | + finally: |
| 177 | + connection.close() |
| 178 | + |
| 179 | + |
| 180 | +class StdlibHttpClient(HttpClient): |
| 181 | + """A hooks-only custom sync client: implements `send_request` and inherits the shared pipeline.""" |
| 182 | + |
| 183 | + def send_request( |
| 184 | + self, |
| 185 | + *, |
| 186 | + method: str, |
| 187 | + url: str, |
| 188 | + headers: dict[str, str], |
| 189 | + content: bytes | None, |
| 190 | + timeout: float | None, |
| 191 | + stream: bool, |
| 192 | + ) -> HttpResponse: |
| 193 | + _ = stream |
| 194 | + return _stdlib_fetch(method=method, url=url, headers=headers, content=content, timeout=timeout) |
| 195 | + |
| 196 | + |
| 197 | +class StdlibHttpClientAsync(HttpClientAsync): |
| 198 | + """A hooks-only custom async client: implements `send_request` and inherits the shared pipeline.""" |
| 199 | + |
| 200 | + async def send_request( |
| 201 | + self, |
| 202 | + *, |
| 203 | + method: str, |
| 204 | + url: str, |
| 205 | + headers: dict[str, str], |
| 206 | + content: bytes | None, |
| 207 | + timeout: float | None, |
| 208 | + stream: bool, |
| 209 | + ) -> HttpResponse: |
| 210 | + _ = stream |
| 211 | + return await asyncio.to_thread( |
| 212 | + _stdlib_fetch, method=method, url=url, headers=headers, content=content, timeout=timeout |
| 213 | + ) |
| 214 | + |
| 215 | + |
145 | 216 | # -- Protocol conformance tests -- |
146 | 217 |
|
147 | 218 |
|
@@ -560,3 +631,97 @@ def test_custom_http_client_keeps_differently_cased_authorization() -> None: |
560 | 631 |
|
561 | 632 | assert 'Authorization' not in http_client._headers |
562 | 633 | assert http_client._headers['authorization'] == 'Bearer client_token' |
| 634 | + |
| 635 | + |
| 636 | +def test_hooks_only_client_retries_and_sends_default_headers(httpserver: HTTPServer) -> None: |
| 637 | + """A client implementing only `send_request` inherits header merging and 5xx retries from the shared `call`.""" |
| 638 | + auth_headers: list[str | None] = [] |
| 639 | + |
| 640 | + def handler(request: Request) -> Response: |
| 641 | + auth_headers.append(request.headers.get('Authorization')) |
| 642 | + if len(auth_headers) < 3: |
| 643 | + return Response( |
| 644 | + response='{"error": {"type": "internal-error", "message": "Server exploded."}}', |
| 645 | + status=500, |
| 646 | + content_type='application/json', |
| 647 | + ) |
| 648 | + return Response(response='{"data": {"id": "abc"}}', status=200, content_type='application/json') |
| 649 | + |
| 650 | + httpserver.expect_request('/v2/things/abc').respond_with_handler(handler) |
| 651 | + |
| 652 | + client = StdlibHttpClient(token='hook_token', min_delay_between_retries=timedelta(milliseconds=1)) |
| 653 | + response = client.call(method='GET', url=httpserver.url_for('/v2/things/abc')) |
| 654 | + |
| 655 | + assert response.status_code == 200 |
| 656 | + assert response.json() == {'data': {'id': 'abc'}} |
| 657 | + assert auth_headers == ['Bearer hook_token'] * 3 |
| 658 | + |
| 659 | + |
| 660 | +def test_hooks_only_client_raises_api_error_without_retry(httpserver: HTTPServer) -> None: |
| 661 | + """A non-retryable error status reaches the caller as `ApifyApiError` after a single attempt.""" |
| 662 | + request_count = 0 |
| 663 | + |
| 664 | + def handler(_request: Request) -> Response: |
| 665 | + nonlocal request_count |
| 666 | + request_count += 1 |
| 667 | + return Response( |
| 668 | + response='{"error": {"type": "record-not-found", "message": "Not there."}}', |
| 669 | + status=404, |
| 670 | + content_type='application/json', |
| 671 | + ) |
| 672 | + |
| 673 | + httpserver.expect_request('/v2/things/missing').respond_with_handler(handler) |
| 674 | + |
| 675 | + client = StdlibHttpClient(token='hook_token') |
| 676 | + with pytest.raises(ApifyApiError) as exc_info: |
| 677 | + client.call(method='GET', url=httpserver.url_for('/v2/things/missing')) |
| 678 | + |
| 679 | + assert exc_info.value.status_code == 404 |
| 680 | + assert request_count == 1 |
| 681 | + |
| 682 | + |
| 683 | +async def test_hooks_only_client_async_retries_and_sends_default_headers(httpserver: HTTPServer) -> None: |
| 684 | + """The async shared `call` gives a `send_request`-only client header merging and 5xx retries too.""" |
| 685 | + auth_headers: list[str | None] = [] |
| 686 | + |
| 687 | + def handler(request: Request) -> Response: |
| 688 | + auth_headers.append(request.headers.get('Authorization')) |
| 689 | + if len(auth_headers) < 3: |
| 690 | + return Response( |
| 691 | + response='{"error": {"type": "internal-error", "message": "Server exploded."}}', |
| 692 | + status=500, |
| 693 | + content_type='application/json', |
| 694 | + ) |
| 695 | + return Response(response='{"data": {"id": "abc"}}', status=200, content_type='application/json') |
| 696 | + |
| 697 | + httpserver.expect_request('/v2/things/abc').respond_with_handler(handler) |
| 698 | + |
| 699 | + client = StdlibHttpClientAsync(token='hook_token', min_delay_between_retries=timedelta(milliseconds=1)) |
| 700 | + response = await client.call(method='GET', url=httpserver.url_for('/v2/things/abc')) |
| 701 | + |
| 702 | + assert response.status_code == 200 |
| 703 | + assert response.json() == {'data': {'id': 'abc'}} |
| 704 | + assert auth_headers == ['Bearer hook_token'] * 3 |
| 705 | + |
| 706 | + |
| 707 | +async def test_hooks_only_client_async_raises_api_error_without_retry(httpserver: HTTPServer) -> None: |
| 708 | + """The async pipeline raises `ApifyApiError` without retrying a non-retryable status.""" |
| 709 | + request_count = 0 |
| 710 | + |
| 711 | + def handler(_request: Request) -> Response: |
| 712 | + nonlocal request_count |
| 713 | + request_count += 1 |
| 714 | + return Response( |
| 715 | + response='{"error": {"type": "record-not-found", "message": "Not there."}}', |
| 716 | + status=404, |
| 717 | + content_type='application/json', |
| 718 | + ) |
| 719 | + |
| 720 | + httpserver.expect_request('/v2/things/missing').respond_with_handler(handler) |
| 721 | + |
| 722 | + client = StdlibHttpClientAsync(token='hook_token') |
| 723 | + with pytest.raises(ApifyApiError) as exc_info: |
| 724 | + await client.call(method='GET', url=httpserver.url_for('/v2/things/missing')) |
| 725 | + |
| 726 | + assert exc_info.value.status_code == 404 |
| 727 | + assert request_count == 1 |
0 commit comments