11from .data import AppData
22from abc import ABC
3- from typing import Literal , TYPE_CHECKING
3+ from typing import Literal , TYPE_CHECKING , Callable
44from .square import File
55from .data import StatusData , LogsData , FullLogsData , BackupData
6- from .http import Response
6+ from .http import Response , HTTPClient , Endpoint
77from .errors import SquareException
8+ from .listener import ListenerManager
89
910# avoid circular imports
1011if TYPE_CHECKING :
@@ -57,103 +58,111 @@ class Application(AbstractApplication):
5758 # nine arguments is available in this case
5859 # pylint: disable=invalid-name
5960 __slots__ = [
60- '__client' ,
61- '__http' ,
62- '__id' ,
63- '__tag' ,
64- '__ram' ,
65- '__lang' ,
66- '__type' ,
67- '__cluster' ,
68- '__is_website' ,
69- '__avatar' ,
61+ '_client' ,
62+ '_http' ,
63+ '_listener' ,
64+ '_data' ,
7065 'cache'
7166 ]
7267
73- def __init__ (self , client : 'Client' , data : AppData ):
74- self .__client : Client = client
75- self .__id : str = data .id
76- self .__tag : str = data .tag
77- self .__ram : int = data .ram
78- self .__lang : Literal ['javascript' , 'typescript' , 'python' ] = data .lang
79- self .__type : Literal ['free' , 'paid' ] = data .type
80- self .__cluster : str = data .cluster
81- self .__is_website : bool = data .isWebsite
82- self .__avatar : str = data .avatar
68+ def __init__ (self , client : 'Client' , http : HTTPClient , data : AppData ):
69+ self ._client : 'Client' = client
70+ self ._http = http
71+ self ._listener : ListenerManager = ListenerManager (self )
72+ self ._data = data
8373 self .cache : AppCache = AppCache ()
8474
8575 def __repr__ (self ):
8676 return f'<{ self .__class__ .__name__ } tag={ self .tag } id={ self .id } >'
8777
78+ @property
79+ def data (self ):
80+ return self ._data
81+
8882 @property
8983 def client (self ):
9084 """client instance"""
91- return self .__client
85+ return self ._client
9286
9387 @property
9488 def id (self ):
9589 """application's id"""
96- return self .__id
90+ return self .data . id
9791
9892 @property
9993 def tag (self ):
10094 """application's tag"""
101- return self .__tag
95+ return self .data . tag
10296
10397 @property
10498 def ram (self ):
10599 """application's allocated ram"""
106- return self .__ram
100+ return self .data . ram
107101
108102 @property
109103 def lang (self ):
110104 """application's programing language"""
111- return self .__lang
105+ return self .data . lang
112106
113107 @property
114108 def type (self ):
115109 """application's type"""
116- return self .__type
110+ return self .data . type
117111
118112 @property
119113 def cluster (self ):
120114 """application's cluster"""
121- return self .__cluster
115+ return self .data . cluster
122116
123117 @property
124118 def is_website (self ):
125119 """whether the application is a website"""
126- return self .__is_website
120+ return self .data . isWebsite
127121
128122 @property
129123 def avatar (self ):
130124 """application's avatar"""
131- return self .__avatar
125+ return self .data .avatar
126+
127+ def on_request (self , endpoint : Endpoint ) -> Callable :
128+ def wrapper (func ):
129+ self ._listener .add_listener (endpoint , func )
130+
131+ return wrapper
132132
133133 async def logs (self , update_cache : bool = True ) -> LogsData :
134134 """get application's logs"""
135- logs : LogsData = await self .__client .get_logs (self .id )
135+ logs : LogsData = await self .client .get_logs (self .id )
136+ endpoint : Endpoint = Endpoint .logs ()
137+ await self ._listener .on_request_end (endpoint = endpoint , logs = logs )
136138 if update_cache :
137139 self .cache .logs = logs
138140 return logs
139141
140142 async def full_logs (self , update_cache : bool = True ) -> FullLogsData :
141143 """get application's full logs"""
142- full_logs : FullLogsData = await self .__client .logs_complete (self .id )
144+ full_logs : FullLogsData = await self .client .full_logs (self .id )
145+ endpoint : Endpoint = Endpoint .full_logs ()
146+ await self ._listener .on_request_end (
147+ endpoint = endpoint , full_logs = full_logs )
143148 if update_cache :
144149 self .cache .full_logs = full_logs
145150 return full_logs
146151
147152 async def status (self , update_cache : bool = True ) -> StatusData :
148153 """get application's status"""
149- status : StatusData = await self .__client .app_status (self .id )
154+ status : StatusData = await self .client .app_status (self .id )
155+ endpoint : Endpoint = Endpoint .app_status ()
156+ await self ._listener .on_request_end (endpoint = endpoint , status = status )
150157 if update_cache :
151158 self .cache .status = status
152159 return status
153160
154161 async def backup (self , update_cache : bool = True ) -> BackupData :
155162 """make backup of this application"""
156- backup : BackupData = await self .__client .backup (self .id )
163+ backup : BackupData = await self .client .backup (self .id )
164+ endpoint : Endpoint = Endpoint .backup ()
165+ await self ._listener .on_request_end (endpoint = endpoint , backup = backup )
157166 if update_cache :
158167 self .cache .backup = backup
159168 return backup
@@ -162,26 +171,26 @@ async def start(self, update_cache: bool = True) -> Response:
162171 """start the application"""
163172 if update_cache :
164173 self .cache .status .running = True
165- return await self .__client .start_app (self .id )
174+ return await self .client .start_app (self .id )
166175
167176 async def stop (self , update_cache : bool = True ) -> Response :
168177 """stop the application"""
169- result = await self .__client .stop_app (self .id )
178+ result = await self .client .stop_app (self .id )
170179 if update_cache :
171180 self .cache .status .running = False
172181 return result
173182
174183 async def restart (self , update_cache : bool = True ) -> Response :
175184 """restart the application"""
176- result = await self .__client .restart_app (self .id )
185+ result = await self .client .restart_app (self .id )
177186 if update_cache :
178187 self .cache .status .status = 'restarting'
179188 return result
180189
181190 async def delete (self ) -> Response :
182191 """delete the application"""
183- return await self .__client .delete_app (self .id )
192+ return await self .client .delete_app (self .id )
184193
185194 async def commit (self , file : File ) -> Response :
186195 """commit the application"""
187- return await self .__client .commit (self .id , file = file )
196+ return await self .client .commit (self .id , file = file )
0 commit comments