Skip to content

Commit bd06490

Browse files
Harden SDK defaults, typing, and resource handling
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 979e255 commit bd06490

24 files changed

Lines changed: 91 additions & 71 deletions

File tree

hyperbrowser/client/managers/async_manager/crawl.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
from typing import Optional
23

34
from hyperbrowser.models.consts import POLLING_ATTEMPTS
45
from ....models.crawl import (
@@ -30,11 +31,12 @@ async def get_status(self, job_id: str) -> CrawlJobStatusResponse:
3031
return CrawlJobStatusResponse(**response.data)
3132

3233
async def get(
33-
self, job_id: str, params: GetCrawlJobParams = GetCrawlJobParams()
34+
self, job_id: str, params: Optional[GetCrawlJobParams] = None
3435
) -> CrawlJobResponse:
36+
params_obj = params or GetCrawlJobParams()
3537
response = await self._client.transport.get(
3638
self._client._build_url(f"/crawl/{job_id}"),
37-
params=params.model_dump(exclude_none=True, by_alias=True),
39+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
3840
)
3941
return CrawlJobResponse(**response.data)
4042

hyperbrowser/client/managers/async_manager/extension.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ def __init__(self, client):
1111

1212
async def create(self, params: CreateExtensionParams) -> ExtensionResponse:
1313
file_path = params.file_path
14-
params.file_path = None
14+
payload = params.model_dump(exclude_none=True, by_alias=True)
15+
payload.pop("filePath", None)
1516

1617
# Check if file exists before trying to open it
1718
if not os.path.exists(file_path):
1819
raise FileNotFoundError(f"Extension file not found at path: {file_path}")
1920

20-
response = await self._client.transport.post(
21-
self._client._build_url("/extensions/add"),
22-
data=(
23-
{}
24-
if params is None
25-
else params.model_dump(exclude_none=True, by_alias=True)
26-
),
27-
files={"file": open(file_path, "rb")},
28-
)
21+
with open(file_path, "rb") as extension_file:
22+
response = await self._client.transport.post(
23+
self._client._build_url("/extensions/add"),
24+
data=payload,
25+
files={"file": extension_file},
26+
)
2927
return ExtensionResponse(**response.data)
3028

3129
async def list(self) -> List[ExtensionResponse]:

hyperbrowser/client/managers/async_manager/profile.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from hyperbrowser.models.profile import (
24
CreateProfileParams,
35
CreateProfileResponse,
@@ -36,10 +38,11 @@ async def delete(self, id: str) -> BasicResponse:
3638
return BasicResponse(**response.data)
3739

3840
async def list(
39-
self, params: ProfileListParams = ProfileListParams()
41+
self, params: Optional[ProfileListParams] = None
4042
) -> ProfileListResponse:
43+
params_obj = params or ProfileListParams()
4144
response = await self._client.transport.get(
4245
self._client._build_url("/profiles"),
43-
params=params.model_dump(exclude_none=True, by_alias=True),
46+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
4447
)
4548
return ProfileListResponse(**response.data)

hyperbrowser/client/managers/async_manager/scrape.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ async def get_status(self, job_id: str) -> BatchScrapeJobStatusResponse:
3737
return BatchScrapeJobStatusResponse(**response.data)
3838

3939
async def get(
40-
self, job_id: str, params: GetBatchScrapeJobParams = GetBatchScrapeJobParams()
40+
self, job_id: str, params: Optional[GetBatchScrapeJobParams] = None
4141
) -> BatchScrapeJobResponse:
42+
params_obj = params or GetBatchScrapeJobParams()
4243
response = await self._client.transport.get(
4344
self._client._build_url(f"/scrape/batch/{job_id}"),
44-
params=params.model_dump(exclude_none=True, by_alias=True),
45+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
4546
)
4647
return BatchScrapeJobResponse(**response.data)
4748

hyperbrowser/client/managers/async_manager/session.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
UploadFileResponse,
1414
SessionEventLogListParams,
1515
SessionEventLogListResponse,
16-
SessionEventLog,
1716
UpdateSessionProfileParams,
1817
SessionGetParams,
1918
)
@@ -26,11 +25,12 @@ def __init__(self, client):
2625
async def list(
2726
self,
2827
session_id: str,
29-
params: SessionEventLogListParams = SessionEventLogListParams(),
30-
) -> List[SessionEventLog]:
28+
params: Optional[SessionEventLogListParams] = None,
29+
) -> SessionEventLogListResponse:
30+
params_obj = params or SessionEventLogListParams()
3131
response = await self._client.transport.get(
3232
self._client._build_url(f"/session/{session_id}/event-logs"),
33-
params=params.model_dump(exclude_none=True, by_alias=True),
33+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
3434
)
3535
return SessionEventLogListResponse(**response.data)
3636

@@ -54,11 +54,12 @@ async def create(self, params: CreateSessionParams = None) -> SessionDetail:
5454
return SessionDetail(**response.data)
5555

5656
async def get(
57-
self, id: str, params: SessionGetParams = SessionGetParams()
57+
self, id: str, params: Optional[SessionGetParams] = None
5858
) -> SessionDetail:
59+
params_obj = params or SessionGetParams()
5960
response = await self._client.transport.get(
6061
self._client._build_url(f"/session/{id}"),
61-
params=params.model_dump(exclude_none=True, by_alias=True),
62+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
6263
)
6364
return SessionDetail(**response.data)
6465

@@ -69,11 +70,12 @@ async def stop(self, id: str) -> BasicResponse:
6970
return BasicResponse(**response.data)
7071

7172
async def list(
72-
self, params: SessionListParams = SessionListParams()
73+
self, params: Optional[SessionListParams] = None
7374
) -> SessionListResponse:
75+
params_obj = params or SessionListParams()
7476
response = await self._client.transport.get(
7577
self._client._build_url("/sessions"),
76-
params=params.model_dump(exclude_none=True, by_alias=True),
78+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
7779
)
7880
return SessionListResponse(**response.data)
7981

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from hyperbrowser.models import (
24
StartBatchFetchJobParams,
35
StartBatchFetchJobResponse,
@@ -43,11 +45,12 @@ async def get_status(self, job_id: str) -> BatchFetchJobStatusResponse:
4345
return BatchFetchJobStatusResponse(**response.data)
4446

4547
async def get(
46-
self, job_id: str, params: GetBatchFetchJobParams = GetBatchFetchJobParams()
48+
self, job_id: str, params: Optional[GetBatchFetchJobParams] = None
4749
) -> BatchFetchJobResponse:
50+
params_obj = params or GetBatchFetchJobParams()
4851
response = await self._client.transport.get(
4952
self._client._build_url(f"/web/batch-fetch/{job_id}"),
50-
params=params.model_dump(exclude_none=True, by_alias=True),
53+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
5154
)
5255
return BatchFetchJobResponse(**response.data)
5356

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from hyperbrowser.models import (
24
StartWebCrawlJobParams,
35
StartWebCrawlJobResponse,
@@ -41,11 +43,12 @@ async def get_status(self, job_id: str) -> WebCrawlJobStatusResponse:
4143
return WebCrawlJobStatusResponse(**response.data)
4244

4345
async def get(
44-
self, job_id: str, params: GetWebCrawlJobParams = GetWebCrawlJobParams()
46+
self, job_id: str, params: Optional[GetWebCrawlJobParams] = None
4547
) -> WebCrawlJobResponse:
48+
params_obj = params or GetWebCrawlJobParams()
4649
response = await self._client.transport.get(
4750
self._client._build_url(f"/web/crawl/{job_id}"),
48-
params=params.model_dump(exclude_none=True, by_alias=True),
51+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
4952
)
5053
return WebCrawlJobResponse(**response.data)
5154

hyperbrowser/client/managers/sync_manager/crawl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ def get_status(self, job_id: str) -> CrawlJobStatusResponse:
3131
return CrawlJobStatusResponse(**response.data)
3232

3333
def get(
34-
self, job_id: str, params: GetCrawlJobParams = GetCrawlJobParams()
34+
self, job_id: str, params: Optional[GetCrawlJobParams] = None
3535
) -> CrawlJobResponse:
36+
params_obj = params or GetCrawlJobParams()
3637
response = self._client.transport.get(
3738
self._client._build_url(f"/crawl/{job_id}"),
38-
params=params.model_dump(exclude_none=True, by_alias=True),
39+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
3940
)
4041
return CrawlJobResponse(**response.data)
4142

hyperbrowser/client/managers/sync_manager/extension.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,19 @@ def __init__(self, client):
1111

1212
def create(self, params: CreateExtensionParams) -> ExtensionResponse:
1313
file_path = params.file_path
14-
params.file_path = None
14+
payload = params.model_dump(exclude_none=True, by_alias=True)
15+
payload.pop("filePath", None)
1516

1617
# Check if file exists before trying to open it
1718
if not os.path.exists(file_path):
1819
raise FileNotFoundError(f"Extension file not found at path: {file_path}")
1920

20-
response = self._client.transport.post(
21-
self._client._build_url("/extensions/add"),
22-
data=(
23-
{}
24-
if params is None
25-
else params.model_dump(exclude_none=True, by_alias=True)
26-
),
27-
files={"file": open(file_path, "rb")},
28-
)
21+
with open(file_path, "rb") as extension_file:
22+
response = self._client.transport.post(
23+
self._client._build_url("/extensions/add"),
24+
data=payload,
25+
files={"file": extension_file},
26+
)
2927
return ExtensionResponse(**response.data)
3028

3129
def list(self) -> List[ExtensionResponse]:

hyperbrowser/client/managers/sync_manager/profile.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Optional
2+
13
from hyperbrowser.models.profile import (
24
CreateProfileParams,
35
CreateProfileResponse,
@@ -35,11 +37,10 @@ def delete(self, id: str) -> BasicResponse:
3537
)
3638
return BasicResponse(**response.data)
3739

38-
def list(
39-
self, params: ProfileListParams = ProfileListParams()
40-
) -> ProfileListResponse:
40+
def list(self, params: Optional[ProfileListParams] = None) -> ProfileListResponse:
41+
params_obj = params or ProfileListParams()
4142
response = self._client.transport.get(
4243
self._client._build_url("/profiles"),
43-
params=params.model_dump(exclude_none=True, by_alias=True),
44+
params=params_obj.model_dump(exclude_none=True, by_alias=True),
4445
)
4546
return ProfileListResponse(**response.data)

0 commit comments

Comments
 (0)