Skip to content

Commit

Permalink
Added optional args and kwargs arguments to on_success and on_failure (
Browse files Browse the repository at this point in the history
…#55)

Co-authored-by: Ollie <[email protected]>
  • Loading branch information
ITz-Viks and olijeffers0n authored Aug 30, 2023
1 parent 610ab3d commit be46782
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
15 changes: 11 additions & 4 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from typing import List, Callable, Union
from typing import List, Union, Coroutine, Callable, Dict, Tuple
from PIL import Image

from .remote.events.event_loop_manager import EventLoopManager
Expand Down Expand Up @@ -113,8 +113,10 @@ async def connect(
self,
retries: int = float("inf"),
delay: int = 20,
on_failure=None,
on_success=None,
on_failure: Union[Coroutine, Callable[[], None], None] = None,
on_success: Union[Coroutine, Callable[[], None], None] = None,
on_success_args_kwargs: Tuple[List, Dict] = ([], {}),
on_failure_args_kwargs: Tuple[List, Dict] = ([], {}),
) -> None:
"""
Attempts to open a connection to the rust game server specified in the constructor
Expand All @@ -123,7 +125,10 @@ async def connect(
:param delay: The delay (in seconds) between reconnection attempts.
:param on_failure: Optional function to be called when connecting fails.
:param on_success: Optional function to be called when connecting succeeds.
:param on_success_args_kwargs: Optional tuple holding keyword and regular arguments
for on_success in this format (args, kwargs)
:param on_failure_args_kwargs: Optional tuple holding keyword and regular arguments
for on_failure in this format (args, kwargs)
:return: None
"""
Expand Down Expand Up @@ -151,6 +156,8 @@ async def connect(
delay=delay,
on_failure=on_failure,
on_success=on_success,
on_success_args_kwargs=on_success_args_kwargs,
on_failure_args_kwargs=on_failure_args_kwargs,
)
await self.heartbeat.start_beat()
except ConnectionRefusedError:
Expand Down
12 changes: 11 additions & 1 deletion rustplus/api/remote/rust_remote_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ def __init__(
self.pending_entity_subscriptions = []
self.camera_manager: Union[CameraManager, None] = None

async def connect(self, retries, delay, on_failure=None, on_success=None) -> None:
async def connect(
self,
retries,
delay,
on_failure,
on_success,
on_success_args_kwargs,
on_failure_args_kwargs,
) -> None:
self.ws = RustWebsocket(
server_id=self.server_id,
remote=self,
Expand All @@ -76,6 +84,8 @@ async def connect(self, retries, delay, on_failure=None, on_success=None) -> Non
on_failure=on_failure,
on_success=on_success,
delay=delay,
on_success_args_kwargs=on_success_args_kwargs,
on_failure_args_kwargs=on_failure_args_kwargs,
)
await self.ws.connect(retries=retries)

Expand Down
24 changes: 20 additions & 4 deletions rustplus/api/remote/rustws.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def __init__(
on_failure,
on_success,
delay,
on_success_args_kwargs,
on_failure_args_kwargs,
):
self.connection: Union[WebSocketClientProtocol, None] = None
self.task: Union[Task, None] = None
Expand All @@ -49,6 +51,8 @@ def __init__(
self.on_failure = on_failure
self.on_success = on_success
self.delay = delay
self.on_success_args_kwargs = on_success_args_kwargs
self.on_failure_args_kwargs = on_failure_args_kwargs

async def connect(
self, retries=float("inf"), ignore_open_value: bool = False
Expand Down Expand Up @@ -90,9 +94,15 @@ async def connect(
if self.on_success is not None:
try:
if asyncio.iscoroutinefunction(self.on_success):
await self.on_success()
await self.on_success(
*self.on_success_args_kwargs[0],
**self.on_success_args_kwargs[1],
)
else:
self.on_success()
self.on_success(
*self.on_success_args_kwargs[0],
**self.on_success_args_kwargs[1],
)
except Exception as e:
self.logger.warning(e)
break
Expand All @@ -105,9 +115,15 @@ async def connect(
if self.on_failure is not None:
try:
if asyncio.iscoroutinefunction(self.on_failure):
val = await self.on_failure()
val = await self.on_failure(
*self.on_failure_args_kwargs[0],
**self.on_failure_args_kwargs[1],
)
else:
val = self.on_failure()
val = self.on_failure(
*self.on_failure_args_kwargs[0],
**self.on_failure_args_kwargs[1],
)

if val is not None:
print_error = val
Expand Down

0 comments on commit be46782

Please sign in to comment.