1515 FullLogsData ,
1616 UploadData
1717)
18- from .errors import ApplicationNotFound , InvalidFile
18+ from .errors import ApplicationNotFound , InvalidFile , SquareException
1919from .http import HTTPClient , Response
2020from .http .endpoints import Endpoint
21+ from .listener import ListenerManager , Listener
2122from .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
3334class 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
0 commit comments