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

Raise in validate_server_version instead of returning a bool #19

Merged
Merged
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
22 changes: 22 additions & 0 deletions go2rtc_client/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ class Go2RtcClientError(Exception):
"""Base exception for go2rtc client."""


class Go2RtcVersionError(Exception):
"""Base exception for go2rtc client."""

def __init__(
self,
server_version: str | None,
min_version_supported: str,
max_version_supported: str,
) -> None:
"""Initialize."""
self._server_version = server_version
self._min_version_supported = min_version_supported
self._max_version_supported = max_version_supported

def __str__(self) -> str:
"""Return exception message."""
return (
f"server version '{self._server_version}' not "
f">= {self._min_version_supported} and < {self._max_version_supported}"
)


def handle_error[**_P, _R](
func: Callable[_P, Coroutine[Any, Any, _R]],
) -> Callable[_P, Coroutine[Any, Any, _R]]:
Expand Down
21 changes: 14 additions & 7 deletions go2rtc_client/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from mashumaro.mixins.dict import DataClassDictMixin
from yarl import URL

from .exceptions import handle_error
from .exceptions import Go2RtcVersionError, handle_error
from .models import ApplicationInfo, Stream, WebRTCSdpAnswer, WebRTCSdpOffer

if TYPE_CHECKING:
Expand Down Expand Up @@ -145,17 +145,24 @@ def __init__(self, websession: ClientSession, server_url: str) -> None:
self.webrtc: Final = _WebRTCClient(self._client)

@handle_error
async def validate_server_version(self) -> bool:
async def validate_server_version(self) -> None:
"""Validate the server version is compatible."""
application_info = await self.application.get_info()
try:
return (
version_supported = (
_MIN_VERSION_SUPPORTED
<= application_info.version
< _MIN_VERSION_UNSUPPORTED
)
except AwesomeVersionException:
_LOGGER.exception(
"Invalid version received from server: %s", application_info.version
except AwesomeVersionException as err:
raise Go2RtcVersionError(
application_info.version if application_info else "unknown",
_MIN_VERSION_SUPPORTED,
_MIN_VERSION_UNSUPPORTED,
) from err
if not version_supported:
raise Go2RtcVersionError(
application_info.version,
_MIN_VERSION_SUPPORTED,
_MIN_VERSION_UNSUPPORTED,
)
return False
24 changes: 15 additions & 9 deletions tests/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

from __future__ import annotations

from contextlib import AbstractContextManager, nullcontext as does_not_raise
import json
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from aiohttp.hdrs import METH_PUT
from awesomeversion import AwesomeVersion
import pytest

from go2rtc_client.exceptions import Go2RtcVersionError
from go2rtc_client.models import WebRTCSdpOffer
from go2rtc_client.rest import _ApplicationClient, _StreamClient, _WebRTCClient
from tests import load_fixture
Expand Down Expand Up @@ -94,22 +96,25 @@ async def test_streams_add(
responses.assert_called_once_with(url, method=METH_PUT, params=params)


VERSION_ERR = "server version '{}' not >= 1.9.5 and < 2.0.0"


@pytest.mark.parametrize(
("server_version", "expected_result"),
[
("0.0.0", False),
("1.9.4", False),
("1.9.5", True),
("1.9.6", True),
("2.0.0", False),
("BLAH", False),
("0.0.0", pytest.raises(Go2RtcVersionError, match=VERSION_ERR.format("0.0.0"))),
("1.9.4", pytest.raises(Go2RtcVersionError, match=VERSION_ERR.format("1.9.4"))),
("1.9.5", does_not_raise()),
("1.9.6", does_not_raise()),
("2.0.0", pytest.raises(Go2RtcVersionError, match=VERSION_ERR.format("2.0.0"))),
("BLAH", pytest.raises(Go2RtcVersionError, match=VERSION_ERR.format("BLAH"))),
],
)
async def test_version_supported(
responses: aioresponses,
rest_client: Go2RtcRestClient,
server_version: str,
expected_result: bool,
expected_result: AbstractContextManager[Any],
) -> None:
"""Test webrtc offer."""
payload = json.loads(load_fixture("application_info_answer.json"))
Expand All @@ -119,7 +124,8 @@ async def test_version_supported(
status=200,
payload=payload,
)
assert await rest_client.validate_server_version() == expected_result
with expected_result:
await rest_client.validate_server_version()


async def test_webrtc_offer(
Expand Down