Skip to content

Commit df7f517

Browse files
Centralize web fetch and search manager metadata
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 26b12af commit df7f517

9 files changed

Lines changed: 101 additions & 32 deletions

File tree

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ This runs lint, format checks, compile checks, tests, and package build.
144144
- `tests/test_team_route_constants_usage.py` (team manager route-constant usage enforcement),
145145
- `tests/test_tool_mapping_reader_usage.py` (tools mapping-helper usage),
146146
- `tests/test_type_utils_usage.py` (type `__mro__` boundary centralization in `hyperbrowser/type_utils.py`),
147+
- `tests/test_web_fetch_search_usage.py` (web fetch/search manager shared route/metadata/request-helper usage enforcement),
147148
- `tests/test_web_operation_metadata_usage.py` (web manager operation-metadata usage enforcement),
148149
- `tests/test_web_pagination_internal_reuse.py` (web pagination helper internal reuse of shared job pagination helpers),
149150
- `tests/test_web_payload_helper_usage.py` (web manager payload-helper usage enforcement),

‎hyperbrowser/client/managers/async_manager/web/__init__.py‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
WebSearchParams,
77
WebSearchResponse,
88
)
9-
from ...response_utils import parse_response_model
9+
from ...web_operation_metadata import WEB_REQUEST_OPERATION_METADATA
1010
from ...web_payload_utils import build_web_fetch_payload, build_web_search_payload
11+
from ...web_request_utils import start_web_job_async
12+
from ...web_route_constants import WEB_FETCH_ROUTE_PATH, WEB_SEARCH_ROUTE_PATH
1113

1214

1315
class WebManager:
16+
_OPERATION_METADATA = WEB_REQUEST_OPERATION_METADATA
17+
_FETCH_ROUTE_PATH = WEB_FETCH_ROUTE_PATH
18+
_SEARCH_ROUTE_PATH = WEB_SEARCH_ROUTE_PATH
19+
1420
def __init__(self, client):
1521
self._client = client
1622
self.batch_fetch = BatchFetchManager(client)
1723
self.crawl = WebCrawlManager(client)
1824

1925
async def fetch(self, params: FetchParams) -> FetchResponse:
2026
payload = build_web_fetch_payload(params)
21-
22-
response = await self._client.transport.post(
23-
self._client._build_url("/web/fetch"),
24-
data=payload,
25-
)
26-
return parse_response_model(
27-
response.data,
27+
return await start_web_job_async(
28+
client=self._client,
29+
route_prefix=self._FETCH_ROUTE_PATH,
30+
payload=payload,
2831
model=FetchResponse,
29-
operation_name="web fetch",
32+
operation_name=self._OPERATION_METADATA.fetch_operation_name,
3033
)
3134

3235
async def search(self, params: WebSearchParams) -> WebSearchResponse:
3336
payload = build_web_search_payload(params)
34-
response = await self._client.transport.post(
35-
self._client._build_url("/web/search"),
36-
data=payload,
37-
)
38-
return parse_response_model(
39-
response.data,
37+
return await start_web_job_async(
38+
client=self._client,
39+
route_prefix=self._SEARCH_ROUTE_PATH,
40+
payload=payload,
4041
model=WebSearchResponse,
41-
operation_name="web search",
42+
operation_name=self._OPERATION_METADATA.search_operation_name,
4243
)

‎hyperbrowser/client/managers/sync_manager/web/__init__.py‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,38 @@
66
WebSearchParams,
77
WebSearchResponse,
88
)
9-
from ...response_utils import parse_response_model
9+
from ...web_operation_metadata import WEB_REQUEST_OPERATION_METADATA
1010
from ...web_payload_utils import build_web_fetch_payload, build_web_search_payload
11+
from ...web_request_utils import start_web_job
12+
from ...web_route_constants import WEB_FETCH_ROUTE_PATH, WEB_SEARCH_ROUTE_PATH
1113

1214

1315
class WebManager:
16+
_OPERATION_METADATA = WEB_REQUEST_OPERATION_METADATA
17+
_FETCH_ROUTE_PATH = WEB_FETCH_ROUTE_PATH
18+
_SEARCH_ROUTE_PATH = WEB_SEARCH_ROUTE_PATH
19+
1420
def __init__(self, client):
1521
self._client = client
1622
self.batch_fetch = BatchFetchManager(client)
1723
self.crawl = WebCrawlManager(client)
1824

1925
def fetch(self, params: FetchParams) -> FetchResponse:
2026
payload = build_web_fetch_payload(params)
21-
22-
response = self._client.transport.post(
23-
self._client._build_url("/web/fetch"),
24-
data=payload,
25-
)
26-
return parse_response_model(
27-
response.data,
27+
return start_web_job(
28+
client=self._client,
29+
route_prefix=self._FETCH_ROUTE_PATH,
30+
payload=payload,
2831
model=FetchResponse,
29-
operation_name="web fetch",
32+
operation_name=self._OPERATION_METADATA.fetch_operation_name,
3033
)
3134

3235
def search(self, params: WebSearchParams) -> WebSearchResponse:
3336
payload = build_web_search_payload(params)
34-
response = self._client.transport.post(
35-
self._client._build_url("/web/search"),
36-
data=payload,
37-
)
38-
return parse_response_model(
39-
response.data,
37+
return start_web_job(
38+
client=self._client,
39+
route_prefix=self._SEARCH_ROUTE_PATH,
40+
payload=payload,
4041
model=WebSearchResponse,
41-
operation_name="web search",
42+
operation_name=self._OPERATION_METADATA.search_operation_name,
4243
)

‎hyperbrowser/client/managers/web_operation_metadata.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class WebOperationMetadata:
1010
operation_name_prefix: str
1111

1212

13+
@dataclass(frozen=True)
14+
class WebRequestOperationMetadata:
15+
fetch_operation_name: str
16+
search_operation_name: str
17+
18+
1319
BATCH_FETCH_OPERATION_METADATA = WebOperationMetadata(
1420
start_operation_name="batch fetch start",
1521
status_operation_name="batch fetch status",
@@ -25,3 +31,8 @@ class WebOperationMetadata:
2531
start_error_message="Failed to start web crawl job",
2632
operation_name_prefix="web crawl job ",
2733
)
34+
35+
WEB_REQUEST_OPERATION_METADATA = WebRequestOperationMetadata(
36+
fetch_operation_name="web fetch",
37+
search_operation_name="web search",
38+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
BATCH_FETCH_JOB_ROUTE_PREFIX = "/web/batch-fetch"
22
WEB_CRAWL_JOB_ROUTE_PREFIX = "/web/crawl"
3+
WEB_FETCH_ROUTE_PATH = "/web/fetch"
4+
WEB_SEARCH_ROUTE_PATH = "/web/search"

‎tests/test_architecture_marker_usage.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"tests/test_web_operation_metadata_usage.py",
7878
"tests/test_web_pagination_internal_reuse.py",
7979
"tests/test_web_payload_helper_usage.py",
80+
"tests/test_web_fetch_search_usage.py",
8081
"tests/test_web_request_helper_usage.py",
8182
"tests/test_web_route_constants_usage.py",
8283
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
pytestmark = pytest.mark.architecture
6+
7+
8+
MODULES = (
9+
"hyperbrowser/client/managers/sync_manager/web/__init__.py",
10+
"hyperbrowser/client/managers/async_manager/web/__init__.py",
11+
)
12+
13+
14+
def test_web_managers_use_shared_fetch_search_route_and_metadata_constants():
15+
for module_path in MODULES:
16+
module_text = Path(module_path).read_text(encoding="utf-8")
17+
assert "web_route_constants import WEB_FETCH_ROUTE_PATH, WEB_SEARCH_ROUTE_PATH" in module_text
18+
assert "web_operation_metadata import WEB_REQUEST_OPERATION_METADATA" in module_text
19+
assert "_FETCH_ROUTE_PATH = WEB_FETCH_ROUTE_PATH" in module_text
20+
assert "_SEARCH_ROUTE_PATH = WEB_SEARCH_ROUTE_PATH" in module_text
21+
assert "_OPERATION_METADATA = WEB_REQUEST_OPERATION_METADATA" in module_text
22+
assert '"/web/fetch"' not in module_text
23+
assert '"/web/search"' not in module_text
24+
assert 'operation_name="web fetch"' not in module_text
25+
assert 'operation_name="web search"' not in module_text
26+
27+
28+
def test_web_managers_use_shared_fetch_search_request_helpers():
29+
sync_text = Path("hyperbrowser/client/managers/sync_manager/web/__init__.py").read_text(
30+
encoding="utf-8"
31+
)
32+
async_text = Path("hyperbrowser/client/managers/async_manager/web/__init__.py").read_text(
33+
encoding="utf-8"
34+
)
35+
36+
assert "start_web_job(" in sync_text
37+
assert "_client.transport.post(" not in sync_text
38+
assert "parse_response_model(" not in sync_text
39+
40+
assert "start_web_job_async(" in async_text
41+
assert "_client.transport.post(" not in async_text
42+
assert "parse_response_model(" not in async_text

‎tests/test_web_operation_metadata.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from hyperbrowser.client.managers.web_operation_metadata import (
22
BATCH_FETCH_OPERATION_METADATA,
3+
WEB_REQUEST_OPERATION_METADATA,
34
WEB_CRAWL_OPERATION_METADATA,
45
)
56

@@ -24,3 +25,8 @@ def test_web_crawl_operation_metadata_values():
2425
== "Failed to start web crawl job"
2526
)
2627
assert WEB_CRAWL_OPERATION_METADATA.operation_name_prefix == "web crawl job "
28+
29+
30+
def test_web_request_operation_metadata_values():
31+
assert WEB_REQUEST_OPERATION_METADATA.fetch_operation_name == "web fetch"
32+
assert WEB_REQUEST_OPERATION_METADATA.search_operation_name == "web search"

‎tests/test_web_route_constants.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from hyperbrowser.client.managers.web_route_constants import (
22
BATCH_FETCH_JOB_ROUTE_PREFIX,
3+
WEB_FETCH_ROUTE_PATH,
34
WEB_CRAWL_JOB_ROUTE_PREFIX,
5+
WEB_SEARCH_ROUTE_PATH,
46
)
57

68

79
def test_web_route_constants_match_expected_api_paths():
810
assert BATCH_FETCH_JOB_ROUTE_PREFIX == "/web/batch-fetch"
911
assert WEB_CRAWL_JOB_ROUTE_PREFIX == "/web/crawl"
12+
assert WEB_FETCH_ROUTE_PATH == "/web/fetch"
13+
assert WEB_SEARCH_ROUTE_PATH == "/web/search"

0 commit comments

Comments
 (0)