diff --git a/README.md b/README.md new file mode 100644 index 00000000..bcd7d6b6 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# `royalnet` + diff --git a/requirements.txt b/requirements.txt index b4556fcd..483e63f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ python-telegram-bot>=11.1.0 websockets>=7.0 +pytest>=4.3.1 diff --git a/royalnet/bots/__init__.py b/royalnet/bots/__init__.py index e69de29b..83599e7e 100644 --- a/royalnet/bots/__init__.py +++ b/royalnet/bots/__init__.py @@ -0,0 +1,3 @@ +from .telegram import TelegramBot + +__all__ = ["TelegramBot"] diff --git a/royalnet/bots/telegram.py b/royalnet/bots/telegram.py index d3c46c33..b56e23be 100644 --- a/royalnet/bots/telegram.py +++ b/royalnet/bots/telegram.py @@ -6,6 +6,10 @@ from ..utils import asyncify, Call, Command from ..network import RoyalnetLink, Message +async def todo(message: Message): + pass + + class TelegramBot: def __init__(self, api_key: str, @@ -17,7 +21,7 @@ class TelegramBot: self.should_run: bool = False self.offset: int = -100 self.missing_command = missing_command - self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "telegram", null) + self.network: RoyalnetLink = RoyalnetLink(master_server_uri, master_server_secret, "telegram", todo) # Generate commands self.commands = {} for command in commands: diff --git a/royalnet/network/royalnetlink.py b/royalnet/network/royalnetlink.py index 6f6f679b..557cb1e2 100644 --- a/royalnet/network/royalnetlink.py +++ b/royalnet/network/royalnetlink.py @@ -68,7 +68,7 @@ class RoyalnetLink: self.request_handler = request_handler self._pending_requests: typing.Dict[typing.Optional[Message]] = {} self._connect_event: asyncio.Event = asyncio.Event() - self._identify_event: asyncio.Event = asyncio.Event() + self.identify_event: asyncio.Event = asyncio.Event() async def connect(self): log.info(f"Connecting to {self.master_uri}...") @@ -83,7 +83,7 @@ class RoyalnetLink: except websockets.ConnectionClosed: self.websocket = None self._connect_event.clear() - self._identify_event.clear() + self.identify_event.clear() log.info(f"Connection to {self.master_uri} was closed.") # What to do now? Let's just reraise. raise @@ -100,7 +100,7 @@ class RoyalnetLink: response = response_package.data if isinstance(response, ErrorMessage): raise NetworkError(response, "Server returned error while identifying self") - self._identify_event.set() + self.identify_event.set() log.info(f"Identified successfully!") @requires_identification @@ -128,7 +128,7 @@ class RoyalnetLink: while True: if self.websocket is None: await self.connect() - if not self._identify_event.is_set(): + if not self.identify_event.is_set(): await self.identify() package: Package = await self.receive() # Package is a response diff --git a/setup.py b/setup.py new file mode 100644 index 00000000..6595a41a --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +import setuptools + +with open("README.md", "r") as f: + long_description = f.read() + +setuptools.setup( + name="royalnet", + version="5.0a1", + author="Stefano Pigozzi", + author_email="ste.pigozzi@gmail.com", + description="The great bot network of the Royal Games community", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/Steffo99/royalnet", + packages=setuptools.find_packages(), + install_requires=[ + "python-telegram-bot>=11.1.0", + "websockets>=7.0" + ], + python_requires=">=3.7", + classifiers=[ + "Development Status :: 3 - Alpha", + "Operating System :: OS Independent", + "Programming Language :: Python :: 3.7", + "Topic :: Internet" + ] +) diff --git a/tests/test_network.py b/tests/test_network.py new file mode 100644 index 00000000..75c2880c --- /dev/null +++ b/tests/test_network.py @@ -0,0 +1,33 @@ +import pytest +import asyncio +from royalnet.network import RoyalnetLink, RoyalnetServer +from royalnet.network import Message + + +async def echo(message: Message): + return message + + +def test_connection(): + loop = asyncio.SelectorEventLoop() + server = RoyalnetServer("localhost", 1234, "testing") + link = RoyalnetLink("ws://localhost:1234", "testing", "testing", echo) + loop.create_task(server.run()) + loop.run_until_complete(link.run()) + assert link.websocket is not None + assert link.identify_event.is_set() + assert len(server.identified_clients) == 1 + assert server.identified_clients[0].link_type == "testing" + + +def test_request(): + loop = asyncio.SelectorEventLoop() + server = RoyalnetServer("localhost", 1234, "testing") + link1 = RoyalnetLink("ws://localhost:1234", "testing", "testing1", echo) + link2 = RoyalnetLink("ws://localhost:1234", "testing", "testing2", echo) + loop.create_task(server.run()) + loop.create_task(link1.run()) + loop.create_task(link2.run()) + message = Message() + response = loop.run_until_complete(link1.request(message, "testing2")) + assert message is response