1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

Add support for network broadcasts

This commit is contained in:
Steffo 2019-10-20 18:54:48 +02:00
parent f42e9d4180
commit a0147b0e73
2 changed files with 22 additions and 19 deletions

View file

@ -30,7 +30,7 @@ python-telegram-bot==12.2.0
pytz==2019.3
pywin32-ctypes==0.2.0
regex==2019.8.19
royalherald==5.0b8
royalherald==5.1b2
sentry-sdk==0.13.0
six==1.12.0
sortedcontainers==2.1.0

View file

@ -29,7 +29,7 @@ class GenericBot:
self._Data = self._data_factory()
self.commands = {}
self.network_handlers: typing.Dict[str, typing.Callable[["GenericBot", typing.Any],
typing.Awaitable[typing.Dict]]] = {}
typing.Awaitable[typing.Optional[typing.Dict]]]] = {}
for SelectedCommand in self.uninitialized_commands:
interface = self._Interface()
try:
@ -97,26 +97,29 @@ class GenericBot:
log.debug(f"Running NetworkLink {self.network}")
self.loop.create_task(self.network.run())
async def _network_handler(self, request: rh.Request) -> rh.Response:
async def _network_handler(self, message: typing.Union[rh.Request, rh.Broadcast]) -> rh.Response:
try:
network_handler = self.network_handlers[request.handler]
network_handler = self.network_handlers[message.handler]
except KeyError:
log.warning(f"Missing network_handler for {request.handler}")
return rh.ResponseFailure("no_handler", f"This bot is missing a network handler for {request.handler}.")
log.warning(f"Missing network_handler for {message.handler}")
return rh.ResponseFailure("no_handler", f"This bot is missing a network handler for {message.handler}.")
else:
log.debug(f"Using {network_handler} as handler for {request.handler}")
try:
response_data = await network_handler(self, **request.data)
return rh.ResponseSuccess(data=response_data)
except Exception as e:
sentry_sdk.capture_exception(e)
log.error(f"Exception {e} in {network_handler}")
return rh.ResponseFailure("exception_in_handler",
f"An exception was raised in {network_handler} for {request.handler}.",
extra_info={
"type": e.__class__.__name__,
"message": str(e)
})
log.debug(f"Using {network_handler} as handler for {message.handler}")
if isinstance(message, rh.Request):
try:
response_data = await network_handler(self, **message.data)
return rh.ResponseSuccess(data=response_data)
except Exception as e:
sentry_sdk.capture_exception(e)
log.error(f"Exception {e} in {network_handler}")
return rh.ResponseFailure("exception_in_handler",
f"An exception was raised in {network_handler} for {message.handler}.",
extra_info={
"type": e.__class__.__name__,
"message": str(e)
})
elif isinstance(message, rh.Broadcast):
await network_handler(self, **message.data)
def _init_database(self):
"""Create an :py:class:`royalnet.database.Alchemy` with the tables required by the packs. Then,