From b311691b092da9df755e5dbb70bd1d5835268817 Mon Sep 17 00:00:00 2001 From: lionellloh Date: Wed, 30 Sep 2020 17:09:43 +0800 Subject: [PATCH] Removed constants, include type hints --- __init__.py | 0 channel.py | 21 +++++++-------------- connection.py | 20 +++++++------------- constants.py | 14 -------------- 4 files changed, 14 insertions(+), 41 deletions(-) create mode 100644 __init__.py delete mode 100644 constants.py diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/channel.py b/channel.py index 138ad4a..3634f4a 100644 --- a/channel.py +++ b/channel.py @@ -1,22 +1,19 @@ -from constants import ChannelEvents -import websockets import asyncio import json +from typing import List class Channel: - def __init__(self, socket, topic, params): + def __init__(self, socket, topic: str, params: dict): self.socket = socket - self.topic = topic - self.params = params - self.callbacks = [] - self.joined = False - + self.topic: str = topic + self.params: dict = params + self.callbacks: List[function] = [] + self.joined: bool = False def join(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._join()) - print("joined") return self async def _join(self): @@ -27,16 +24,12 @@ async def _join(self): except Exception as e: # TODO: this needs some work. - # raise ChannelJoinFailure() from e - pass - + print("Failed to join. Check if Phoenix server is running") def on(self, event: str, callback): # TODO: Should I return self so that I can allow chaining? self.callbacks.append((event, callback)) - - def off(self, event: str): self.callbacks = [callback for callback in self.callbacks if callback[0] != event] diff --git a/connection.py b/connection.py index bdf6ff3..730a52f 100644 --- a/connection.py +++ b/connection.py @@ -5,31 +5,30 @@ import asyncio from messages import Message, ChannelEvents, PHOENIX_CHANNEL, HEARTBEAT_PAYLOAD + class Socket: def __init__(self, url: str, params: dict = {}, hb_interval: int = 5): self.url = url self.channels = defaultdict(list) self.connected = False - self.params: dict= params + self.params: dict = params self.hb_interval: int = hb_interval self.ws_connection: websockets.client.WebSocketClientProtocol = None - self.kept_alive = False - + self.kept_alive: bool = False def listen(self): loop = asyncio.get_event_loop() loop.run_until_complete(asyncio.gather(self._listen(), self._keep_alive())) - async def _listen(self): while True: try: msg = await self.ws_connection.recv() # TODO: Load msg into some class with expected schema msg = Message(**json.loads(msg)) - if msg.event== ChannelEvents.reply: + if msg.event == ChannelEvents.reply: continue # TODO: use a named tuple? for channel in self.channels.get(msg.topic, []): @@ -41,17 +40,14 @@ async def _listen(self): print('Connection Closed') break - def connect(self): loop = asyncio.get_event_loop() loop.run_until_complete(self._connect()) - async def _connect(self): ws_connection = await websockets.connect(self.url) if ws_connection.open: # TODO: Include a logger to indicate successful connection - print(type(ws_connection)) self.ws_connection = ws_connection self.connected = True @@ -88,8 +84,6 @@ def set_channel(self, topic: str): # TODO: Implement this to show summary to subscriptions def summary(self): # print a summary of subscriptions from the socket - for topic, chans in self.channels.items(): - for chan in chans: - print(f"Topic: {topic} | Events: {[e for e, _ in chan.callbacks]}]") - - + for topic, chans in self.channels.items(): + for chan in chans: + print(f"Topic: {topic} | Events: {[e for e, _ in chan.callbacks]}]") diff --git a/constants.py b/constants.py deleted file mode 100644 index 0f3760a..0000000 --- a/constants.py +++ /dev/null @@ -1,14 +0,0 @@ -from enum import Enum - - -class ChannelEvents(str, Enum): - close = "phx_close" - error = "phx_error" - join = "phx_join" - reply = "phx_reply" - leave = "phx_leave" - heartbeat = "heartbeat" - - -PHOENIX_CHANNEL = "phoenix" -HEARTBEAT_PAYLOAD = {"msg": "ping"} \ No newline at end of file