Skip to content

Commit

Permalink
makes the client ready for Websockets 14.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jmts-w24 committed Nov 12, 2024
1 parent 73718cc commit 5908aaa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ boto3>=1.14.44
certifi>=2020.12.5
colorama>=0.4.4
cryptography>=42.0.7
packaging>=21.3
pint>=0.21
pydantic-extra-types>=2.1.0
pydantic>=2.5.1
Expand Down
12 changes: 11 additions & 1 deletion werk24/cli/health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
import os.path
import websockets
from websockets.exceptions import InvalidStatusCode
from websockets.exceptions import InvalidStatusCode, InvalidStatus
from werk24.cli import utils
import werk24
from werk24.techread_client import LicenseError
Expand Down Expand Up @@ -91,11 +91,21 @@ async def get_network_info() -> dict:
try:
async with websockets.connect(URI):
return {"ws-api.w24.co Connection": "Successful"}

# Websocket 14.0 and above
except InvalidStatus as e:
if e.response.status_code == 401:
return {"Websocket Connection": "Successful, Connected"}
else:
return {"Websocket Connection": f"Unsuccessful: {str(e.status_code)}"}

# Websocket 13.0 and below
except InvalidStatusCode as e:
if e.status_code == 401:
return {"Websocket Connection": "Successful, Connected"}
else:
return {"Websocket Connection": f"Unsuccessful: {str(e.status_code)}"}

except Exception as e:
return {"Websocket Connection": f"Unsuccessful: {type(e)}, {e}"}

Expand Down
26 changes: 21 additions & 5 deletions werk24/techread_client_wss.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Websocket-part of the Werk24 client
"""
import json
from packaging.version import Version
from types import TracebackType
from typing import Optional, Type, AsyncGenerator

Expand All @@ -15,6 +16,12 @@
# make the logger
logger = logging.getLogger("w24_techread_client")

try:
version = Version(websockets.__version__)
USE_EXTRA_HEADERS = version < Version("14.0")
except:
USE_EXTRA_HEADERS = False

class TechreadClientWss:
"""
TechreadClient subpart that handles the websocket
Expand Down Expand Up @@ -67,11 +74,20 @@ async def __aenter__(self) -> "TechreadClientWss":
"""
logger.debug(f"Entered the session with the server {self._techread_server_wss}")
headers = self._auth_client.get_auth_headers()
self._techread_session_wss = await websockets.connect(
self.endpoint,
extra_headers=headers,
close_timeout=self.wss_close_timeout,
)

if USE_EXTRA_HEADERS:
self._techread_session_wss = await websockets.connect(
self.endpoint,
extra_headers=headers,
close_timeout=self.wss_close_timeout,
)
else:
self._techread_session_wss = await websockets.connect(
self.endpoint,
additional_headers=headers,
close_timeout=self.wss_close_timeout,
)

return self

async def __aexit__(
Expand Down

0 comments on commit 5908aaa

Please sign in to comment.