Skip to content

Commit

Permalink
Removed constants, include type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
lionellloh committed Sep 30, 2020
1 parent 4987917 commit b311691
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 41 deletions.
Empty file added __init__.py
Empty file.
21 changes: 7 additions & 14 deletions channel.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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]
20 changes: 7 additions & 13 deletions connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, []):
Expand All @@ -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

Expand Down Expand Up @@ -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]}]")
14 changes: 0 additions & 14 deletions constants.py

This file was deleted.

0 comments on commit b311691

Please sign in to comment.