Skip to content

Commit 4a8ca64

Browse files
Clarify sync and async transport interface contracts
Co-authored-by: Shri Sukhani <shrisukhani@users.noreply.github.com>
1 parent 925cbaa commit 4a8ca64

4 files changed

Lines changed: 59 additions & 19 deletions

File tree

hyperbrowser/client/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typing import Optional
1+
from typing import Optional, Type, Union
22

33
from hyperbrowser.exceptions import HyperbrowserError
44
from ..config import ClientConfig
5-
from ..transport.base import TransportStrategy
5+
from ..transport.base import AsyncTransportStrategy, SyncTransportStrategy
66
import os
77

88

@@ -11,7 +11,7 @@ class HyperbrowserBase:
1111

1212
def __init__(
1313
self,
14-
transport: TransportStrategy,
14+
transport: Type[Union[SyncTransportStrategy, AsyncTransportStrategy]],
1515
config: Optional[ClientConfig] = None,
1616
api_key: Optional[str] = None,
1717
base_url: Optional[str] = None,

hyperbrowser/transport/async_transport.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
from typing import Optional
44

55
from hyperbrowser.exceptions import HyperbrowserError
6-
from .base import TransportStrategy, APIResponse
6+
from .base import APIResponse, AsyncTransportStrategy
77

88

9-
class AsyncTransport(TransportStrategy):
9+
class AsyncTransport(AsyncTransportStrategy):
1010
"""Asynchronous transport implementation using httpx"""
1111

1212
def __init__(self, api_key: str):

hyperbrowser/transport/base.py

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC, abstractmethod
2-
from typing import Optional, TypeVar, Generic, Type, Union
2+
from typing import Generic, Optional, Type, TypeVar, Union
33

44
from hyperbrowser.exceptions import HyperbrowserError
55

@@ -33,29 +33,69 @@ def is_success(self) -> bool:
3333
return 200 <= self.status_code < 300
3434

3535

36-
class TransportStrategy(ABC):
37-
"""Abstract base class for different transport implementations"""
36+
class SyncTransportStrategy(ABC):
37+
"""Abstract base class for synchronous transport implementations"""
3838

3939
@abstractmethod
4040
def __init__(self, api_key: str):
41-
pass
41+
...
4242

4343
@abstractmethod
4444
def close(self) -> None:
45-
pass
45+
...
4646

4747
@abstractmethod
48-
def post(self, url: str) -> APIResponse:
49-
pass
48+
def post(
49+
self, url: str, data: Optional[dict] = None, files: Optional[dict] = None
50+
) -> APIResponse:
51+
...
5052

5153
@abstractmethod
52-
def get(self, url: str, params: Optional[dict] = None) -> APIResponse:
53-
pass
54+
def get(
55+
self, url: str, params: Optional[dict] = None, follow_redirects: bool = False
56+
) -> APIResponse:
57+
...
5458

5559
@abstractmethod
56-
def put(self, url: str) -> APIResponse:
57-
pass
60+
def put(self, url: str, data: Optional[dict] = None) -> APIResponse:
61+
...
5862

5963
@abstractmethod
6064
def delete(self, url: str) -> APIResponse:
61-
pass
65+
...
66+
67+
68+
class AsyncTransportStrategy(ABC):
69+
"""Abstract base class for asynchronous transport implementations"""
70+
71+
@abstractmethod
72+
def __init__(self, api_key: str):
73+
...
74+
75+
@abstractmethod
76+
async def close(self) -> None:
77+
...
78+
79+
@abstractmethod
80+
async def post(
81+
self, url: str, data: Optional[dict] = None, files: Optional[dict] = None
82+
) -> APIResponse:
83+
...
84+
85+
@abstractmethod
86+
async def get(
87+
self, url: str, params: Optional[dict] = None, follow_redirects: bool = False
88+
) -> APIResponse:
89+
...
90+
91+
@abstractmethod
92+
async def put(self, url: str, data: Optional[dict] = None) -> APIResponse:
93+
...
94+
95+
@abstractmethod
96+
async def delete(self, url: str) -> APIResponse:
97+
...
98+
99+
100+
class TransportStrategy(SyncTransportStrategy):
101+
"""Backward-compatible alias for the sync transport interface."""

hyperbrowser/transport/sync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
from typing import Optional
33

44
from hyperbrowser.exceptions import HyperbrowserError
5-
from .base import TransportStrategy, APIResponse
5+
from .base import APIResponse, SyncTransportStrategy
66

77

8-
class SyncTransport(TransportStrategy):
8+
class SyncTransport(SyncTransportStrategy):
99
"""Synchronous transport implementation using httpx"""
1010

1111
def __init__(self, api_key: str):

0 commit comments

Comments
 (0)