|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +from apify_client import ApifyClient, ApifyClientAsync |
| 6 | +from apify_client.http_clients import HttpResponse |
| 7 | + |
| 8 | +if TYPE_CHECKING: |
| 9 | + from typing import Any |
| 10 | + |
| 11 | + from pytest_httpserver import HTTPServer |
| 12 | + |
| 13 | + from apify_client.http_clients import HttpClient, HttpClientAsync |
| 14 | + |
| 15 | + |
| 16 | +DATASET_ID = 'test-dataset-id' |
| 17 | +KVS_ID = 'test-kvs-id' |
| 18 | +RECORD_KEY = 'test-record-key' |
| 19 | +STREAM_CONTENT = b'[{"id": 1}]' |
| 20 | + |
| 21 | + |
| 22 | +def test_dataset_stream_items_sync( |
| 23 | + httpserver: HTTPServer, |
| 24 | + http_client_class: type[HttpClient], |
| 25 | +) -> None: |
| 26 | + """Dataset streams expose a transport-independent response that can be read synchronously.""" |
| 27 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 28 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 29 | + client = ApifyClient.with_custom_http_client( |
| 30 | + api_url=api_url, |
| 31 | + http_client=http_client_class(), |
| 32 | + ) |
| 33 | + |
| 34 | + with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 35 | + assert isinstance(response, HttpResponse) |
| 36 | + assert response.read() == STREAM_CONTENT |
| 37 | + |
| 38 | + |
| 39 | +async def test_dataset_stream_items_async( |
| 40 | + httpserver: HTTPServer, |
| 41 | + http_client_async_class: type[HttpClientAsync], |
| 42 | +) -> None: |
| 43 | + """Dataset streams expose a transport-independent response that can be read asynchronously.""" |
| 44 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 45 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 46 | + client = ApifyClientAsync.with_custom_http_client( |
| 47 | + api_url=api_url, |
| 48 | + http_client=http_client_async_class(), |
| 49 | + ) |
| 50 | + |
| 51 | + async with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 52 | + assert isinstance(response, HttpResponse) |
| 53 | + assert await response.aread() == STREAM_CONTENT |
| 54 | + |
| 55 | + |
| 56 | +def test_key_value_store_stream_record_sync( |
| 57 | + httpserver: HTTPServer, |
| 58 | + http_client_class: type[HttpClient], |
| 59 | +) -> None: |
| 60 | + """KVS streams require reading the generic response before consuming its content.""" |
| 61 | + httpserver.expect_request(f'/v2/key-value-stores/{KVS_ID}/records/{RECORD_KEY}').respond_with_data(STREAM_CONTENT) |
| 62 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 63 | + client = ApifyClient.with_custom_http_client( |
| 64 | + api_url=api_url, |
| 65 | + http_client=http_client_class(), |
| 66 | + ) |
| 67 | + |
| 68 | + with client.key_value_store(KVS_ID).stream_record(RECORD_KEY) as record: |
| 69 | + assert isinstance(record, dict) |
| 70 | + response = record['value'] |
| 71 | + assert isinstance(response, HttpResponse) |
| 72 | + assert response.read() == STREAM_CONTENT |
| 73 | + |
| 74 | + |
| 75 | +async def test_key_value_store_stream_record_async( |
| 76 | + httpserver: HTTPServer, |
| 77 | + http_client_async_class: type[HttpClientAsync], |
| 78 | +) -> None: |
| 79 | + """KVS streams require asynchronously reading the generic response before consuming its content.""" |
| 80 | + httpserver.expect_request(f'/v2/key-value-stores/{KVS_ID}/records/{RECORD_KEY}').respond_with_data(STREAM_CONTENT) |
| 81 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 82 | + client = ApifyClientAsync.with_custom_http_client( |
| 83 | + api_url=api_url, |
| 84 | + http_client=http_client_async_class(), |
| 85 | + ) |
| 86 | + |
| 87 | + async with client.key_value_store(KVS_ID).stream_record(RECORD_KEY) as record: |
| 88 | + assert isinstance(record, dict) |
| 89 | + response = record['value'] |
| 90 | + assert isinstance(response, HttpResponse) |
| 91 | + assert await response.aread() == STREAM_CONTENT |
| 92 | + |
| 93 | + |
| 94 | +def test_protocol_check_leaves_stream_unread_sync( |
| 95 | + httpserver: HTTPServer, |
| 96 | + http_client_class: type[HttpClient], |
| 97 | +) -> None: |
| 98 | + """Checking a streaming response against the protocol inspects it without pulling the body off the wire.""" |
| 99 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 100 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 101 | + client = ApifyClient.with_custom_http_client( |
| 102 | + api_url=api_url, |
| 103 | + http_client=http_client_class(), |
| 104 | + ) |
| 105 | + |
| 106 | + with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 107 | + assert isinstance(response, HttpResponse) |
| 108 | + # `is_stream_consumed` is transport state, not part of the protocol, but the built-in client exposes it. |
| 109 | + raw: Any = response |
| 110 | + assert raw.is_stream_consumed is False |
| 111 | + |
| 112 | + |
| 113 | +async def test_protocol_check_leaves_stream_unread_async( |
| 114 | + httpserver: HTTPServer, |
| 115 | + http_client_async_class: type[HttpClientAsync], |
| 116 | +) -> None: |
| 117 | + """Checking a streaming response against the protocol inspects it without pulling the body off the wire.""" |
| 118 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 119 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 120 | + client = ApifyClientAsync.with_custom_http_client( |
| 121 | + api_url=api_url, |
| 122 | + http_client=http_client_async_class(), |
| 123 | + ) |
| 124 | + |
| 125 | + async with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 126 | + assert isinstance(response, HttpResponse) |
| 127 | + # `is_stream_consumed` is transport state, not part of the protocol, but the built-in client exposes it. |
| 128 | + raw: Any = response |
| 129 | + assert raw.is_stream_consumed is False |
0 commit comments