@@ -219,18 +219,51 @@ def _get_application_status(self, app_id: str):
219219 )
220220 return response ["Result" ]["Status" ], response
221221
222- def _list_application (self ):
223- response = ve_request (
224- request_body = {},
225- action = "ListApplications" ,
226- ak = self .ak ,
227- sk = self .sk ,
228- service = "vefaas" ,
229- version = "2021-03-03" ,
230- region = "cn-beijing" ,
231- host = "open.volcengineapi.com" ,
232- )
233- return response ["Result" ]["Items" ]
222+ def _list_application (self , app_id : str = None , app_name : str = None ):
223+ # firt match app_id. if app_id is None,then match app_name and remove app_id
224+ request_body = {
225+ "OrderBy" : {"Key" : "CreateTime" , "Ascend" : False },
226+ "FunctionId" : app_id if app_id else None ,
227+ "Filters" : [{"Item" : {"Key" : "Name" , "Value" : [app_name ]}}]
228+ if app_name and not app_id
229+ else None ,
230+ }
231+ # remove None
232+ request_body = {k : v for k , v in request_body .items () if v is not None }
233+
234+ page_size = 50
235+ page_number = 1
236+ all_items = []
237+ total_page = None
238+ while True :
239+ try :
240+ request_body .update ({"PageNumber" : page_number , "PageSize" : page_size })
241+ response = ve_request (
242+ request_body = request_body ,
243+ action = "ListApplications" ,
244+ ak = self .ak ,
245+ sk = self .sk ,
246+ service = "vefaas" ,
247+ version = "2021-03-03" ,
248+ region = "cn-beijing" ,
249+ host = "open.volcengineapi.com" ,
250+ )
251+ result = response .get ("Result" , {})
252+ items = result .get ("Items" , [])
253+ all_items .extend (items )
254+
255+ if total_page is None :
256+ total = result .get ("Total" , 0 )
257+ total_page = (total + page_size - 1 ) // page_size
258+
259+ if page_number >= total_page or not items :
260+ break
261+ page_number += 1
262+ except Exception as e :
263+ raise ValueError (
264+ f"List application failed. Error: { str (e )} . Response: { response } ."
265+ )
266+ return all_items
234267
235268 def _update_function_code (
236269 self ,
@@ -306,7 +339,7 @@ def _update_function_code(
306339 def get_application_details (self , app_id : str = None , app_name : str = None ):
307340 if not app_id and not app_name :
308341 raise ValueError ("app_id and app_name cannot be both empty." )
309- apps = self ._list_application ()
342+ apps = self ._list_application (app_id = app_id , app_name = app_name )
310343 if app_id :
311344 for app in apps :
312345 if app ["Id" ] == app_id :
@@ -318,7 +351,7 @@ def get_application_details(self, app_id: str = None, app_name: str = None):
318351 return app
319352
320353 def find_app_id_by_name (self , name : str ):
321- apps = self ._list_application ()
354+ apps = self ._list_application (app_name = name )
322355 for app in apps :
323356 if app ["Name" ] == name :
324357 return app ["Id" ]
0 commit comments