Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API version selector #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mir_connector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ flake8 --max-line-length=100 --exclude venv
```bash
# Create the virtualenv if not active already
virtualenv venv/
pip install -e '.[dev]'
. venv/bin/activate
pip install -e '.[dev]'
pytest -v
```

Expand Down
9 changes: 5 additions & 4 deletions mir_connector/inorbit_mir_connector/config/mir100_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from pydantic import BaseModel, field_validator
from .config_base_model import EdgeConnectorModel
from .utils import read_yaml
from ..src.mir_api import APIS

# TODO: leverate ruamel.yaml capabilities to add comments to
# TODO: leverage ruamel.yaml capabilities to add comments to
# the yaml and improve how the default configuration section
# that gets added automatically looks.
default_mir100_config = {
Expand All @@ -30,7 +31,6 @@

# Expected values
CONNECTOR_TYPE = "mir100"
MIR_API_VERSION = "v2.0"


class MiR100ConfigModel(BaseModel):
Expand All @@ -49,9 +49,10 @@ class MiR100ConfigModel(BaseModel):

@field_validator("mir_api_version")
def api_version_validation(cls, mir_api_version):
if mir_api_version != MIR_API_VERSION:
if mir_api_version not in APIS.keys():
raise ValueError(
f"Unexpected MiR API version '{mir_api_version}'. Expected '{MIR_API_VERSION}'"
f"MiR API version '{mir_api_version}' is not supported. "
f"Supported versions are: {', '.join(APIS.keys())}"
)
return mir_api_version

Expand Down
9 changes: 5 additions & 4 deletions mir_connector/inorbit_mir_connector/src/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from inorbit_edge.robot import COMMAND_NAV_GOAL
from inorbit_edge.robot import RobotSession
from inorbit_edge.video import OpenCVCamera
from .mir_api import MirApiV2
from .mir_api import MirWebSocketV2
from .mir_api import APIS
from .mission import MirInorbitMissionTracking
from ..config.mir100_model import MiR100Config

Expand Down Expand Up @@ -42,9 +41,11 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:
log_level = config.log_level.value
self.logger = logging.getLogger(name=self.__class__.__name__)
self.logger.setLevel(log_level)
self.api_version = config.connector_config.mir_api_version
self.logger.debug(f"Using MiR API version: {self.api_version}")

# Configure the connection to the robot
self.mir_api = MirApiV2(
self.mir_api = APIS[self.api_version]["rest"](
mir_host_address=config.connector_config.mir_host_address,
mir_username=config.connector_config.mir_username,
mir_password=config.connector_config.mir_password,
Expand All @@ -54,7 +55,7 @@ def __init__(self, robot_id: str, config: MiR100Config) -> None:
)

# Configure the ws connection to the robot
self.mir_ws = MirWebSocketV2(
self.mir_ws = APIS[self.api_version]["ws"](
mir_host_address=config.connector_config.mir_host_address,
mir_ws_port=config.connector_config.mir_ws_port,
mir_use_ssl=config.connector_config.mir_use_ssl,
Expand Down
5 changes: 5 additions & 0 deletions mir_connector/inorbit_mir_connector/src/mir_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from .mir_api_base import MirApiBaseClass # noqa: F401
from .mir_api_v2 import MirApiV2 # noqa: F401
from .mir_api_v2 import MirWebSocketV2 # noqa: F401

# Available API versions
APIS = {
"v2.0": {"rest": MirApiV2, "ws": MirWebSocketV2},
}
Comment on lines +6 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without having much context, are we sure these versions are fixed?
E.g: V2.0 means V2 for Rest and WS?
Could it happen that a model uses different APIs versions for WS and rest?

13 changes: 8 additions & 5 deletions mir_connector/inorbit_mir_connector/tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@
import websocket
from inorbit_mir_connector.src.connector import Mir100Connector
from unittest.mock import MagicMock, Mock, call
from inorbit_mir_connector.src.mir_api import MirApiV2
from inorbit_mir_connector.src.mir_api import APIS
import inorbit_mir_connector.src.connector
from inorbit_edge.robot import RobotSession
from inorbit_mir_connector.config.mir100_model import MiR100Config

API_VERSION = "v2.0"
MirApi = APIS[API_VERSION]["rest"]


# NOTE(b-Tomas): Added some example data below to help creating rea
@pytest.fixture
def connector(monkeypatch):
monkeypatch.setenv("INORBIT_KEY", "abc123")
monkeypatch.setattr(MirApiV2, "_create_api_session", MagicMock())
monkeypatch.setattr(MirApiV2, "_create_web_session", MagicMock())
monkeypatch.setattr(MirApi, "_create_api_session", MagicMock())
monkeypatch.setattr(MirApi, "_create_web_session", MagicMock())
monkeypatch.setattr(websocket, "WebSocketApp", MagicMock())
monkeypatch.setattr(RobotSession, "connect", MagicMock())
monkeypatch.setattr(inorbit_mir_connector.src.connector.os, "makedirs", Mock())
Expand Down Expand Up @@ -125,8 +128,8 @@ def test_registers_user_scripts_config(monkeypatch):

def create_connector(user_scripts):
monkeypatch.setenv("INORBIT_KEY", "abc123")
monkeypatch.setattr(MirApiV2, "_create_api_session", MagicMock())
monkeypatch.setattr(MirApiV2, "_create_web_session", MagicMock())
monkeypatch.setattr(MirApi, "_create_api_session", MagicMock())
monkeypatch.setattr(MirApi, "_create_web_session", MagicMock())
monkeypatch.setattr(websocket, "WebSocketApp", MagicMock())
monkeypatch.setattr(RobotSession, "connect", MagicMock())
monkeypatch.setattr(RobotSession, "register_commands_path", MagicMock())
Expand Down
Loading