Skip to content

Commit cf34a0b

Browse files
Refactor manager serialization to shared helper
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent e6ff900 commit cf34a0b

24 files changed

+156
-374
lines changed

hyperbrowser/client/managers/async_manager/agents/browser_use.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
)
88
from ....schema_utils import resolve_schema_input
99
from ...response_utils import parse_response_model
10+
from ...serialization_utils import serialize_model_dump_to_dict
1011

1112
from .....models import (
1213
POLLING_ATTEMPTS,
@@ -16,7 +17,6 @@
1617
StartBrowserUseTaskParams,
1718
StartBrowserUseTaskResponse,
1819
)
19-
from .....exceptions import HyperbrowserError
2020

2121

2222
class BrowserUseManager:
@@ -26,17 +26,10 @@ def __init__(self, client):
2626
async def start(
2727
self, params: StartBrowserUseTaskParams
2828
) -> StartBrowserUseTaskResponse:
29-
try:
30-
payload = params.model_dump(exclude_none=True, by_alias=True)
31-
except HyperbrowserError:
32-
raise
33-
except Exception as exc:
34-
raise HyperbrowserError(
35-
"Failed to serialize browser-use start params",
36-
original_error=exc,
37-
) from exc
38-
if type(payload) is not dict:
39-
raise HyperbrowserError("Failed to serialize browser-use start params")
29+
payload = serialize_model_dump_to_dict(
30+
params,
31+
error_message="Failed to serialize browser-use start params",
32+
)
4033
if params.output_model_schema:
4134
payload["outputModelSchema"] = resolve_schema_input(
4235
params.output_model_schema

hyperbrowser/client/managers/async_manager/agents/claude_computer_use.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
wait_for_job_result_async,
77
)
88
from ...response_utils import parse_response_model
9-
from .....exceptions import HyperbrowserError
9+
from ...serialization_utils import serialize_model_dump_to_dict
1010

1111
from .....models import (
1212
POLLING_ATTEMPTS,
@@ -25,19 +25,10 @@ def __init__(self, client):
2525
async def start(
2626
self, params: StartClaudeComputerUseTaskParams
2727
) -> StartClaudeComputerUseTaskResponse:
28-
try:
29-
payload = params.model_dump(exclude_none=True, by_alias=True)
30-
except HyperbrowserError:
31-
raise
32-
except Exception as exc:
33-
raise HyperbrowserError(
34-
"Failed to serialize Claude Computer Use start params",
35-
original_error=exc,
36-
) from exc
37-
if type(payload) is not dict:
38-
raise HyperbrowserError(
39-
"Failed to serialize Claude Computer Use start params"
40-
)
28+
payload = serialize_model_dump_to_dict(
29+
params,
30+
error_message="Failed to serialize Claude Computer Use start params",
31+
)
4132
response = await self._client.transport.post(
4233
self._client._build_url("/task/claude-computer-use"),
4334
data=payload,

hyperbrowser/client/managers/async_manager/agents/cua.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
wait_for_job_result_async,
77
)
88
from ...response_utils import parse_response_model
9-
from .....exceptions import HyperbrowserError
9+
from ...serialization_utils import serialize_model_dump_to_dict
1010

1111
from .....models import (
1212
POLLING_ATTEMPTS,
@@ -23,17 +23,10 @@ def __init__(self, client):
2323
self._client = client
2424

2525
async def start(self, params: StartCuaTaskParams) -> StartCuaTaskResponse:
26-
try:
27-
payload = params.model_dump(exclude_none=True, by_alias=True)
28-
except HyperbrowserError:
29-
raise
30-
except Exception as exc:
31-
raise HyperbrowserError(
32-
"Failed to serialize CUA start params",
33-
original_error=exc,
34-
) from exc
35-
if type(payload) is not dict:
36-
raise HyperbrowserError("Failed to serialize CUA start params")
26+
payload = serialize_model_dump_to_dict(
27+
params,
28+
error_message="Failed to serialize CUA start params",
29+
)
3730
response = await self._client.transport.post(
3831
self._client._build_url("/task/cua"),
3932
data=payload,

hyperbrowser/client/managers/async_manager/agents/gemini_computer_use.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
wait_for_job_result_async,
77
)
88
from ...response_utils import parse_response_model
9-
from .....exceptions import HyperbrowserError
9+
from ...serialization_utils import serialize_model_dump_to_dict
1010

1111
from .....models import (
1212
POLLING_ATTEMPTS,
@@ -25,19 +25,10 @@ def __init__(self, client):
2525
async def start(
2626
self, params: StartGeminiComputerUseTaskParams
2727
) -> StartGeminiComputerUseTaskResponse:
28-
try:
29-
payload = params.model_dump(exclude_none=True, by_alias=True)
30-
except HyperbrowserError:
31-
raise
32-
except Exception as exc:
33-
raise HyperbrowserError(
34-
"Failed to serialize Gemini Computer Use start params",
35-
original_error=exc,
36-
) from exc
37-
if type(payload) is not dict:
38-
raise HyperbrowserError(
39-
"Failed to serialize Gemini Computer Use start params"
40-
)
28+
payload = serialize_model_dump_to_dict(
29+
params,
30+
error_message="Failed to serialize Gemini Computer Use start params",
31+
)
4132
response = await self._client.transport.post(
4233
self._client._build_url("/task/gemini-computer-use"),
4334
data=payload,

hyperbrowser/client/managers/async_manager/agents/hyper_agent.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
wait_for_job_result_async,
77
)
88
from ...response_utils import parse_response_model
9-
from .....exceptions import HyperbrowserError
9+
from ...serialization_utils import serialize_model_dump_to_dict
1010

1111
from .....models import (
1212
POLLING_ATTEMPTS,
@@ -25,17 +25,10 @@ def __init__(self, client):
2525
async def start(
2626
self, params: StartHyperAgentTaskParams
2727
) -> StartHyperAgentTaskResponse:
28-
try:
29-
payload = params.model_dump(exclude_none=True, by_alias=True)
30-
except HyperbrowserError:
31-
raise
32-
except Exception as exc:
33-
raise HyperbrowserError(
34-
"Failed to serialize HyperAgent start params",
35-
original_error=exc,
36-
) from exc
37-
if type(payload) is not dict:
38-
raise HyperbrowserError("Failed to serialize HyperAgent start params")
28+
payload = serialize_model_dump_to_dict(
29+
params,
30+
error_message="Failed to serialize HyperAgent start params",
31+
)
3932
response = await self._client.transport.post(
4033
self._client._build_url("/task/hyper-agent"),
4134
data=payload,

hyperbrowser/client/managers/async_manager/computer_action.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Union, List, Optional
33
from hyperbrowser.exceptions import HyperbrowserError
44
from ..response_utils import parse_response_model
5+
from ..serialization_utils import serialize_model_dump_to_dict
56
from hyperbrowser.models import (
67
SessionDetail,
78
ComputerActionParams,
@@ -91,15 +92,12 @@ async def _execute_request(
9192
)
9293

9394
if isinstance(params, BaseModel):
94-
try:
95-
payload = params.model_dump(by_alias=True, exclude_none=True)
96-
except HyperbrowserError:
97-
raise
98-
except Exception as exc:
99-
raise HyperbrowserError(
100-
"Failed to serialize computer action params",
101-
original_error=exc,
102-
) from exc
95+
payload = serialize_model_dump_to_dict(
96+
params,
97+
error_message="Failed to serialize computer action params",
98+
by_alias=True,
99+
exclude_none=True,
100+
)
103101
else:
104102
payload = params
105103

hyperbrowser/client/managers/async_manager/extension.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from hyperbrowser.exceptions import HyperbrowserError
44
from ...file_utils import ensure_existing_file_path
5+
from ..serialization_utils import serialize_model_dump_to_dict
56
from ..extension_utils import parse_extension_list_response_data
67
from ..response_utils import parse_response_model
78
from hyperbrowser.models.extension import CreateExtensionParams, ExtensionResponse
@@ -23,17 +24,10 @@ async def create(self, params: CreateExtensionParams) -> ExtensionResponse:
2324
"params.file_path is invalid",
2425
original_error=exc,
2526
) from exc
26-
try:
27-
payload = params.model_dump(exclude_none=True, by_alias=True)
28-
except HyperbrowserError:
29-
raise
30-
except Exception as exc:
31-
raise HyperbrowserError(
32-
"Failed to serialize extension create params",
33-
original_error=exc,
34-
) from exc
35-
if type(payload) is not dict:
36-
raise HyperbrowserError("Failed to serialize extension create params")
27+
payload = serialize_model_dump_to_dict(
28+
params,
29+
error_message="Failed to serialize extension create params",
30+
)
3731
payload.pop("filePath", None)
3832

3933
file_path = ensure_existing_file_path(

hyperbrowser/client/managers/async_manager/extract.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
ensure_started_job_id,
1414
wait_for_job_result_async,
1515
)
16+
from ..serialization_utils import serialize_model_dump_to_dict
1617
from ...schema_utils import resolve_schema_input
1718
from ..response_utils import parse_response_model
1819

@@ -25,17 +26,10 @@ async def start(self, params: StartExtractJobParams) -> StartExtractJobResponse:
2526
if not params.schema_ and not params.prompt:
2627
raise HyperbrowserError("Either schema or prompt must be provided")
2728

28-
try:
29-
payload = params.model_dump(exclude_none=True, by_alias=True)
30-
except HyperbrowserError:
31-
raise
32-
except Exception as exc:
33-
raise HyperbrowserError(
34-
"Failed to serialize extract start params",
35-
original_error=exc,
36-
) from exc
37-
if type(payload) is not dict:
38-
raise HyperbrowserError("Failed to serialize extract start params")
29+
payload = serialize_model_dump_to_dict(
30+
params,
31+
error_message="Failed to serialize extract start params",
32+
)
3933
if params.schema_:
4034
payload["schema"] = resolve_schema_input(params.schema_)
4135

hyperbrowser/client/managers/async_manager/profile.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from typing import Optional
22

3-
from hyperbrowser.exceptions import HyperbrowserError
43
from hyperbrowser.models.profile import (
54
CreateProfileParams,
65
CreateProfileResponse,
@@ -9,6 +8,7 @@
98
ProfileResponse,
109
)
1110
from hyperbrowser.models.session import BasicResponse
11+
from ..serialization_utils import serialize_model_dump_to_dict
1212
from ..response_utils import parse_response_model
1313

1414

@@ -21,17 +21,10 @@ async def create(
2121
) -> CreateProfileResponse:
2222
payload = {}
2323
if params is not None:
24-
try:
25-
payload = params.model_dump(exclude_none=True, by_alias=True)
26-
except HyperbrowserError:
27-
raise
28-
except Exception as exc:
29-
raise HyperbrowserError(
30-
"Failed to serialize profile create params",
31-
original_error=exc,
32-
) from exc
33-
if type(payload) is not dict:
34-
raise HyperbrowserError("Failed to serialize profile create params")
24+
payload = serialize_model_dump_to_dict(
25+
params,
26+
error_message="Failed to serialize profile create params",
27+
)
3528
response = await self._client.transport.post(
3629
self._client._build_url("/profile"),
3730
data=payload,
@@ -66,17 +59,10 @@ async def list(
6659
self, params: Optional[ProfileListParams] = None
6760
) -> ProfileListResponse:
6861
params_obj = params or ProfileListParams()
69-
try:
70-
query_params = params_obj.model_dump(exclude_none=True, by_alias=True)
71-
except HyperbrowserError:
72-
raise
73-
except Exception as exc:
74-
raise HyperbrowserError(
75-
"Failed to serialize profile list params",
76-
original_error=exc,
77-
) from exc
78-
if type(query_params) is not dict:
79-
raise HyperbrowserError("Failed to serialize profile list params")
62+
query_params = serialize_model_dump_to_dict(
63+
params_obj,
64+
error_message="Failed to serialize profile list params",
65+
)
8066
response = await self._client.transport.get(
8167
self._client._build_url("/profiles"),
8268
params=query_params,

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

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from .batch_fetch import BatchFetchManager
22
from .crawl import WebCrawlManager
3-
from hyperbrowser.exceptions import HyperbrowserError
43
from hyperbrowser.models import (
54
FetchParams,
65
FetchResponse,
76
WebSearchParams,
87
WebSearchResponse,
98
)
109
from ....schema_utils import inject_web_output_schemas
10+
from ...serialization_utils import serialize_model_dump_to_dict
1111
from ...response_utils import parse_response_model
1212

1313

@@ -18,17 +18,10 @@ def __init__(self, client):
1818
self.crawl = WebCrawlManager(client)
1919

2020
async def fetch(self, params: FetchParams) -> FetchResponse:
21-
try:
22-
payload = params.model_dump(exclude_none=True, by_alias=True)
23-
except HyperbrowserError:
24-
raise
25-
except Exception as exc:
26-
raise HyperbrowserError(
27-
"Failed to serialize web fetch params",
28-
original_error=exc,
29-
) from exc
30-
if type(payload) is not dict:
31-
raise HyperbrowserError("Failed to serialize web fetch params")
21+
payload = serialize_model_dump_to_dict(
22+
params,
23+
error_message="Failed to serialize web fetch params",
24+
)
3225
inject_web_output_schemas(
3326
payload, params.outputs.formats if params.outputs else None
3427
)
@@ -44,17 +37,10 @@ async def fetch(self, params: FetchParams) -> FetchResponse:
4437
)
4538

4639
async def search(self, params: WebSearchParams) -> WebSearchResponse:
47-
try:
48-
payload = params.model_dump(exclude_none=True, by_alias=True)
49-
except HyperbrowserError:
50-
raise
51-
except Exception as exc:
52-
raise HyperbrowserError(
53-
"Failed to serialize web search params",
54-
original_error=exc,
55-
) from exc
56-
if type(payload) is not dict:
57-
raise HyperbrowserError("Failed to serialize web search params")
40+
payload = serialize_model_dump_to_dict(
41+
params,
42+
error_message="Failed to serialize web search params",
43+
)
5844
response = await self._client.transport.post(
5945
self._client._build_url("/web/search"),
6046
data=payload,

0 commit comments

Comments
 (0)