Skip to content

Commit 550b752

Browse files
feat: capture_listeners and request_listeners added
1 parent 2d4ac0f commit 550b752

File tree

10 files changed

+190
-91
lines changed

10 files changed

+190
-91
lines changed

squarecloud/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .client import Client, create_config_file
2-
from .square import File
32
from .errors import *
43
from .http.endpoints import Endpoint
4+
from .square import File

squarecloud/app.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from .data import AppData
21
from abc import ABC
3-
from typing import Literal, TYPE_CHECKING, Callable
4-
from .square import File
2+
from typing import TYPE_CHECKING, Callable
3+
4+
from .data import AppData
55
from .data import StatusData, LogsData, FullLogsData, BackupData
6-
from .http import Response, HTTPClient, Endpoint
76
from .errors import SquareException
8-
from .listener import ListenerManager
7+
from .http import Response, HTTPClient, Endpoint
8+
from .listener import ListenerManager, Listener
9+
from .square import File
910

1011
# avoid circular imports
1112
if TYPE_CHECKING:
@@ -68,7 +69,7 @@ class Application(AbstractApplication):
6869
def __init__(self, client: 'Client', http: HTTPClient, data: AppData):
6970
self._client: 'Client' = client
7071
self._http = http
71-
self._listener: ListenerManager = ListenerManager(self)
72+
self._listener: ListenerManager = Listener
7273
self._data = data
7374
self.cache: AppCache = AppCache()
7475

@@ -124,17 +125,20 @@ def avatar(self):
124125
"""application's avatar"""
125126
return self.data.avatar
126127

127-
def on_request(self, endpoint: Endpoint) -> Callable:
128+
def capture(self, endpoint: Endpoint) -> Callable:
128129
def wrapper(func):
129-
self._listener.add_listener(endpoint, func)
130+
if not self._listener.get_capture_listener(endpoint):
131+
return self._listener.add_capture_listener(endpoint, func)
132+
raise SquareException(
133+
f'Already exists an capture_listener for {endpoint}')
130134

131135
return wrapper
132136

133137
async def logs(self, update_cache: bool = True) -> LogsData:
134138
"""get application's logs"""
135139
logs: LogsData = await self.client.get_logs(self.id)
136140
endpoint: Endpoint = Endpoint.logs()
137-
await self._listener.on_request_end(endpoint=endpoint, logs=logs)
141+
await self._listener.on_capture(endpoint=endpoint, logs=logs)
138142
if update_cache:
139143
self.cache.logs = logs
140144
return logs
@@ -143,7 +147,7 @@ async def full_logs(self, update_cache: bool = True) -> FullLogsData:
143147
"""get application's full logs"""
144148
full_logs: FullLogsData = await self.client.full_logs(self.id)
145149
endpoint: Endpoint = Endpoint.full_logs()
146-
await self._listener.on_request_end(
150+
await self._listener.on_capture(
147151
endpoint=endpoint, full_logs=full_logs)
148152
if update_cache:
149153
self.cache.full_logs = full_logs
@@ -153,7 +157,7 @@ async def status(self, update_cache: bool = True) -> StatusData:
153157
"""get application's status"""
154158
status: StatusData = await self.client.app_status(self.id)
155159
endpoint: Endpoint = Endpoint.app_status()
156-
await self._listener.on_request_end(endpoint=endpoint, status=status)
160+
await self._listener.on_capture(endpoint=endpoint, status=status)
157161
if update_cache:
158162
self.cache.status = status
159163
return status
@@ -162,35 +166,53 @@ async def backup(self, update_cache: bool = True) -> BackupData:
162166
"""make backup of this application"""
163167
backup: BackupData = await self.client.backup(self.id)
164168
endpoint: Endpoint = Endpoint.backup()
165-
await self._listener.on_request_end(endpoint=endpoint, backup=backup)
169+
await self._listener.on_capture(endpoint=endpoint, backup=backup)
166170
if update_cache:
167171
self.cache.backup = backup
168172
return backup
169173

170174
async def start(self, update_cache: bool = True) -> Response:
171175
"""start the application"""
176+
response: Response = await self.client.start_app(self.id)
177+
endpoint: Endpoint = Endpoint.start()
172178
if update_cache:
173179
self.cache.status.running = True
174-
return await self.client.start_app(self.id)
180+
await self._listener.on_capture(endpoint=endpoint,
181+
response=response)
182+
return response
175183

176184
async def stop(self, update_cache: bool = True) -> Response:
177185
"""stop the application"""
178-
result = await self.client.stop_app(self.id)
186+
response: Response = await self.client.stop_app(self.id)
187+
endpoint: Endpoint = Endpoint.stop()
179188
if update_cache:
180189
self.cache.status.running = False
181-
return result
190+
await self._listener.on_capture(endpoint=endpoint,
191+
response=response)
192+
return response
182193

183194
async def restart(self, update_cache: bool = True) -> Response:
184195
"""restart the application"""
185-
result = await self.client.restart_app(self.id)
196+
response: Response = await self.client.restart_app(self.id)
197+
endpoint: Endpoint = Endpoint.restart()
198+
await self._listener.on_capture(endpoint=endpoint,
199+
response=response)
186200
if update_cache:
187201
self.cache.status.status = 'restarting'
188-
return result
202+
return response
189203

190204
async def delete(self) -> Response:
191205
"""delete the application"""
192-
return await self.client.delete_app(self.id)
206+
response: Response = await self.client.delete_app(self.id)
207+
endpoint: Endpoint = Endpoint.delete()
208+
await self._listener.on_capture(endpoint=endpoint,
209+
response=response)
210+
return response
193211

194212
async def commit(self, file: File) -> Response:
195213
"""commit the application"""
196-
return await self.client.commit(self.id, file=file)
214+
response: Response = await self.client.commit(self.id, file=file)
215+
endpoint: Endpoint = Endpoint.commit()
216+
await self._listener.on_capture(endpoint=endpoint,
217+
response=response)
218+
return response

squarecloud/client.py

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
FullLogsData,
1616
UploadData
1717
)
18-
from .errors import ApplicationNotFound, InvalidFile
18+
from .errors import ApplicationNotFound, InvalidFile, SquareException
1919
from .http import HTTPClient, Response
2020
from .http.endpoints import Endpoint
21+
from .listener import ListenerManager, Listener
2122
from .logs import logger
22-
from .square import File
23-
from .types import (
23+
from .payloads import (
2424
UserPayload,
2525
StatusPayload,
2626
LogsPayload,
2727
BackupPayload,
2828
FullLogsPayload,
2929
UploadPayload,
3030
)
31+
from .square import File
3132

3233

3334
class AbstractClient(ABC):
@@ -102,7 +103,7 @@ def __init__(self, api_key: str, debug: bool = True) -> None:
102103
self.debug = debug
103104
self._api_key = api_key
104105
self._http = HTTPClient(api_key=api_key)
105-
self._listeners: dict[list[Endpoint], Callable]
106+
self._listener: ListenerManager = Listener
106107
if self.debug:
107108
logger.setLevel(logging.DEBUG)
108109

@@ -116,16 +117,35 @@ def api_key(self) -> str:
116117
"""
117118
return self._api_key
118119

119-
async def user_info(self) -> UserData:
120+
def on_request(self, endpoint: Endpoint):
121+
def wrapper(func: Callable):
122+
if not self._listener.get_request_listener(endpoint):
123+
return self._listener.add_request_listener(endpoint, func)
124+
raise SquareException(
125+
f'Already exists an capture_listener for {endpoint}')
126+
127+
return wrapper
128+
129+
async def me(self) -> UserData:
130+
response: Response = await self._http.fetch_user_info()
131+
payload: UserPayload = response.response
132+
user_data: UserData = UserData(**payload['user'])
133+
endpoint: Endpoint = response.route.endpoint
134+
await self._listener.on_request(endpoint=endpoint, response=response)
135+
return user_data
136+
137+
async def user_info(self, user_id: int | None = None) -> UserData:
120138
"""
121139
Get user information
122140
123141
Returns:
124142
UserData
125143
"""
126-
result: Response = await self._http.fetch_user_info()
127-
payload: UserPayload = result.response
144+
response: Response = await self._http.fetch_user_info(user_id=user_id)
145+
payload: UserPayload = response.response
128146
user_data: UserData = UserData(**payload['user'])
147+
endpoint: Endpoint = response.route.endpoint
148+
await self._listener.on_request(endpoint=endpoint, response=response)
129149
return user_data
130150

131151
async def get_logs(self, app_id: str) -> LogsData:
@@ -138,9 +158,11 @@ async def get_logs(self, app_id: str) -> LogsData:
138158
Returns:
139159
LogData
140160
"""
141-
result: Response = await self._http.fetch_logs(app_id)
142-
payload: LogsPayload = result.response
161+
response: Response = await self._http.fetch_logs(app_id)
162+
payload: LogsPayload = response.response
143163
logs_data: LogsData = LogsData(**payload)
164+
endpoint: Endpoint = response.route.endpoint
165+
await self._listener.on_request(endpoint=endpoint, response=response)
144166
return logs_data
145167

146168
async def full_logs(self, app_id: str) -> FullLogsData:
@@ -153,9 +175,11 @@ async def full_logs(self, app_id: str) -> FullLogsData:
153175
Returns:
154176
FullLogsData
155177
"""
156-
result: Response = await self._http.fetch_logs_complete(app_id)
157-
payload: FullLogsPayload = result.response
178+
response: Response = await self._http.fetch_logs_complete(app_id)
179+
payload: FullLogsPayload = response.response
158180
logs_data: FullLogsData = FullLogsData(**payload)
181+
endpoint: Endpoint = response.route.endpoint
182+
await self._listener.on_request(endpoint=endpoint, response=response)
159183
return logs_data
160184

161185
async def app_status(self, app_id: str) -> StatusData:
@@ -168,9 +192,11 @@ async def app_status(self, app_id: str) -> StatusData:
168192
Returns:
169193
StatusData
170194
"""
171-
result: Response = await self._http.fetch_app_status(app_id)
172-
payload: StatusPayload = result.response
195+
response: Response = await self._http.fetch_app_status(app_id)
196+
payload: StatusPayload = response.response
173197
status: StatusData = StatusData(**payload)
198+
endpoint: Endpoint = response.route.endpoint
199+
await self._listener.on_request(endpoint=endpoint, response=response)
174200
return status
175201

176202
async def start_app(self, app_id: str) -> Response:
@@ -180,7 +206,11 @@ async def start_app(self, app_id: str) -> Response:
180206
Args:
181207
app_id: the application ID
182208
"""
183-
return await self._http.start_application(app_id)
209+
210+
response: Response = await self._http.start_application(app_id)
211+
endpoint: Endpoint = response.route.endpoint
212+
await self._listener.on_request(endpoint=endpoint, response=response)
213+
return response
184214

185215
async def stop_app(self, app_id: str) -> Response:
186216
"""
@@ -189,7 +219,10 @@ async def stop_app(self, app_id: str) -> Response:
189219
Args:
190220
app_id: the application ID
191221
"""
192-
return await self._http.stop_application(app_id)
222+
response: Response = await self._http.stop_application(app_id)
223+
endpoint: Endpoint = response.route.endpoint
224+
await self._listener.on_request(endpoint=endpoint, response=response)
225+
return response
193226

194227
async def restart_app(self, app_id: str) -> Response:
195228
"""
@@ -198,7 +231,10 @@ async def restart_app(self, app_id: str) -> Response:
198231
Args:
199232
app_id: the application ID
200233
"""
201-
return await self._http.restart_application(app_id)
234+
response: Response = await self._http.restart_application(app_id)
235+
endpoint: Endpoint = response.route.endpoint
236+
await self._listener.on_request(endpoint=endpoint, response=response)
237+
return response
202238

203239
async def backup(self, app_id: str) -> BackupData:
204240
"""
@@ -209,9 +245,11 @@ async def backup(self, app_id: str) -> BackupData:
209245
Returns:
210246
Backup
211247
"""
212-
result: Response = await self._http.backup(app_id)
213-
payload: BackupPayload = result.response
248+
response: Response = await self._http.backup(app_id)
249+
payload: BackupPayload = response.response
214250
backup: BackupData = BackupData(**payload)
251+
endpoint: Endpoint = response.route.endpoint
252+
await self._listener.on_request(endpoint=endpoint, response=response)
215253
return backup
216254

217255
async def delete_app(self, app_id: str) -> Response:
@@ -221,7 +259,10 @@ async def delete_app(self, app_id: str) -> Response:
221259
Args:
222260
app_id: the application ID
223261
"""
224-
return await self._http.delete_application(app_id)
262+
response: Response = await self._http.delete_application(app_id)
263+
endpoint: Endpoint = response.route.endpoint
264+
await self._listener.on_request(endpoint=endpoint, response=response)
265+
return response
225266

226267
async def commit(self, app_id: str, file: File) -> Response:
227268
"""
@@ -231,7 +272,10 @@ async def commit(self, app_id: str, file: File) -> Response:
231272
app_id: the application ID
232273
file: the file object to be committed
233274
"""
234-
return await self._http.commit(app_id, file)
275+
response: Response = await self._http.commit(app_id, file)
276+
endpoint: Endpoint = response.route.endpoint
277+
await self._listener.on_request(endpoint=endpoint, response=response)
278+
return response
235279

236280
async def app(self, app_id: str) -> Application:
237281
"""
@@ -240,8 +284,8 @@ async def app(self, app_id: str) -> Application:
240284
Args:
241285
app_id: the application ID
242286
"""
243-
result: Response = await self._http.fetch_user_info()
244-
payload: UserPayload = result.response
287+
response: Response = await self._http.fetch_user_info()
288+
payload: UserPayload = response.response
245289
app_data = list(filter(lambda application: application['id'] == app_id,
246290
payload['applications']))
247291
if not app_data:
@@ -259,8 +303,8 @@ async def all_apps(self) -> List[Application]:
259303
Returns:
260304
List[AppData]
261305
"""
262-
result: Response = await self._http.fetch_user_info()
263-
payload: UserPayload = result.response
306+
response: Response = await self._http.fetch_user_info()
307+
payload: UserPayload = response.response
264308
apps_data: List[AppData] = [
265309
AppData(**app_data) for app_data in # type: ignore
266310
payload['applications']]
@@ -275,7 +319,9 @@ async def upload_app(self, file: File) -> UploadData:
275319
f'you need provide an {File.__name__} object')
276320
if file.name.split('.')[-1] != 'zip':
277321
raise InvalidFile('the file must be a .zip file')
278-
result: Response = await self._http.upload(file)
279-
payload: UploadPayload = result.app
322+
response: Response = await self._http.upload(file)
323+
payload: UploadPayload = response.app
280324
app: UploadData = UploadData(**payload)
325+
endpoint: Endpoint = response.route.endpoint
326+
await self._listener.on_request(endpoint=endpoint, response=response)
281327
return app

squarecloud/data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class UserData:
4848
"""user data class"""
4949
id: int
5050
tag: str
51-
email: str | Literal['Access denied']
51+
locale: str
52+
email: str | None
5253
plan: PlanData
5354
blocklist: bool
5455

squarecloud/http/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
from .http_client import HTTPClient, Response
21
from .endpoints import Endpoint
2+
from .http_client import HTTPClient, Response

0 commit comments

Comments
 (0)