Skip to content

Commit 42af140

Browse files
Reuse shared job request helpers in web request utilities
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 4532485 commit 42af140

6 files changed

Lines changed: 137 additions & 195 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ This runs lint, format checks, compile checks, tests, and package build.
161161
- `tests/test_web_pagination_internal_reuse.py` (web pagination helper internal reuse of shared job pagination helpers),
162162
- `tests/test_web_payload_helper_usage.py` (web manager payload-helper usage enforcement),
163163
- `tests/test_web_request_helper_usage.py` (web manager request-helper usage enforcement),
164+
- `tests/test_web_request_internal_reuse.py` (web request helper internal reuse of shared job request helpers),
164165
- `tests/test_web_route_constants_usage.py` (web manager route-constant usage enforcement).
165166

166167
## Code quality conventions

hyperbrowser/client/managers/web_request_utils.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from typing import Any, Dict, Optional, Type, TypeVar
22

3-
from .job_route_builders import build_job_route, build_job_status_route
4-
from .response_utils import parse_response_model
3+
from .job_request_utils import (
4+
get_job,
5+
get_job_async,
6+
get_job_status,
7+
get_job_status_async,
8+
start_job,
9+
start_job_async,
10+
)
511

612
T = TypeVar("T")
713

@@ -14,12 +20,10 @@ def start_web_job(
1420
model: Type[T],
1521
operation_name: str,
1622
) -> T:
17-
response = client.transport.post(
18-
client._build_url(route_prefix),
19-
data=payload,
20-
)
21-
return parse_response_model(
22-
response.data,
23+
return start_job(
24+
client=client,
25+
route_prefix=route_prefix,
26+
payload=payload,
2327
model=model,
2428
operation_name=operation_name,
2529
)
@@ -33,11 +37,10 @@ def get_web_job_status(
3337
model: Type[T],
3438
operation_name: str,
3539
) -> T:
36-
response = client.transport.get(
37-
client._build_url(build_job_status_route(route_prefix, job_id)),
38-
)
39-
return parse_response_model(
40-
response.data,
40+
return get_job_status(
41+
client=client,
42+
route_prefix=route_prefix,
43+
job_id=job_id,
4144
model=model,
4245
operation_name=operation_name,
4346
)
@@ -52,12 +55,11 @@ def get_web_job(
5255
model: Type[T],
5356
operation_name: str,
5457
) -> T:
55-
response = client.transport.get(
56-
client._build_url(build_job_route(route_prefix, job_id)),
58+
return get_job(
59+
client=client,
60+
route_prefix=route_prefix,
61+
job_id=job_id,
5762
params=params,
58-
)
59-
return parse_response_model(
60-
response.data,
6163
model=model,
6264
operation_name=operation_name,
6365
)
@@ -71,12 +73,10 @@ async def start_web_job_async(
7173
model: Type[T],
7274
operation_name: str,
7375
) -> T:
74-
response = await client.transport.post(
75-
client._build_url(route_prefix),
76-
data=payload,
77-
)
78-
return parse_response_model(
79-
response.data,
76+
return await start_job_async(
77+
client=client,
78+
route_prefix=route_prefix,
79+
payload=payload,
8080
model=model,
8181
operation_name=operation_name,
8282
)
@@ -90,11 +90,10 @@ async def get_web_job_status_async(
9090
model: Type[T],
9191
operation_name: str,
9292
) -> T:
93-
response = await client.transport.get(
94-
client._build_url(build_job_status_route(route_prefix, job_id)),
95-
)
96-
return parse_response_model(
97-
response.data,
93+
return await get_job_status_async(
94+
client=client,
95+
route_prefix=route_prefix,
96+
job_id=job_id,
9897
model=model,
9998
operation_name=operation_name,
10099
)
@@ -109,12 +108,11 @@ async def get_web_job_async(
109108
model: Type[T],
110109
operation_name: str,
111110
) -> T:
112-
response = await client.transport.get(
113-
client._build_url(build_job_route(route_prefix, job_id)),
111+
return await get_job_async(
112+
client=client,
113+
route_prefix=route_prefix,
114+
job_id=job_id,
114115
params=params,
115-
)
116-
return parse_response_model(
117-
response.data,
118116
model=model,
119117
operation_name=operation_name,
120118
)

tests/test_architecture_marker_usage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
"tests/test_web_pagination_internal_reuse.py",
9191
"tests/test_web_payload_helper_usage.py",
9292
"tests/test_web_fetch_search_usage.py",
93+
"tests/test_web_request_internal_reuse.py",
9394
"tests/test_web_request_helper_usage.py",
9495
"tests/test_web_route_constants_usage.py",
9596
)

tests/test_job_route_builder_usage.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@
55
pytestmark = pytest.mark.architecture
66

77

8-
MODULES = (
9-
"hyperbrowser/client/managers/job_request_utils.py",
10-
"hyperbrowser/client/managers/web_request_utils.py",
11-
)
8+
def test_job_request_helpers_use_route_builders():
9+
module_text = Path("hyperbrowser/client/managers/job_request_utils.py").read_text(
10+
encoding="utf-8"
11+
)
12+
assert (
13+
"job_route_builders import build_job_route, build_job_status_route"
14+
in module_text
15+
)
16+
assert "build_job_route(route_prefix, job_id)" in module_text
17+
assert "build_job_status_route(route_prefix, job_id)" in module_text
18+
assert 'f"{route_prefix}/{job_id}"' not in module_text
19+
assert 'f"{route_prefix}/{job_id}/status"' not in module_text
1220

1321

14-
def test_job_and_web_request_helpers_use_route_builders():
15-
for module_path in MODULES:
16-
module_text = Path(module_path).read_text(encoding="utf-8")
17-
assert "job_route_builders import build_job_route, build_job_status_route" in module_text
18-
assert "build_job_route(route_prefix, job_id)" in module_text
19-
assert "build_job_status_route(route_prefix, job_id)" in module_text
20-
assert 'f"{route_prefix}/{job_id}"' not in module_text
21-
assert 'f"{route_prefix}/{job_id}/status"' not in module_text
22+
def test_web_request_helpers_reuse_job_request_helpers():
23+
module_text = Path("hyperbrowser/client/managers/web_request_utils.py").read_text(
24+
encoding="utf-8"
25+
)
26+
assert "job_request_utils import" in module_text
27+
assert "start_job(" in module_text
28+
assert "get_job_status(" in module_text
29+
assert "get_job(" in module_text
30+
assert "start_job_async(" in module_text
31+
assert "get_job_status_async(" in module_text
32+
assert "get_job_async(" in module_text
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
pytestmark = pytest.mark.architecture
6+
7+
8+
def test_web_request_utils_reuse_job_request_helpers():
9+
module_text = Path("hyperbrowser/client/managers/web_request_utils.py").read_text(
10+
encoding="utf-8"
11+
)
12+
assert "job_request_utils import" in module_text
13+
assert "start_job(" in module_text
14+
assert "get_job_status(" in module_text
15+
assert "get_job(" in module_text
16+
assert "start_job_async(" in module_text
17+
assert "get_job_status_async(" in module_text
18+
assert "get_job_async(" in module_text
19+
assert "client.transport." not in module_text
20+
assert "parse_response_model(" not in module_text

0 commit comments

Comments
 (0)