-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added websockets dependency * client and logger added * documentos updated
- Loading branch information
1 parent
7e03ff6
commit e77c995
Showing
7 changed files
with
425 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,4 +40,5 @@ nosetests.xml | |
zap | ||
server.py | ||
sanic_server.py | ||
starlette_server.py | ||
starlette_server.py. | ||
client_demo.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ name = "zaptools" | |
description = "A toolkit for Event-Driven websocket management" | ||
readme = "README.md" | ||
authors = [{name = "Nathan Mejia", email = "[email protected]"}] | ||
version = "0.2.5" | ||
version = "0.3.0" | ||
requires-python = ">=3.10" | ||
classifiers = [ | ||
'Development Status :: 4 - Beta', | ||
|
@@ -17,6 +17,10 @@ classifiers = [ | |
'Programming Language :: Python :: 3.10', | ||
] | ||
license = { file = "LICENSE" } | ||
dependencies = [ | ||
"rich>=13.9.4", | ||
"websockets>=14.1", | ||
] | ||
|
||
[project.urls] | ||
"Homepage" = "https://github.com/NathanDraco22/zaptools-python" | ||
|
@@ -25,3 +29,8 @@ license = { file = "LICENSE" } | |
dev = [ | ||
"pytest" | ||
] | ||
|
||
[dependency-groups] | ||
dev = [ | ||
"build>=1.2.2.post1", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import enum | ||
import json | ||
import asyncio | ||
from typing import Any, AsyncGenerator | ||
from websockets.asyncio.client import connect | ||
|
||
from .tools import EventData | ||
from .zap_logger import zap_logger | ||
|
||
class ZapClientState(enum.Enum): | ||
ONLINE = 1 | ||
OFFLINE = 2 | ||
CONNECTING = 3 | ||
ERROR = 4 | ||
|
||
class ZapClient: | ||
|
||
_connection_state_queue = asyncio.Queue[ZapClientState]() | ||
_current_state = ZapClientState.OFFLINE | ||
|
||
async def connect(self, url: str) -> None: | ||
await self._update_connection_state(ZapClientState.CONNECTING) | ||
self._conn = await connect(url) | ||
await self._update_connection_state(ZapClientState.ONLINE) | ||
zap_logger.info_green(f"Connected to {url}") | ||
|
||
|
||
async def send( | ||
self, | ||
event_name: str, | ||
payload: dict[str, Any], | ||
headers: dict[str, Any]|None | ||
) -> None: | ||
conn = self._conn | ||
inner_header = headers if headers is not None else {} | ||
event_data = EventData(event_name, payload, inner_header) | ||
event_json = json.dumps(event_data.to_dict()) | ||
await conn.send(event_json) | ||
|
||
|
||
async def event_stream(self) -> AsyncGenerator[EventData, None]: | ||
conn = self._conn | ||
while True: | ||
try: | ||
data = await conn.recv() | ||
except Exception: | ||
await self._update_connection_state(ZapClientState.ERROR) | ||
zap_logger.error("Error receiving data from server") | ||
break | ||
data = json.loads(data) | ||
try: | ||
event_data = EventData( | ||
data["eventName"], | ||
data["payload"], | ||
data["headers"] | ||
) | ||
yield event_data | ||
except Exception: | ||
await self._update_connection_state(ZapClientState.ERROR) | ||
zap_logger.error("Error parsing event from server") | ||
break | ||
await self._update_connection_state(ZapClientState.OFFLINE) | ||
zap_logger.warning("Disconnected from server") | ||
|
||
async def connection_state(self) -> AsyncGenerator[ZapClientState, None]: | ||
while True: | ||
yield await self._connection_state_queue.get() | ||
|
||
async def close(self) -> None: | ||
await self._conn.close() | ||
|
||
async def _update_connection_state(self, state: ZapClientState): | ||
self._current_state = state | ||
await self._connection_state_queue.put(state) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import logging | ||
from rich.logging import RichHandler | ||
|
||
|
||
FORMAT = "%(message)s" | ||
class ZapLogger(): | ||
def __init__(self): | ||
logging.basicConfig( | ||
level= logging.INFO, | ||
format=FORMAT, datefmt="[%X]", | ||
handlers=[RichHandler(rich_tracebacks=True)] | ||
) | ||
self.logger = logging.getLogger('zap_logger') | ||
|
||
def info(self, message): | ||
self.logger.info(f"[Zap]{message}") | ||
|
||
def info_green(self, message): | ||
self.logger.info(f"[green][Zap]{message}", extra={"markup": True}) | ||
|
||
def warning(self, message): | ||
self.logger.warning(f"[yellow][Zap]{message}", extra={"markup": True}) | ||
|
||
def error(self, message): | ||
self.logger.error(f"[red][Zap]{message}", extra={"markup": True}) | ||
|
||
|
||
zap_logger = ZapLogger() | ||
|
||
|