Skip to content
This repository has been archived by the owner on Jan 21, 2025. It is now read-only.

Commit

Permalink
Merge pull request #17 from wandelbotsgmbh/fix/websocket-auth
Browse files Browse the repository at this point in the history
fix: support auth for websocket handling
  • Loading branch information
stefanwagnerdev authored Nov 15, 2024
2 parents debb463 + 1953ecd commit 7616f94
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 313 deletions.
5 changes: 4 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"args": ["tests", "--maxfail=1", "--disable-warnings"],
"console": "integratedTerminal",
"justMyCode": false,
"envFile": "${workspaceFolder}/.env"
"envFile": "${workspaceFolder}/.env",
"env": {
"PYTHONPATH": "${workspaceFolder}"
}
}
]
}
567 changes: 268 additions & 299 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ readme = "README.md"
python = "^3.9"
httpx = "0.27.2"
requests = "2.32.3"
wandelbots_api_client = "24.8.0.dev17"
websockets = "13.0.1"
wandelbots_api_client = "24.8.0rc6"
websockets = ">=14.0.0"
numpy = "^1.26"
scipy = "^1.11"
nbqa = "^1.9.0"
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ ipyleaflet
# Python: plotting libraries (optional)
plotly>=5,<6
bqplot

python-dotenv
websockets>14
9 changes: 7 additions & 2 deletions wandelbots/apis/motion.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ def stream_motion(
direction: Literal["forward", "backward"] = "forward",
callback: Callable[[MoveResponse], None] = None,
) -> None:
uri = f"{instance.socket_uri}/cells/{cell}/motions/{motion}/execute{direction}"
uri += f"?playback_speed_in_percent={playback_speed}&response_rate={response_rate}"
additional_params = {
"playback_speed_in_percent": playback_speed,
"response_rate": response_rate,
}
uri = instance.get_socket_uri_with_auth(
additional_params, f"/cells/{cell}/motions/{motion}/execute{direction}"
)
logger.debug(f"Connecting to {uri}")
with connect(uri) as socket:
logger.debug(f"Connected to {uri}")
Expand Down
23 changes: 17 additions & 6 deletions wandelbots/core/instance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from urllib.parse import urlencode

from wandelbots.util.logger import _get_logger


Expand Down Expand Up @@ -41,13 +43,22 @@ def _parse_url(self, host: str) -> str:

@property
def socket_uri(self):
if not self.has_auth():
_uri = self.url.replace("http", "ws").replace("https", "wss")
return f"{_uri}/api/{self._api_version}"
return self.url.replace("http", "ws").replace("https", "wss")

def get_socket_uri_with_auth(self, additional_params: dict = None, url: str = None):
if self.has_basic_auth():
_uri = self.socket_uri.replace("wss://", f"wss://{self.user}:{self.password}@")
else:
_url_no_scheme = self.url.split("://")[1]
_uri = f"wss://{_url_no_scheme}"
return f"{_uri}/api/{self._api_version}"
_uri = self.socket_uri
params = {}
if self.has_access_token():
params["token"] = self.access_token

if additional_params:
params.update(additional_params)

query_string = urlencode(params)
return f"{_uri}/api/{self._api_version}/{url}?{query_string}"

def _connect(self):
self.logger.info(f"Connecting to {self.url}")
Expand Down
8 changes: 5 additions & 3 deletions wandelbots/request/syncs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from typing import Dict, Optional, Tuple

import httpx
import requests
from typing import Dict, Tuple, Optional
from wandelbots.util.logger import _get_logger
from wandelbots.request.config import TIMEOUT

from wandelbots.core.instance import Instance
from wandelbots.request.config import TIMEOUT
from wandelbots.util.logger import _get_logger

__logger = _get_logger(__name__)

Expand Down

0 comments on commit 7616f94

Please sign in to comment.