Skip to content

Commit 523247d

Browse files
♻️ refactor: Application methods parameters refactored and added new property "always_avoid_listeners"
1 parent f0b76a4 commit 523247d

File tree

1 file changed

+79
-107
lines changed

1 file changed

+79
-107
lines changed

squarecloud/app.py

Lines changed: 79 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ def __init__(
175175
cluster: str,
176176
isWebsite: bool,
177177
desc: str | None = None,
178-
**kwargs,
179178
) -> None:
180179
"""
181180
The `__init__` function is called when the class is instantiated.
182181
It sets up all the attributes that are passed in as arguments,
183-
and does any other initialization your class needs before it's ready
182+
and does any other initialization your class needs before It's ready
184183
for use.
185184
186185
@@ -210,6 +209,7 @@ def __init__(
210209
self._http: HTTPClient = http
211210
self._listener: CaptureListenerManager = CaptureListenerManager()
212211
self.cache: AppCache = AppCache()
212+
self.always_avoid_listeners: bool = False
213213

214214
def __repr__(self) -> str:
215215
"""
@@ -270,7 +270,6 @@ def desc(self) -> str | None:
270270

271271
@property
272272
def ram(self) -> int:
273-
274273
"""
275274
The ram function is a property that returns
276275
the amount of ram allocated to the application
@@ -324,7 +323,7 @@ def capture(self, endpoint: Endpoint) -> Callable:
324323
The capture function is a decorator that can be used to add a function
325324
to be called when a request is made to the specified endpoint.
326325
327-
:param self: Refer to the class instance
326+
:param self: Refer to the class instance.
328327
:param endpoint: Endpoint: Specify which endpoint the function will be
329328
called on
330329
:return: A decorator
@@ -365,261 +364,232 @@ def wrapper(func) -> None:
365364

366365
return wrapper
367366

368-
async def data(self, **kwargs) -> AppData:
367+
async def data(
368+
self, avoid_listener: bool = None, update_cache: bool = True
369+
) -> AppData:
369370
"""
370371
The data function is used to retrieve the data of an app.
371372
373+
:param update_cache: if True, update the application cache
374+
:param avoid_listener: whether the capture listener will be called
372375
:param self: Refer to the class instance
373-
:param kwargs: Pass a variable number of keyword arguments to the
374-
function
375376
:return: A AppData object
376377
:rtype: AppData
377378
"""
378379
app_data: AppData = await self.client.app_data(self.id)
379-
if not kwargs.get('avoid_listener'):
380+
avoid_listener = avoid_listener or self.always_avoid_listeners
381+
if not avoid_listener:
380382
endpoint: Endpoint = Endpoint.app_data()
381383
await self._listener.on_capture(
382384
endpoint=endpoint,
383385
before=self.cache.app_data,
384386
after=app_data,
385387
)
386-
if kwargs.get('update_cache', True):
388+
if update_cache:
387389
self.cache.update(app_data)
388390
return app_data
389391

390-
async def logs(self, **kwargs) -> LogsData:
392+
async def logs(
393+
self, avoid_listener: bool = None, update_cache: bool = True
394+
) -> LogsData:
391395
"""
392396
The logs function is used to get the application's logs.
393397
394398
:param self: Refer to the class instance
395-
:param kwargs: Pass a variable number of keyword arguments to a
396-
function
399+
:param update_cache: if True, update the application cache
400+
:param avoid_listener: whether the capture listener will be called
397401
:return: A LogsData object
398402
:rtype: LogsData
399403
"""
400404
logs: LogsData = await self.client.get_logs(self.id)
401-
if not kwargs.get('avoid_listener'):
405+
avoid_listener = avoid_listener or self.always_avoid_listeners
406+
if not avoid_listener:
402407
endpoint: Endpoint = Endpoint.logs()
403408
await self._listener.on_capture(
404409
endpoint=endpoint, before=self.cache.logs, after=logs
405410
)
406-
if kwargs.get('update_cache', True):
411+
if update_cache:
407412
self.cache.update(logs)
408413
return logs
409414

410-
async def status(self, **kwargs) -> StatusData:
415+
async def status(
416+
self, avoid_listener: bool = None, update_cache: bool = True
417+
) -> StatusData:
411418
"""
412419
The status function returns the status of an application.
413420
414421
:param self: Refer to the class instance
415-
:param kwargs: Pass a variable number of keyword arguments to a
416-
function
422+
:param update_cache: if True, update the application cache
423+
:param avoid_listener: whether the capture listener will be called
417424
:return: A StatusData object
418425
:rtype: StatusData
419426
"""
420427
status: StatusData = await self.client.app_status(self.id)
421-
if not kwargs.get('avoid_listener'):
428+
avoid_listener = avoid_listener or self.always_avoid_listeners
429+
if not avoid_listener:
422430
endpoint: Endpoint = Endpoint.app_status()
423431
await self._listener.on_capture(
424432
endpoint=endpoint, before=self.cache.status, after=status
425433
)
426-
if kwargs.get('update_cache', True):
434+
if update_cache:
427435
self.cache.update(status)
428436
return status
429437

430-
async def backup(self, **kwargs) -> BackupData:
438+
async def backup(
439+
self, avoid_listener: bool = None, update_cache: bool = True
440+
) -> BackupData:
431441
"""
432442
The backup function is used to create a backup of the application.
433443
434444
:param self: Refer to the class instance
435-
:param kwargs: Pass a variable number of keyword arguments to a
436-
function
445+
:param update_cache: if True, update the application cache
446+
:param avoid_listener: whether the capture listener will be called
437447
:return: A BackupData object
438448
:rtype: BackupData
439449
"""
440450
backup: BackupData = await self.client.backup(self.id)
441-
if not kwargs.get('avoid_listener'):
451+
avoid_listener = avoid_listener or self.always_avoid_listeners
452+
if not avoid_listener:
442453
endpoint: Endpoint = Endpoint.backup()
443454
await self._listener.on_capture(
444455
endpoint=endpoint, before=self.cache.backup, after=backup
445456
)
446-
if kwargs.get('update_cache', True):
457+
if update_cache:
447458
self.cache.update(backup)
448459
return backup
449460

450-
async def start(self, **kwargs) -> Response:
461+
async def start(self) -> Response:
451462
"""
452463
The start function starts the application.
453464
454465
:param self: Refer to the class instance
455-
:param kwargs: Pass a variable number of keyword arguments to the
456-
function
457466
:return: A Response object
458467
:rtype: Response
459468
"""
460-
response: Response = await self.client.start_app(self.id)
461-
if not kwargs.get('avoid_listener'):
462-
endpoint: Endpoint = Endpoint.start()
463-
await self._listener.on_capture(
464-
endpoint=endpoint, response=response
465-
)
469+
response: Response = await self.client.start_app(
470+
self.id, avoid_listener=True
471+
)
466472
return response
467473

468-
async def stop(self, **kwargs) -> Response:
474+
async def stop(self) -> Response:
469475
"""
470476
The stop function stops the application.
471477
472478
:param self: Refer to the class instance
473-
:param kwargs: Pass a variable number of keyword arguments to the
474-
function
475479
:return: A Response object
476480
:rtype: Response
477481
"""
478-
response: Response = await self.client.stop_app(self.id)
479-
if not kwargs.get('avoid_listener'):
480-
endpoint: Endpoint = Endpoint.stop()
481-
await self._listener.on_capture(
482-
endpoint=endpoint, response=response
483-
)
482+
response: Response = await self.client.stop_app(
483+
self.id, avoid_listener=True
484+
)
484485
return response
485486

486-
async def restart(self, **kwargs) -> Response:
487+
async def restart(self) -> Response:
487488
"""
488489
The restart function restarts the application.
489490
490491
:param self: Refer to the class instance
491-
:param kwargs: Pass a variable number of keyword arguments to the
492-
function
493492
:return: The Response object
494493
:rtype: Response
495494
"""
496-
response: Response = await self.client.restart_app(self.id)
497-
if not kwargs.get('avoid_listener'):
498-
endpoint: Endpoint = Endpoint.restart()
499-
await self._listener.on_capture(
500-
endpoint=endpoint, response=response
501-
)
495+
response: Response = await self.client.restart_app(
496+
self.id, avoid_listener=True
497+
)
502498
return response
503499

504-
async def delete(self, **kwargs) -> Response:
500+
async def delete(self) -> Response:
505501
"""
506502
The delete function deletes the application.
507503
508504
:param self: Refer to the class instance
509-
:param kwargs: Pass in keyword arguments as a dictionary
510505
:return: A Response object
511506
:rtype: Response
512507
"""
513-
response: Response = await self.client.delete_app(self.id)
514-
if not kwargs.get('avoid_listener'):
515-
endpoint: Endpoint = Endpoint.delete_app()
516-
await self._listener.on_capture(
517-
endpoint=endpoint, response=response
518-
)
508+
response: Response = await self.client.delete_app(
509+
self.id, avoid_listener=True
510+
)
519511
return response
520512

521-
async def commit(self, file: File, **kwargs) -> Response:
513+
async def commit(self, file: File) -> Response:
522514
"""
523515
The commit function is used to commit the application.
524516
525517
526518
:param self: Refer to the class instance
527519
:param file: File: The squarecloud.File to be committed
528-
:param kwargs: Pass a variable number of keyword arguments to the
529-
function
530520
:return: A Response object
531521
:rtype: Response
532522
"""
533-
response: Response = await self.client.commit(self.id, file=file)
534-
if not kwargs.get('avoid_listener'):
535-
endpoint: Endpoint = Endpoint.commit()
536-
await self._listener.on_capture(
537-
endpoint=endpoint, response=response
538-
)
523+
response: Response = await self.client.commit(
524+
self.id, file=file, avoid_listener=True
525+
)
539526
return response
540527

541-
async def files_list(self, path: str, **kwargs) -> list[FileInfo]:
528+
async def files_list(self, path: str) -> list[FileInfo]:
542529
"""
543530
The files_list function returns a list of files and folders in the
544531
specified directory.
545532
546533
:param self: Refer to the class instance
547534
:param path: str: Specify the path of the file to be listed
548-
:param kwargs: Pass a variable number of keyword arguments to the
549-
function
550535
:return: A list of FileInfo objects.
551536
:rtype: list[FileInfo]
552537
"""
553538
response: list[FileInfo] = await self.client.app_files_list(
554-
self.id, path
539+
self.id,
540+
path,
541+
avoid_listener=True,
555542
)
556-
557-
if not kwargs.get('avoid_listener'):
558-
endpoint: Endpoint = Endpoint.files_list()
559-
await self._listener.on_capture(
560-
endpoint=endpoint, response=response
561-
)
562543
return response
563544

564-
async def read_file(self, path: str, **kwargs) -> BytesIO:
545+
async def read_file(self, path: str) -> BytesIO:
565546
"""
566547
The read_file function reads the contents of a file from an app.
567548
568549
:param self: Refer to the class instance
569550
:param path: str: Specify the path of the file to be read
570-
:param kwargs: Pass in keyword arguments to the function
571551
:return: A BytesIO object
572552
:rtype: BytesIO
573553
"""
574-
response: BytesIO = await self.client.read_app_file(self.id, path)
575-
if not kwargs.get('avoid_listener'):
576-
endpoint: Endpoint = Endpoint.files_read()
577-
await self._listener.on_capture(
578-
endpoint=endpoint, response=response
579-
)
554+
response: BytesIO = await self.client.read_app_file(
555+
self.id, path, avoid_listener=True
556+
)
580557
return response
581558

582-
async def create_file(self, file: File, path: str, **kwargs) -> Response:
559+
async def create_file(self, file: File, path: str) -> Response:
583560

584561
"""
585562
The create_file function creates a file in the specified path.
586563
587564
:param self: Refer to the class instance
588565
:param file: File: Specify the file that is to be uploaded
589566
:param path: str: Specify the path of the file to be created
590-
:param kwargs: Pass additional keyword arguments to the function
591567
:return: A Response object
592568
:rtype: Response
593569
"""
594570
response: Response = await self.client.create_app_file(
595-
self.id, file, path
571+
self.id,
572+
file,
573+
path,
574+
avoid_listener=True,
596575
)
597-
if not kwargs.get('avoid_listener'):
598-
endpoint: Endpoint = Endpoint.files_read()
599-
await self._listener.on_capture(
600-
endpoint=endpoint, response=response
601-
)
602576
return response
603577

604-
async def delete_file(self, path: str, **kwargs) -> Response:
578+
async def delete_file(self, path: str) -> Response:
605579
"""
606580
The delete_file function deletes a file from the app.
607581
608582
:param self: Refer to the class instance
609583
:param path: str: Specify the path of the file to be deleted
610-
:param kwargs: Pass in a dictionary of additional arguments
611584
:return: A Response object
612585
:rtype: Response
613586
"""
614-
response: Response = await self.client.delete_app_file(self.id, path)
615-
if not kwargs.get('avoid_listener'):
616-
endpoint: Endpoint = Endpoint.files_delete()
617-
await self._listener.on_capture(
618-
endpoint=endpoint, response=response
619-
)
587+
response: Response = await self.client.delete_app_file(
588+
self.id, path, avoid_listener=True
589+
)
620590
return response
621591

622-
async def last_deploys(self, **kwargs) -> list[list[DeployData]]:
592+
async def last_deploys(self) -> list[list[DeployData]]:
623593
"""
624594
The last_deploys function returns a list of the last deploys for this
625595
application.
@@ -629,21 +599,23 @@ async def last_deploys(self, **kwargs) -> list[list[DeployData]]:
629599
:return: A list of DeployData objects
630600
"""
631601
response: list[list[DeployData]] = await self.client.last_deploys(
632-
self.id, avoid_listener=True,
602+
self.id,
603+
avoid_listener=True,
633604
)
634605
return response
635606

636-
async def github_integration(self, access_token: str, **kwargs) -> str:
607+
async def github_integration(self, access_token: str) -> str:
637608
"""
638609
The create_github_integration function returns a webhook to integrate
639610
with a GitHub repository.
640611
641612
:param self: Access the properties of the class
642613
:param access_token: str: Authenticate the user with GitHub
643-
:param kwargs: Pass in additional arguments to the function
644614
:return: A string containing the webhook url
645615
"""
646616
webhook: str = await self.client.github_integration(
647-
self.id, access_token, avoid_listener=True,
617+
self.id,
618+
access_token,
619+
avoid_listener=True,
648620
)
649621
return webhook

0 commit comments

Comments
 (0)