mirror of
https://github.com/gogcom/galaxy-integrations-python-api.git
synced 2026-01-01 03:18:25 -05:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c9b1c8fcae | ||
|
|
a19a6cf11f | ||
|
|
98cff9cfb8 | ||
|
|
2e2aa8c4a0 | ||
|
|
f57e03db2d | ||
|
|
66085e2239 | ||
|
|
4d3c9b78c4 | ||
|
|
392e4c5f68 | ||
|
|
4d6d3b8eb2 | ||
|
|
d5610221a9 |
@@ -2,7 +2,7 @@
|
|||||||
pytest==4.2.0
|
pytest==4.2.0
|
||||||
pytest-asyncio==0.10.0
|
pytest-asyncio==0.10.0
|
||||||
pytest-mock==1.10.3
|
pytest-mock==1.10.3
|
||||||
pytest-mypy==0.3.2
|
pytest-mypy==0.4.1
|
||||||
pytest-flakes==4.0.0
|
pytest-flakes==4.0.0
|
||||||
# because of pip bug https://github.com/pypa/pip/issues/4780
|
# because of pip bug https://github.com/pypa/pip/issues/4780
|
||||||
aiohttp==3.5.4
|
aiohttp==3.5.4
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="galaxy.plugin.api",
|
name="galaxy.plugin.api",
|
||||||
version="0.49",
|
version="0.53",
|
||||||
description="GOG Galaxy Integrations Python API",
|
description="GOG Galaxy Integrations Python API",
|
||||||
author='Galaxy team',
|
author='Galaxy team',
|
||||||
author_email='galaxy@gog.com',
|
author_email='galaxy@gog.com',
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
__path__: str = __import__('pkgutil').extend_path(__path__, __name__)
|
__path__: str = __import__('pkgutil').extend_path(__path__, __name__) # type: ignore
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ class Platform(Enum):
|
|||||||
Playfire = "playfire"
|
Playfire = "playfire"
|
||||||
Oculus = "oculus"
|
Oculus = "oculus"
|
||||||
Test = "test"
|
Test = "test"
|
||||||
|
Rockstar = "rockstar"
|
||||||
|
|
||||||
|
|
||||||
class Feature(Enum):
|
class Feature(Enum):
|
||||||
@@ -110,6 +111,8 @@ class Feature(Enum):
|
|||||||
ImportFriends = "ImportFriends"
|
ImportFriends = "ImportFriends"
|
||||||
ShutdownPlatformClient = "ShutdownPlatformClient"
|
ShutdownPlatformClient = "ShutdownPlatformClient"
|
||||||
LaunchPlatformClient = "LaunchPlatformClient"
|
LaunchPlatformClient = "LaunchPlatformClient"
|
||||||
|
ImportGameLibrarySettings = "ImportGameLibrarySettings"
|
||||||
|
ImportOSCompatibility = "ImportOSCompatibility"
|
||||||
|
|
||||||
|
|
||||||
class LicenseType(Enum):
|
class LicenseType(Enum):
|
||||||
@@ -128,3 +131,12 @@ class LocalGameState(Flag):
|
|||||||
None_ = 0
|
None_ = 0
|
||||||
Installed = 1
|
Installed = 1
|
||||||
Running = 2
|
Running = 2
|
||||||
|
|
||||||
|
|
||||||
|
class OSCompatibility(Flag):
|
||||||
|
"""Possible game OS compatibility.
|
||||||
|
Use "bitwise or" to express multiple OSs compatibility, e.g. ``os=OSCompatibility.Windows|OSCompatibility.MacOS``
|
||||||
|
"""
|
||||||
|
Windows = 0b001
|
||||||
|
MacOS = 0b010
|
||||||
|
Linux = 0b100
|
||||||
|
|||||||
@@ -18,6 +18,17 @@ class JsonRpcError(Exception):
|
|||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return self.code == other.code and self.message == other.message and self.data == other.data
|
return self.code == other.code and self.message == other.message and self.data == other.data
|
||||||
|
|
||||||
|
def json(self):
|
||||||
|
obj = {
|
||||||
|
"code": self.code,
|
||||||
|
"message": self.message
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.data is not None:
|
||||||
|
obj["error"]["data"] = self.data
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
class ParseError(JsonRpcError):
|
class ParseError(JsonRpcError):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(-32700, "Parse error")
|
super().__init__(-32700, "Parse error")
|
||||||
@@ -232,15 +243,9 @@ class Server():
|
|||||||
response = {
|
response = {
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
"id": request_id,
|
"id": request_id,
|
||||||
"error": {
|
"error": error.json()
|
||||||
"code": error.code,
|
|
||||||
"message": error.message
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if error.data is not None:
|
|
||||||
response["error"]["data"] = error.data
|
|
||||||
|
|
||||||
self._send(response)
|
self._send(response)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import sys
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Any, Dict, List, Optional, Set, Union
|
from typing import Any, Dict, List, Optional, Set, Union
|
||||||
|
|
||||||
from galaxy.api.consts import Feature
|
from galaxy.api.consts import Feature, OSCompatibility
|
||||||
from galaxy.api.errors import ImportInProgress, UnknownError
|
from galaxy.api.errors import ImportInProgress, UnknownError
|
||||||
from galaxy.api.jsonrpc import ApplicationError, NotificationClient, Server
|
from galaxy.api.jsonrpc import ApplicationError, NotificationClient, Server
|
||||||
from galaxy.api.types import Achievement, Authentication, FriendInfo, Game, GameTime, LocalGame, NextStep
|
from galaxy.api.types import Achievement, Authentication, FriendInfo, Game, GameTime, LocalGame, NextStep, GameLibrarySettings
|
||||||
from galaxy.task_manager import TaskManager
|
from galaxy.task_manager import TaskManager
|
||||||
|
|
||||||
|
|
||||||
class JSONEncoder(json.JSONEncoder):
|
class JSONEncoder(json.JSONEncoder):
|
||||||
def default(self, o): # pylint: disable=method-hidden
|
def default(self, o): # pylint: disable=method-hidden
|
||||||
if dataclasses.is_dataclass(o):
|
if dataclasses.is_dataclass(o):
|
||||||
@@ -46,6 +47,8 @@ class Plugin:
|
|||||||
|
|
||||||
self._achievements_import_in_progress = False
|
self._achievements_import_in_progress = False
|
||||||
self._game_times_import_in_progress = False
|
self._game_times_import_in_progress = False
|
||||||
|
self._game_library_settings_import_in_progress = False
|
||||||
|
self._os_compatibility_import_in_progress = False
|
||||||
|
|
||||||
self._persistent_cache = dict()
|
self._persistent_cache = dict()
|
||||||
|
|
||||||
@@ -109,6 +112,12 @@ class Plugin:
|
|||||||
self._register_method("start_game_times_import", self._start_game_times_import)
|
self._register_method("start_game_times_import", self._start_game_times_import)
|
||||||
self._detect_feature(Feature.ImportGameTime, ["get_game_time"])
|
self._detect_feature(Feature.ImportGameTime, ["get_game_time"])
|
||||||
|
|
||||||
|
self._register_method("start_game_library_settings_import", self._start_game_library_settings_import)
|
||||||
|
self._detect_feature(Feature.ImportGameLibrarySettings, ["get_game_library_settings"])
|
||||||
|
|
||||||
|
self._register_method("start_os_compatibility_import", self._start_os_compatibility_import)
|
||||||
|
self._detect_feature(Feature.ImportOSCompatibility, ["get_os_compatibility"])
|
||||||
|
|
||||||
async def __aenter__(self):
|
async def __aenter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@@ -169,12 +178,12 @@ class Plugin:
|
|||||||
def _wrap_external_method(self, handler, name: str):
|
def _wrap_external_method(self, handler, name: str):
|
||||||
async def wrapper(*args, **kwargs):
|
async def wrapper(*args, **kwargs):
|
||||||
return await self._external_task_manager.create_task(handler(*args, **kwargs), name, False)
|
return await self._external_task_manager.create_task(handler(*args, **kwargs), name, False)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
"""Plugin's main coroutine."""
|
"""Plugin's main coroutine."""
|
||||||
await self._server.run()
|
await self._server.run()
|
||||||
await self._external_task_manager.wait()
|
|
||||||
|
|
||||||
def close(self) -> None:
|
def close(self) -> None:
|
||||||
if not self._active:
|
if not self._active:
|
||||||
@@ -332,10 +341,7 @@ class Plugin:
|
|||||||
def _game_achievements_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
def _game_achievements_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
||||||
params = {
|
params = {
|
||||||
"game_id": game_id,
|
"game_id": game_id,
|
||||||
"error": {
|
"error": error.json()
|
||||||
"code": error.code,
|
|
||||||
"message": error.message
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
self._notification_client.notify("game_achievements_import_failure", params)
|
self._notification_client.notify("game_achievements_import_failure", params)
|
||||||
|
|
||||||
@@ -399,16 +405,48 @@ class Plugin:
|
|||||||
def _game_time_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
def _game_time_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
||||||
params = {
|
params = {
|
||||||
"game_id": game_id,
|
"game_id": game_id,
|
||||||
"error": {
|
"error": error.json()
|
||||||
"code": error.code,
|
|
||||||
"message": error.message
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
self._notification_client.notify("game_time_import_failure", params)
|
self._notification_client.notify("game_time_import_failure", params)
|
||||||
|
|
||||||
def _game_times_import_finished(self) -> None:
|
def _game_times_import_finished(self) -> None:
|
||||||
self._notification_client.notify("game_times_import_finished", None)
|
self._notification_client.notify("game_times_import_finished", None)
|
||||||
|
|
||||||
|
def _game_library_settings_import_success(self, game_library_settings: GameLibrarySettings) -> None:
|
||||||
|
params = {"game_library_settings": game_library_settings}
|
||||||
|
self._notification_client.notify("game_library_settings_import_success", params)
|
||||||
|
|
||||||
|
def _game_library_settings_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
||||||
|
params = {
|
||||||
|
"game_id": game_id,
|
||||||
|
"error": error.json()
|
||||||
|
}
|
||||||
|
self._notification_client.notify("game_library_settings_import_failure", params)
|
||||||
|
|
||||||
|
def _game_library_settings_import_finished(self) -> None:
|
||||||
|
self._notification_client.notify("game_library_settings_import_finished", None)
|
||||||
|
|
||||||
|
def _os_compatibility_import_success(self, game_id: str, os_compatibility: Optional[OSCompatibility]) -> None:
|
||||||
|
self._notification_client.notify(
|
||||||
|
"os_compatibility_import_success",
|
||||||
|
{
|
||||||
|
"game_id": game_id,
|
||||||
|
"os_compatibility": os_compatibility
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def _os_compatibility_import_failure(self, game_id: str, error: ApplicationError) -> None:
|
||||||
|
self._notification_client.notify(
|
||||||
|
"os_compatibility_import_failure",
|
||||||
|
{
|
||||||
|
"game_id": game_id,
|
||||||
|
"error": error.json()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
def _os_compatibility_import_finished(self) -> None:
|
||||||
|
self._notification_client.notify("os_compatibility_import_finished", None)
|
||||||
|
|
||||||
def lost_authentication(self) -> None:
|
def lost_authentication(self) -> None:
|
||||||
"""Notify the client that integration has lost authentication for the
|
"""Notify the client that integration has lost authentication for the
|
||||||
current user and is unable to perform actions which would require it.
|
current user and is unable to perform actions which would require it.
|
||||||
@@ -757,6 +795,120 @@ class Plugin:
|
|||||||
(like updating cache).
|
(like updating cache).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
async def _start_game_library_settings_import(self, game_ids: List[str]) -> None:
|
||||||
|
if self._game_library_settings_import_in_progress:
|
||||||
|
raise ImportInProgress()
|
||||||
|
|
||||||
|
context = await self.prepare_game_library_settings_context(game_ids)
|
||||||
|
|
||||||
|
async def import_game_library_settings(game_id, context_):
|
||||||
|
try:
|
||||||
|
game_library_settings = await self.get_game_library_settings(game_id, context_)
|
||||||
|
self._game_library_settings_import_success(game_library_settings)
|
||||||
|
except ApplicationError as error:
|
||||||
|
self._game_library_settings_import_failure(game_id, error)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Unexpected exception raised in import_game_library_settings")
|
||||||
|
self._game_library_settings_import_failure(game_id, UnknownError())
|
||||||
|
|
||||||
|
async def import_game_library_settings_set(game_ids_, context_):
|
||||||
|
try:
|
||||||
|
imports = [import_game_library_settings(game_id, context_) for game_id in game_ids_]
|
||||||
|
await asyncio.gather(*imports)
|
||||||
|
finally:
|
||||||
|
self._game_library_settings_import_finished()
|
||||||
|
self._game_library_settings_import_in_progress = False
|
||||||
|
self.game_library_settings_import_complete()
|
||||||
|
|
||||||
|
self._external_task_manager.create_task(
|
||||||
|
import_game_library_settings_set(game_ids, context),
|
||||||
|
"game library settings import",
|
||||||
|
handle_exceptions=False
|
||||||
|
)
|
||||||
|
self._game_library_settings_import_in_progress = True
|
||||||
|
|
||||||
|
async def prepare_game_library_settings_context(self, game_ids: List[str]) -> Any:
|
||||||
|
"""Override this method to prepare context for get_game_library_settings.
|
||||||
|
This allows for optimizations like batch requests to platform API.
|
||||||
|
Default implementation returns None.
|
||||||
|
|
||||||
|
:param game_ids: the ids of the games for which game library settings are imported
|
||||||
|
:return: context
|
||||||
|
"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def get_game_library_settings(self, game_id: str, context: Any) -> GameLibrarySettings:
|
||||||
|
"""Override this method to return the game library settings for the game
|
||||||
|
identified by the provided game_id.
|
||||||
|
This method is called by import task initialized by GOG Galaxy Client.
|
||||||
|
|
||||||
|
:param game_id: the id of the game for which the game library settings are imported
|
||||||
|
:param context: the value returned from :meth:`prepare_game_library_settings_context`
|
||||||
|
:return: GameLibrarySettings object
|
||||||
|
"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def game_library_settings_import_complete(self) -> None:
|
||||||
|
"""Override this method to handle operations after game library settings import is finished
|
||||||
|
(like updating cache).
|
||||||
|
"""
|
||||||
|
|
||||||
|
async def _start_os_compatibility_import(self, game_ids: List[str]) -> None:
|
||||||
|
if self._os_compatibility_import_in_progress:
|
||||||
|
raise ImportInProgress()
|
||||||
|
|
||||||
|
context = await self.prepare_os_compatibility_context(game_ids)
|
||||||
|
|
||||||
|
async def import_os_compatibility(game_id, context_):
|
||||||
|
try:
|
||||||
|
os_compatibility = await self.get_os_compatibility(game_id, context_)
|
||||||
|
self._os_compatibility_import_success(game_id, os_compatibility)
|
||||||
|
except ApplicationError as error:
|
||||||
|
self._os_compatibility_import_failure(game_id, error)
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Unexpected exception raised in import_os_compatibility")
|
||||||
|
self._os_compatibility_import_failure(game_id, UnknownError())
|
||||||
|
|
||||||
|
async def import_os_compatibility_set(game_ids_, context_):
|
||||||
|
try:
|
||||||
|
await asyncio.gather(*[
|
||||||
|
import_os_compatibility(game_id, context_) for game_id in game_ids_
|
||||||
|
])
|
||||||
|
finally:
|
||||||
|
self._os_compatibility_import_finished()
|
||||||
|
self._os_compatibility_import_in_progress = False
|
||||||
|
self.os_compatibility_import_complete()
|
||||||
|
|
||||||
|
self._external_task_manager.create_task(
|
||||||
|
import_os_compatibility_set(game_ids, context),
|
||||||
|
"game OS compatibility import",
|
||||||
|
handle_exceptions=False
|
||||||
|
)
|
||||||
|
self._os_compatibility_import_in_progress = True
|
||||||
|
|
||||||
|
async def prepare_os_compatibility_context(self, game_ids: List[str]) -> Any:
|
||||||
|
"""Override this method to prepare context for get_os_compatibility.
|
||||||
|
This allows for optimizations like batch requests to platform API.
|
||||||
|
Default implementation returns None.
|
||||||
|
|
||||||
|
:param game_ids: the ids of the games for which game os compatibility is imported
|
||||||
|
:return: context
|
||||||
|
"""
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def get_os_compatibility(self, game_id: str, context: Any) -> Optional[OSCompatibility]:
|
||||||
|
"""Override this method to return the OS compatibility for the game with the provided game_id.
|
||||||
|
This method is called by import task initialized by GOG Galaxy Client.
|
||||||
|
|
||||||
|
:param game_id: the id of the game for which the game os compatibility is imported
|
||||||
|
:param context: the value returned from :meth:`prepare_os_compatibility_context`
|
||||||
|
:return: OSCompatibility flags indicating compatible OSs, or None if compatibility is not know
|
||||||
|
"""
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def os_compatibility_import_complete(self) -> None:
|
||||||
|
"""Override this method to handle operations after OS compatibility import is finished (like updating cache)."""
|
||||||
|
|
||||||
|
|
||||||
def create_and_run_plugin(plugin_class, argv):
|
def create_and_run_plugin(plugin_class, argv):
|
||||||
"""Call this method as an entry point for the implemented integration.
|
"""Call this method as an entry point for the implemented integration.
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List, Dict, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from galaxy.api.consts import LicenseType, LocalGameState
|
from galaxy.api.consts import LicenseType, LocalGameState
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Authentication():
|
class Authentication:
|
||||||
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials`
|
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials`
|
||||||
to inform the client that authentication has successfully finished.
|
to inform the client that authentication has successfully finished.
|
||||||
|
|
||||||
@@ -14,8 +15,9 @@ class Authentication():
|
|||||||
user_id: str
|
user_id: str
|
||||||
user_name: str
|
user_name: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Cookie():
|
class Cookie:
|
||||||
"""Cookie
|
"""Cookie
|
||||||
|
|
||||||
:param name: name of the cookie
|
:param name: name of the cookie
|
||||||
@@ -28,8 +30,9 @@ class Cookie():
|
|||||||
domain: Optional[str] = None
|
domain: Optional[str] = None
|
||||||
path: Optional[str] = None
|
path: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class NextStep():
|
class NextStep:
|
||||||
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials` to open client built-in browser with given url.
|
"""Return this from :meth:`.authenticate` or :meth:`.pass_login_credentials` to open client built-in browser with given url.
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
@@ -67,8 +70,9 @@ class NextStep():
|
|||||||
cookies: Optional[List[Cookie]] = None
|
cookies: Optional[List[Cookie]] = None
|
||||||
js: Optional[Dict[str, List[str]]] = None
|
js: Optional[Dict[str, List[str]]] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class LicenseInfo():
|
class LicenseInfo:
|
||||||
"""Information about the license of related product.
|
"""Information about the license of related product.
|
||||||
|
|
||||||
:param license_type: type of license
|
:param license_type: type of license
|
||||||
@@ -77,8 +81,9 @@ class LicenseInfo():
|
|||||||
license_type: LicenseType
|
license_type: LicenseType
|
||||||
owner: Optional[str] = None
|
owner: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Dlc():
|
class Dlc:
|
||||||
"""Downloadable content object.
|
"""Downloadable content object.
|
||||||
|
|
||||||
:param dlc_id: id of the dlc
|
:param dlc_id: id of the dlc
|
||||||
@@ -89,8 +94,9 @@ class Dlc():
|
|||||||
dlc_title: str
|
dlc_title: str
|
||||||
license_info: LicenseInfo
|
license_info: LicenseInfo
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Game():
|
class Game:
|
||||||
"""Game object.
|
"""Game object.
|
||||||
|
|
||||||
:param game_id: unique identifier of the game, this will be passed as parameter for methods such as launch_game
|
:param game_id: unique identifier of the game, this will be passed as parameter for methods such as launch_game
|
||||||
@@ -103,8 +109,9 @@ class Game():
|
|||||||
dlcs: Optional[List[Dlc]]
|
dlcs: Optional[List[Dlc]]
|
||||||
license_info: LicenseInfo
|
license_info: LicenseInfo
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Achievement():
|
class Achievement:
|
||||||
"""Achievement, has to be initialized with either id or name.
|
"""Achievement, has to be initialized with either id or name.
|
||||||
|
|
||||||
:param unlock_time: unlock time of the achievement
|
:param unlock_time: unlock time of the achievement
|
||||||
@@ -119,8 +126,9 @@ class Achievement():
|
|||||||
assert self.achievement_id or self.achievement_name, \
|
assert self.achievement_id or self.achievement_name, \
|
||||||
"One of achievement_id or achievement_name is required"
|
"One of achievement_id or achievement_name is required"
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class LocalGame():
|
class LocalGame:
|
||||||
"""Game locally present on the authenticated user's computer.
|
"""Game locally present on the authenticated user's computer.
|
||||||
|
|
||||||
:param game_id: id of the game
|
:param game_id: id of the game
|
||||||
@@ -129,8 +137,9 @@ class LocalGame():
|
|||||||
game_id: str
|
game_id: str
|
||||||
local_game_state: LocalGameState
|
local_game_state: LocalGameState
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class FriendInfo():
|
class FriendInfo:
|
||||||
"""Information about a friend of the currently authenticated user.
|
"""Information about a friend of the currently authenticated user.
|
||||||
|
|
||||||
:param user_id: id of the user
|
:param user_id: id of the user
|
||||||
@@ -139,8 +148,9 @@ class FriendInfo():
|
|||||||
user_id: str
|
user_id: str
|
||||||
user_name: str
|
user_name: str
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class GameTime():
|
class GameTime:
|
||||||
"""Game time of a game, defines the total time spent in the game
|
"""Game time of a game, defines the total time spent in the game
|
||||||
and the last time the game was played.
|
and the last time the game was played.
|
||||||
|
|
||||||
@@ -151,3 +161,16 @@ class GameTime():
|
|||||||
game_id: str
|
game_id: str
|
||||||
time_played: Optional[int]
|
time_played: Optional[int]
|
||||||
last_played_time: Optional[int]
|
last_played_time: Optional[int]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class GameLibrarySettings:
|
||||||
|
"""Library settings of a game, defines assigned tags and visibility flag.
|
||||||
|
|
||||||
|
:param game_id: id of the related game
|
||||||
|
:param tags: collection of tags assigned to the game
|
||||||
|
:param hidden: indicates if the game should be hidden in GOG Galaxy application
|
||||||
|
"""
|
||||||
|
game_id: str
|
||||||
|
tags: Optional[List[str]]
|
||||||
|
hidden: Optional[bool]
|
||||||
|
|||||||
98
src/galaxy/registry_monitor.py
Normal file
98
src/galaxy/registry_monitor.py
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
import sys
|
||||||
|
if sys.platform == "win32":
|
||||||
|
import logging
|
||||||
|
import ctypes
|
||||||
|
from ctypes.wintypes import LONG, HKEY, LPCWSTR, DWORD, BOOL, HANDLE, LPVOID
|
||||||
|
|
||||||
|
LPSECURITY_ATTRIBUTES = LPVOID
|
||||||
|
|
||||||
|
RegOpenKeyEx = ctypes.windll.advapi32.RegOpenKeyExW
|
||||||
|
RegOpenKeyEx.restype = LONG
|
||||||
|
RegOpenKeyEx.argtypes = [HKEY, LPCWSTR, DWORD, DWORD, ctypes.POINTER(HKEY)]
|
||||||
|
|
||||||
|
RegCloseKey = ctypes.windll.advapi32.RegCloseKey
|
||||||
|
RegCloseKey.restype = LONG
|
||||||
|
RegCloseKey.argtypes = [HKEY]
|
||||||
|
|
||||||
|
RegNotifyChangeKeyValue = ctypes.windll.advapi32.RegNotifyChangeKeyValue
|
||||||
|
RegNotifyChangeKeyValue.restype = LONG
|
||||||
|
RegNotifyChangeKeyValue.argtypes = [HKEY, BOOL, DWORD, HANDLE, BOOL]
|
||||||
|
|
||||||
|
CloseHandle = ctypes.windll.kernel32.CloseHandle
|
||||||
|
CloseHandle.restype = BOOL
|
||||||
|
CloseHandle.argtypes = [HANDLE]
|
||||||
|
|
||||||
|
CreateEvent = ctypes.windll.kernel32.CreateEventW
|
||||||
|
CreateEvent.restype = BOOL
|
||||||
|
CreateEvent.argtypes = [LPSECURITY_ATTRIBUTES, BOOL, BOOL, LPCWSTR]
|
||||||
|
|
||||||
|
WaitForSingleObject = ctypes.windll.kernel32.WaitForSingleObject
|
||||||
|
WaitForSingleObject.restype = DWORD
|
||||||
|
WaitForSingleObject.argtypes = [HANDLE, DWORD]
|
||||||
|
|
||||||
|
ERROR_SUCCESS = 0x00000000
|
||||||
|
|
||||||
|
KEY_READ = 0x00020019
|
||||||
|
KEY_QUERY_VALUE = 0x00000001
|
||||||
|
|
||||||
|
REG_NOTIFY_CHANGE_NAME = 0x00000001
|
||||||
|
REG_NOTIFY_CHANGE_LAST_SET = 0x00000004
|
||||||
|
|
||||||
|
WAIT_OBJECT_0 = 0x00000000
|
||||||
|
WAIT_TIMEOUT = 0x00000102
|
||||||
|
|
||||||
|
class RegistryMonitor:
|
||||||
|
|
||||||
|
def __init__(self, root, subkey):
|
||||||
|
self._root = root
|
||||||
|
self._subkey = subkey
|
||||||
|
self._event = CreateEvent(None, False, False, None)
|
||||||
|
|
||||||
|
self._key = None
|
||||||
|
self._open_key()
|
||||||
|
if self._key:
|
||||||
|
self._set_key_update_notification()
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
CloseHandle(self._event)
|
||||||
|
if self._key:
|
||||||
|
RegCloseKey(self._key)
|
||||||
|
self._key = None
|
||||||
|
|
||||||
|
def is_updated(self):
|
||||||
|
wait_result = WaitForSingleObject(self._event, 0)
|
||||||
|
|
||||||
|
# previously watched
|
||||||
|
if wait_result == WAIT_OBJECT_0:
|
||||||
|
self._set_key_update_notification()
|
||||||
|
return True
|
||||||
|
|
||||||
|
# no changes or no key before
|
||||||
|
if wait_result != WAIT_TIMEOUT:
|
||||||
|
# unexpected error
|
||||||
|
logging.warning("Unexpected WaitForSingleObject result %s", wait_result)
|
||||||
|
return False
|
||||||
|
|
||||||
|
if self._key is None:
|
||||||
|
self._open_key()
|
||||||
|
|
||||||
|
if self._key is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
self._set_key_update_notification()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _set_key_update_notification(self):
|
||||||
|
filter_ = REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET
|
||||||
|
status = RegNotifyChangeKeyValue(self._key, True, filter_, self._event, True)
|
||||||
|
if status != ERROR_SUCCESS:
|
||||||
|
# key was deleted
|
||||||
|
RegCloseKey(self._key)
|
||||||
|
self._key = None
|
||||||
|
|
||||||
|
def _open_key(self):
|
||||||
|
access = KEY_QUERY_VALUE | KEY_READ
|
||||||
|
self._key = HKEY()
|
||||||
|
rc = RegOpenKeyEx(self._root, self._subkey, 0, access, ctypes.byref(self._key))
|
||||||
|
if rc != ERROR_SUCCESS:
|
||||||
|
self._key = None
|
||||||
@@ -49,7 +49,13 @@ async def plugin(reader, writer):
|
|||||||
"game_times_import_complete",
|
"game_times_import_complete",
|
||||||
"shutdown_platform_client",
|
"shutdown_platform_client",
|
||||||
"shutdown",
|
"shutdown",
|
||||||
"tick"
|
"tick",
|
||||||
|
"get_game_library_settings",
|
||||||
|
"prepare_game_library_settings_context",
|
||||||
|
"game_library_settings_import_complete",
|
||||||
|
"get_os_compatibility",
|
||||||
|
"prepare_os_compatibility_context",
|
||||||
|
"os_compatibility_import_complete",
|
||||||
)
|
)
|
||||||
|
|
||||||
with ExitStack() as stack:
|
with ExitStack() as stack:
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ async def test_prepare_get_unlocked_achievements_context_error(plugin, read, wri
|
|||||||
"game_ids": ["14"]
|
"game_ids": ["14"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ async def test_import_in_progress(plugin, read, write):
|
|||||||
read.side_effect = [
|
read.side_effect = [
|
||||||
async_return_value(create_message(requests[0])),
|
async_return_value(create_message(requests[0])),
|
||||||
async_return_value(create_message(requests[1])),
|
async_return_value(create_message(requests[1])),
|
||||||
async_return_value(b"")
|
async_return_value(b"", 10)
|
||||||
]
|
]
|
||||||
|
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ async def test_success(plugin, read, write):
|
|||||||
"id": "3",
|
"id": "3",
|
||||||
"method": "init_authentication"
|
"method": "init_authentication"
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.authenticate.return_value = async_return_value(Authentication("132", "Zenek"))
|
plugin.authenticate.return_value = async_return_value(Authentication("132", "Zenek"))
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.authenticate.assert_called_with()
|
plugin.authenticate.assert_called_with()
|
||||||
@@ -55,7 +55,7 @@ async def test_failure(plugin, read, write, error, code, message):
|
|||||||
"method": "init_authentication"
|
"method": "init_authentication"
|
||||||
}
|
}
|
||||||
|
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.authenticate.side_effect = error()
|
plugin.authenticate.side_effect = error()
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.authenticate.assert_called_with()
|
plugin.authenticate.assert_called_with()
|
||||||
@@ -84,7 +84,7 @@ async def test_stored_credentials(plugin, read, write):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.authenticate.return_value = async_return_value(Authentication("132", "Zenek"))
|
plugin.authenticate.return_value = async_return_value(Authentication("132", "Zenek"))
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.authenticate.assert_called_with(stored_credentials={"token": "ABC"})
|
plugin.authenticate.assert_called_with(stored_credentials={"token": "ABC"})
|
||||||
|
|||||||
@@ -14,7 +14,9 @@ def test_base_class():
|
|||||||
Feature.ImportGameTime,
|
Feature.ImportGameTime,
|
||||||
Feature.ImportFriends,
|
Feature.ImportFriends,
|
||||||
Feature.ShutdownPlatformClient,
|
Feature.ShutdownPlatformClient,
|
||||||
Feature.LaunchPlatformClient
|
Feature.LaunchPlatformClient,
|
||||||
|
Feature.ImportGameLibrarySettings,
|
||||||
|
Feature.ImportOSCompatibility
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ async def test_get_friends_success(plugin, read, write):
|
|||||||
"method": "import_friends"
|
"method": "import_friends"
|
||||||
}
|
}
|
||||||
|
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.get_friends.return_value = async_return_value([
|
plugin.get_friends.return_value = async_return_value([
|
||||||
FriendInfo("3", "Jan"),
|
FriendInfo("3", "Jan"),
|
||||||
FriendInfo("5", "Ola")
|
FriendInfo("5", "Ola")
|
||||||
@@ -45,7 +45,7 @@ async def test_get_friends_failure(plugin, read, write):
|
|||||||
"method": "import_friends"
|
"method": "import_friends"
|
||||||
}
|
}
|
||||||
|
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.get_friends.side_effect = UnknownError()
|
plugin.get_friends.side_effect = UnknownError()
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.get_friends.assert_called_with()
|
plugin.get_friends.assert_called_with()
|
||||||
|
|||||||
196
tests/test_game_library_settings.py
Normal file
196
tests/test_game_library_settings.py
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
from unittest.mock import call
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from galaxy.api.types import GameLibrarySettings
|
||||||
|
from galaxy.api.errors import BackendError
|
||||||
|
from galaxy.unittest.mock import async_return_value
|
||||||
|
|
||||||
|
from tests import create_message, get_messages
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_library_settings_success(plugin, read, write):
|
||||||
|
plugin.prepare_game_library_settings_context.return_value = async_return_value("abc")
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"method": "start_game_library_settings_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["3", "5", "7"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
plugin.get_game_library_settings.side_effect = [
|
||||||
|
async_return_value(GameLibrarySettings("3", None, True)),
|
||||||
|
async_return_value(GameLibrarySettings("5", [], False)),
|
||||||
|
async_return_value(GameLibrarySettings("7", ["tag1", "tag2", "tag3"], None)),
|
||||||
|
]
|
||||||
|
await plugin.run()
|
||||||
|
plugin.get_game_library_settings.assert_has_calls([
|
||||||
|
call("3", "abc"),
|
||||||
|
call("5", "abc"),
|
||||||
|
call("7", "abc"),
|
||||||
|
])
|
||||||
|
plugin.game_library_settings_import_complete.assert_called_once_with()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"result": None
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_library_settings": {
|
||||||
|
"game_id": "3",
|
||||||
|
"hidden": True
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_library_settings": {
|
||||||
|
"game_id": "5",
|
||||||
|
"tags": [],
|
||||||
|
"hidden": False
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_library_settings": {
|
||||||
|
"game_id": "7",
|
||||||
|
"tags": ["tag1", "tag2", "tag3"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_finished",
|
||||||
|
"params": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("exception,code,message", [
|
||||||
|
(BackendError, 4, "Backend error"),
|
||||||
|
(KeyError, 0, "Unknown error")
|
||||||
|
])
|
||||||
|
async def test_get_game_library_settings_error(exception, code, message, plugin, read, write):
|
||||||
|
plugin.prepare_game_library_settings_context.return_value = async_return_value(None)
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"method": "start_game_library_settings_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["6"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
plugin.get_game_library_settings.side_effect = exception
|
||||||
|
await plugin.run()
|
||||||
|
plugin.get_game_library_settings.assert_called()
|
||||||
|
plugin.game_library_settings_import_complete.assert_called_once_with()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"result": None
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_failure",
|
||||||
|
"params": {
|
||||||
|
"game_id": "6",
|
||||||
|
"error": {
|
||||||
|
"code": code,
|
||||||
|
"message": message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "game_library_settings_import_finished",
|
||||||
|
"params": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_prepare_get_game_library_settings_context_error(plugin, read, write):
|
||||||
|
plugin.prepare_game_library_settings_context.side_effect = BackendError()
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"method": "start_game_library_settings_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["6"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
await plugin.run()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"error": {
|
||||||
|
"code": 4,
|
||||||
|
"message": "Backend error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_import_in_progress(plugin, read, write):
|
||||||
|
plugin.prepare_game_library_settings_context.return_value = async_return_value(None)
|
||||||
|
requests = [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"method": "start_game_library_settings_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["6"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "4",
|
||||||
|
"method": "start_game_library_settings_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["7"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
read.side_effect = [
|
||||||
|
async_return_value(create_message(requests[0])),
|
||||||
|
async_return_value(create_message(requests[1])),
|
||||||
|
async_return_value(b"", 10)
|
||||||
|
]
|
||||||
|
|
||||||
|
await plugin.run()
|
||||||
|
|
||||||
|
messages = get_messages(write)
|
||||||
|
assert {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"result": None
|
||||||
|
} in messages
|
||||||
|
assert {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "4",
|
||||||
|
"error": {
|
||||||
|
"code": 600,
|
||||||
|
"message": "Import already in progress"
|
||||||
|
}
|
||||||
|
} in messages
|
||||||
|
|
||||||
@@ -135,7 +135,7 @@ async def test_prepare_get_game_time_context_error(plugin, read, write):
|
|||||||
"game_ids": ["6"]
|
"game_ids": ["6"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
|
|
||||||
assert get_messages(write) == [
|
assert get_messages(write) == [
|
||||||
@@ -174,7 +174,7 @@ async def test_import_in_progress(plugin, read, write):
|
|||||||
read.side_effect = [
|
read.side_effect = [
|
||||||
async_return_value(create_message(requests[0])),
|
async_return_value(create_message(requests[0])),
|
||||||
async_return_value(create_message(requests[1])),
|
async_return_value(create_message(requests[1])),
|
||||||
async_return_value(b"")
|
async_return_value(b"", 10)
|
||||||
]
|
]
|
||||||
|
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ async def test_success(plugin, read, write):
|
|||||||
"id": "3",
|
"id": "3",
|
||||||
"method": "import_local_games"
|
"method": "import_local_games"
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
|
||||||
plugin.get_local_games.return_value = async_return_value([
|
plugin.get_local_games.return_value = async_return_value([
|
||||||
LocalGame("1", LocalGameState.Running),
|
LocalGame("1", LocalGameState.Running),
|
||||||
@@ -63,7 +63,7 @@ async def test_failure(plugin, read, write, error, code, message):
|
|||||||
"id": "3",
|
"id": "3",
|
||||||
"method": "import_local_games"
|
"method": "import_local_games"
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.get_local_games.side_effect = error()
|
plugin.get_local_games.side_effect = error()
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.get_local_games.assert_called_with()
|
plugin.get_local_games.assert_called_with()
|
||||||
|
|||||||
187
tests/test_os_compatibility.py
Normal file
187
tests/test_os_compatibility.py
Normal file
@@ -0,0 +1,187 @@
|
|||||||
|
from unittest.mock import call
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from galaxy.api.consts import OSCompatibility
|
||||||
|
from galaxy.api.errors import BackendError
|
||||||
|
from galaxy.unittest.mock import async_return_value
|
||||||
|
|
||||||
|
from tests import create_message, get_messages
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_os_compatibility_success(plugin, read, write):
|
||||||
|
context = "abc"
|
||||||
|
plugin.prepare_os_compatibility_context.return_value = async_return_value(context)
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "11",
|
||||||
|
"method": "start_os_compatibility_import",
|
||||||
|
"params": {"game_ids": ["666", "13", "42"]}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
plugin.get_os_compatibility.side_effect = [
|
||||||
|
async_return_value(OSCompatibility.Linux),
|
||||||
|
async_return_value(None),
|
||||||
|
async_return_value(OSCompatibility.Windows | OSCompatibility.MacOS),
|
||||||
|
]
|
||||||
|
await plugin.run()
|
||||||
|
plugin.get_os_compatibility.assert_has_calls([
|
||||||
|
call("666", context),
|
||||||
|
call("13", context),
|
||||||
|
call("42", context),
|
||||||
|
])
|
||||||
|
plugin.os_compatibility_import_complete.assert_called_once_with()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "11",
|
||||||
|
"result": None
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_id": "666",
|
||||||
|
"os_compatibility": OSCompatibility.Linux.value
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_id": "13",
|
||||||
|
"os_compatibility": None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_success",
|
||||||
|
"params": {
|
||||||
|
"game_id": "42",
|
||||||
|
"os_compatibility": (OSCompatibility.Windows | OSCompatibility.MacOS).value
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_finished",
|
||||||
|
"params": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("exception,code,message", [
|
||||||
|
(BackendError, 4, "Backend error"),
|
||||||
|
(KeyError, 0, "Unknown error")
|
||||||
|
])
|
||||||
|
async def test_get_os_compatibility_error(exception, code, message, plugin, read, write):
|
||||||
|
game_id = "6"
|
||||||
|
request_id = "55"
|
||||||
|
plugin.prepare_os_compatibility_context.return_value = async_return_value(None)
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": request_id,
|
||||||
|
"method": "start_os_compatibility_import",
|
||||||
|
"params": {"game_ids": [game_id]}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
plugin.get_os_compatibility.side_effect = exception
|
||||||
|
await plugin.run()
|
||||||
|
plugin.get_os_compatibility.assert_called()
|
||||||
|
plugin.os_compatibility_import_complete.assert_called_once_with()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": request_id,
|
||||||
|
"result": None
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_failure",
|
||||||
|
"params": {
|
||||||
|
"game_id": game_id,
|
||||||
|
"error": {
|
||||||
|
"code": code,
|
||||||
|
"message": message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"method": "os_compatibility_import_finished",
|
||||||
|
"params": None
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_prepare_get_os_compatibility_context_error(plugin, read, write):
|
||||||
|
request_id = "31415"
|
||||||
|
plugin.prepare_os_compatibility_context.side_effect = BackendError()
|
||||||
|
request = {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": request_id,
|
||||||
|
"method": "start_os_compatibility_import",
|
||||||
|
"params": {"game_ids": ["6"]}
|
||||||
|
}
|
||||||
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
await plugin.run()
|
||||||
|
|
||||||
|
assert get_messages(write) == [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": request_id,
|
||||||
|
"error": {
|
||||||
|
"code": 4,
|
||||||
|
"message": "Backend error"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_import_already_in_progress_error(plugin, read, write):
|
||||||
|
plugin.prepare_os_compatibility_context.return_value = async_return_value(None)
|
||||||
|
requests = [
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"method": "start_os_compatibility_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["42"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "4",
|
||||||
|
"method": "start_os_compatibility_import",
|
||||||
|
"params": {
|
||||||
|
"game_ids": ["666"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
read.side_effect = [
|
||||||
|
async_return_value(create_message(requests[0])),
|
||||||
|
async_return_value(create_message(requests[1])),
|
||||||
|
async_return_value(b"", 10)
|
||||||
|
]
|
||||||
|
|
||||||
|
await plugin.run()
|
||||||
|
|
||||||
|
responses = get_messages(write)
|
||||||
|
assert {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "3",
|
||||||
|
"result": None
|
||||||
|
} in responses
|
||||||
|
assert {
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": "4",
|
||||||
|
"error": {
|
||||||
|
"code": 600,
|
||||||
|
"message": "Import already in progress"
|
||||||
|
}
|
||||||
|
} in responses
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ async def test_success(plugin, read, write):
|
|||||||
"id": "3",
|
"id": "3",
|
||||||
"method": "import_owned_games"
|
"method": "import_owned_games"
|
||||||
}
|
}
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
|
|
||||||
plugin.get_owned_games.return_value = async_return_value([
|
plugin.get_owned_games.return_value = async_return_value([
|
||||||
Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None)),
|
Game("3", "Doom", None, LicenseInfo(LicenseType.SinglePurchase, None)),
|
||||||
@@ -80,7 +80,7 @@ async def test_failure(plugin, read, write):
|
|||||||
"method": "import_owned_games"
|
"method": "import_owned_games"
|
||||||
}
|
}
|
||||||
|
|
||||||
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"")]
|
read.side_effect = [async_return_value(create_message(request)), async_return_value(b"", 10)]
|
||||||
plugin.get_owned_games.side_effect = UnknownError()
|
plugin.get_owned_games.side_effect = UnknownError()
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
plugin.get_owned_games.assert_called_with()
|
plugin.get_owned_games.assert_called_with()
|
||||||
|
|||||||
Reference in New Issue
Block a user