Skip to content

Commit f617a45

Browse files
♻️ refactor: Split ListenerManager into CaptureListenerManager and RequestListenerManager
1 parent 9a4123d commit f617a45

File tree

4 files changed

+50
-46
lines changed

4 files changed

+50
-46
lines changed

squarecloud/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .errors import SquareException
99
from .file import File
1010
from .http import Endpoint, HTTPClient, Response
11-
from .listener import Listener, ListenerManager
11+
from .listeners import CaptureListenerManager
1212

1313
# avoid circular imports
1414
if TYPE_CHECKING:
@@ -199,7 +199,7 @@ def __init__(
199199
self._isWebsite: bool = isWebsite
200200
self._client: 'Client' = client
201201
self._http: HTTPClient = http
202-
self._listener: ListenerManager = Listener
202+
self._listener: CaptureListenerManager = CaptureListenerManager()
203203
self.cache: AppCache = AppCache()
204204

205205
def __repr__(self) -> str:

squarecloud/client.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import logging
55
from abc import ABC, abstractmethod
66
from io import BytesIO
7-
from typing import Any, Callable, List, Literal
7+
from typing import Any, Callable, List, Literal, TextIO
88

99
from .app import Application
1010
from .data import (
@@ -21,15 +21,8 @@
2121
from .file import File
2222
from .http import HTTPClient, Response
2323
from .http.endpoints import Endpoint
24-
from .listener import Listener, ListenerManager
24+
from .listeners import RequestListenerManager
2525
from .logs import logger
26-
from .payloads import (
27-
BackupPayload,
28-
LogsPayload,
29-
StatusPayload,
30-
UploadPayload,
31-
UserPayload,
32-
)
3326

3427

3528
class AbstractClient(ABC):
@@ -52,7 +45,7 @@ def create_config_file(
5245
start: str | None = None,
5346
auto_restart: bool = False,
5447
**kwargs,
55-
) -> str:
48+
) -> TextIO | str:
5649
"""
5750
The create_config_file function creates a squarecloud.app file in the
5851
specified path, with the given parameters.
@@ -124,7 +117,7 @@ def __init__(self, api_key: str, debug: bool = True) -> None:
124117
self.debug = debug
125118
self._api_key = api_key
126119
self._http = HTTPClient(api_key=api_key)
127-
self._listener: ListenerManager = Listener
120+
self._listener: RequestListenerManager = RequestListenerManager()
128121
if self.debug:
129122
logger.setLevel(logging.DEBUG)
130123

@@ -180,7 +173,7 @@ async def me(self, **kwargs) -> UserData:
180173
:return: A userdata object
181174
"""
182175
response: Response = await self._http.fetch_user_info()
183-
payload: UserPayload = response.response
176+
payload: dict[str, Any] = response.response
184177
user_data: UserData = UserData(**payload['user'])
185178
if not kwargs.get('avoid_listener'):
186179
endpoint: Endpoint = response.route.endpoint
@@ -202,7 +195,7 @@ async def user_info(
202195
:return: A UserData object
203196
"""
204197
response: Response = await self._http.fetch_user_info(user_id=user_id)
205-
payload: UserPayload = response.response
198+
payload: dict[str, Any] = response.response
206199
user_data: UserData = UserData(**payload['user'])
207200
if not kwargs.get('avoid_listener'):
208201
endpoint: Endpoint = response.route.endpoint
@@ -224,7 +217,7 @@ async def get_logs(self, app_id: str, **kwargs) -> LogsData:
224217
response: Response | None = await self._http.fetch_logs(app_id)
225218
if response.code is None:
226219
return LogsData()
227-
payload: LogsPayload = response.response
220+
payload: dict[str, Any] = response.response
228221
logs_data: LogsData = LogsData(**payload)
229222
if not kwargs.get('avoid_listener'):
230223
endpoint: Endpoint = response.route.endpoint
@@ -243,7 +236,7 @@ async def app_status(self, app_id: str, **kwargs) -> StatusData:
243236
:return: A StatusData object
244237
"""
245238
response: Response = await self._http.fetch_app_status(app_id)
246-
payload: StatusPayload = response.response
239+
payload: dict[str, Any] = response.response
247240
status: StatusData = StatusData(**payload)
248241
if not kwargs.get('avoid_listener'):
249242
endpoint: Endpoint = response.route.endpoint
@@ -315,7 +308,7 @@ async def backup(self, app_id: str, **kwargs) -> BackupData:
315308
:return: A BackupData object
316309
"""
317310
response: Response = await self._http.backup(app_id)
318-
payload: BackupPayload = response.response
311+
payload: dict[str, Any] = response.response
319312
backup: BackupData = BackupData(**payload)
320313
if not kwargs.get('avoid_listener'):
321314
endpoint: Endpoint = response.route.endpoint
@@ -445,7 +438,7 @@ async def upload_app(self, file: File, **kwargs) -> UploadData:
445438
await self._listener.on_request(
446439
endpoint=endpoint, response=response
447440
)
448-
payload: UploadPayload = response.response
441+
payload: dict[str, Any] = response.response
449442
app: UploadData = UploadData(**payload)
450443
endpoint: Endpoint = response.route.endpoint
451444
await self._listener.on_request(endpoint=endpoint, response=response)

squarecloud/http/http_client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
TooManyRequests,
2727
)
2828
from ..logs import logger
29-
from ..payloads import RawResponseData
3029
from .endpoints import Endpoint, Router
3130

3231

3332
class Response:
3433
"""Represents a request response"""
3534

36-
def __init__(self, data: RawResponseData, route: Router) -> None:
35+
def __init__(self, data: dict[str, Any], route: Router) -> None:
3736
"""
3837
The __init__ function is called when the class is instantiated.
3938
It sets up the instance of the class, and defines all of its
@@ -127,7 +126,7 @@ async def request(self, route: Router, **kwargs) -> Response | bytes:
127126
url=route.url, method=route.method, **kwargs
128127
) as resp:
129128
status_code = resp.status
130-
data: RawResponseData = await resp.json()
129+
data: dict[str, Any] = await resp.json()
131130
extra = {
132131
'status': data.get('status'),
133132
'route': route.url,
Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from .http.endpoints import Endpoint
66

77

8-
class ListenerManager:
9-
"""ListenerManager"""
8+
class CaptureListenerManager:
9+
"""CaptureListenerManager"""
1010

1111
def __init__(self):
1212
"""
@@ -19,7 +19,6 @@ def __init__(self):
1919
:return: A dictionary of the capture listeners and request listeners
2020
"""
2121
self.capture_listeners: dict[str, Callable] = {}
22-
self.request_listeners: dict[str, Callable] = {}
2322

2423
def get_capture_listener(self, endpoint: Endpoint) -> Callable:
2524
"""
@@ -72,6 +71,39 @@ def clear_capture_listeners(self) -> None:
7271
"""
7372
self.capture_listeners = None
7473

74+
async def on_capture(self, endpoint: Endpoint, **kwargs) -> Any:
75+
"""
76+
The on_capture function is called when a capture event occurs.
77+
78+
:param self: Refer to the class instance
79+
:param endpoint: Endpoint: Get the endpoint that is being called
80+
:param kwargs: Pass a dictionary of arguments to the function
81+
:return: The result of the call function
82+
"""
83+
call: Callable = self.get_capture_listener(endpoint)
84+
if not call:
85+
return
86+
is_coro: bool = asyncio.iscoroutinefunction(call)
87+
if is_coro:
88+
return await call(**kwargs)
89+
return call(**kwargs)
90+
91+
92+
class RequestListenerManager:
93+
"""CaptureListenerManager"""
94+
95+
def __init__(self):
96+
"""
97+
The __init__ function is called when the class is instantiated.
98+
It sets up the instance variables that will be used by other methods
99+
in the class.
100+
101+
102+
:param self: Refer to the class instance
103+
:return: A dictionary of the capture listeners and request listeners
104+
"""
105+
self.request_listeners: dict[str, Callable] = {}
106+
75107
def get_request_listener(self, endpoint: Endpoint) -> Callable:
76108
"""
77109
The get_request_listener function is a helper function that returns
@@ -120,24 +152,7 @@ def clear_request_listeners(self) -> None:
120152
:param self: Refer to the class instance
121153
:return: None
122154
"""
123-
self.capture_listeners = None
124-
125-
async def on_capture(self, endpoint: Endpoint, **kwargs) -> Any:
126-
"""
127-
The on_capture function is called when a capture event occurs.
128-
129-
:param self: Refer to the class instance
130-
:param endpoint: Endpoint: Get the endpoint that is being called
131-
:param kwargs: Pass a dictionary of arguments to the function
132-
:return: The result of the call function
133-
"""
134-
call: Callable = self.get_capture_listener(endpoint)
135-
if not call:
136-
return
137-
is_coro: bool = asyncio.iscoroutinefunction(call)
138-
if is_coro:
139-
return await call(**kwargs)
140-
return call(**kwargs)
155+
# self.capture_listeners = None
141156

142157
async def on_request(self, endpoint: Endpoint, response: Response) -> Any:
143158
"""
@@ -156,6 +171,3 @@ async def on_request(self, endpoint: Endpoint, response: Response) -> Any:
156171
if asyncio.iscoroutinefunction(call):
157172
return await call(response=response)
158173
return call(response=response)
159-
160-
161-
Listener = ListenerManager()

0 commit comments

Comments
 (0)