mirror of
https://github.com/gogcom/galaxy-integrations-python-api.git
synced 2026-01-01 03:18:25 -05:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23ef34bed5 | ||
|
|
a4b08f8105 | ||
|
|
4d62b8ccb8 | ||
|
|
d759b4aa85 | ||
|
|
9b33397827 | ||
|
|
e09e443064 | ||
|
|
00ed52384a | ||
|
|
958d9bc0e6 | ||
|
|
d73d048ff7 | ||
|
|
e06e40f845 | ||
|
|
833e6999d7 | ||
|
|
ca778e2cdb | ||
|
|
9a06428fc0 | ||
|
|
f9eaeaf726 | ||
|
|
f09171672f | ||
|
|
ca8d0dfaf4 | ||
|
|
73bc9aa8ec | ||
|
|
52273e2f8c | ||
|
|
bda867473c | ||
|
|
6885cdc439 | ||
|
|
88e25a93be | ||
|
|
67e7a4c0b2 | ||
|
|
788d2550e6 | ||
|
|
059a1ea343 | ||
|
|
300ade5d43 | ||
|
|
43556a0470 | ||
|
|
e244d3bb44 | ||
|
|
d6e6efc633 |
12
README.md
12
README.md
@@ -30,4 +30,14 @@ pip install -r requirements.txt
|
|||||||
Run tests:
|
Run tests:
|
||||||
```bash
|
```bash
|
||||||
pytest
|
pytest
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
### 0.16
|
||||||
|
* Do not log sensitive data.
|
||||||
|
* Return `LocalGameState` as int (possible combination of flags).
|
||||||
|
### 0.15
|
||||||
|
* `shutdown()` is called on socket disconnection.
|
||||||
|
### 0.14
|
||||||
|
* Added required version parameter to Plugin constructor.
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from enum import Enum
|
from enum import Enum, Flag
|
||||||
|
|
||||||
class Platform(Enum):
|
class Platform(Enum):
|
||||||
Unknown = "unknown"
|
Unknown = "unknown"
|
||||||
@@ -30,10 +30,10 @@ class LicenseType(Enum):
|
|||||||
FreeToPlay = "FreeToPlay"
|
FreeToPlay = "FreeToPlay"
|
||||||
OtherUserLicense = "OtherUserLicense"
|
OtherUserLicense = "OtherUserLicense"
|
||||||
|
|
||||||
class LocalGameState(Enum):
|
class LocalGameState(Flag):
|
||||||
Unknown = "Unknown"
|
None_ = 0
|
||||||
Installed = "Installed"
|
Installed = 1
|
||||||
Running = "Running"
|
Running = 2
|
||||||
|
|
||||||
class PresenceState(Enum):
|
class PresenceState(Enum):
|
||||||
Unknown = "Unknown"
|
Unknown = "Unknown"
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
from galaxy.api.jsonrpc import ApplicationError
|
from galaxy.api.jsonrpc import ApplicationError, UnknownError
|
||||||
|
|
||||||
class UnknownError(ApplicationError):
|
UnknownError = UnknownError
|
||||||
def __init__(self, data=None):
|
|
||||||
super().__init__(0, "Unknown error", data)
|
|
||||||
|
|
||||||
class AuthenticationRequired(ApplicationError):
|
class AuthenticationRequired(ApplicationError):
|
||||||
def __init__(self, data=None):
|
def __init__(self, data=None):
|
||||||
@@ -20,6 +18,10 @@ class BackendError(ApplicationError):
|
|||||||
def __init__(self, data=None):
|
def __init__(self, data=None):
|
||||||
super().__init__(4, "Backend error", data)
|
super().__init__(4, "Backend error", data)
|
||||||
|
|
||||||
|
class UnknownBackendResponse(ApplicationError):
|
||||||
|
def __init__(self, data=None):
|
||||||
|
super().__init__(4, "Backend responded in uknown way", data)
|
||||||
|
|
||||||
class InvalidCredentials(ApplicationError):
|
class InvalidCredentials(ApplicationError):
|
||||||
def __init__(self, data=None):
|
def __init__(self, data=None):
|
||||||
super().__init__(100, "Invalid credentials", data)
|
super().__init__(100, "Invalid credentials", data)
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from collections.abc import Iterable
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ class MethodNotFound(JsonRpcError):
|
|||||||
|
|
||||||
class InvalidParams(JsonRpcError):
|
class InvalidParams(JsonRpcError):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(-32601, "Invalid params")
|
super().__init__(-32602, "Invalid params")
|
||||||
|
|
||||||
class Timeout(JsonRpcError):
|
class Timeout(JsonRpcError):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -40,8 +41,26 @@ class ApplicationError(JsonRpcError):
|
|||||||
raise ValueError("The error code in reserved range")
|
raise ValueError("The error code in reserved range")
|
||||||
super().__init__(code, message, data)
|
super().__init__(code, message, data)
|
||||||
|
|
||||||
|
class UnknownError(ApplicationError):
|
||||||
|
def __init__(self, data=None):
|
||||||
|
super().__init__(0, "Unknown error", data)
|
||||||
|
|
||||||
Request = namedtuple("Request", ["method", "params", "id"], defaults=[{}, None])
|
Request = namedtuple("Request", ["method", "params", "id"], defaults=[{}, None])
|
||||||
Method = namedtuple("Method", ["callback", "internal"])
|
Method = namedtuple("Method", ["callback", "internal", "sensitive_params"])
|
||||||
|
|
||||||
|
def anonymise_sensitive_params(params, sensitive_params):
|
||||||
|
anomized_data = "****"
|
||||||
|
if not sensitive_params:
|
||||||
|
return params
|
||||||
|
|
||||||
|
if isinstance(sensitive_params, Iterable):
|
||||||
|
anomized_params = params.copy()
|
||||||
|
for key in anomized_params.keys():
|
||||||
|
if key in sensitive_params:
|
||||||
|
anomized_params[key] = anomized_data
|
||||||
|
return anomized_params
|
||||||
|
|
||||||
|
return anomized_data
|
||||||
|
|
||||||
class Server():
|
class Server():
|
||||||
def __init__(self, reader, writer, encoder=json.JSONEncoder()):
|
def __init__(self, reader, writer, encoder=json.JSONEncoder()):
|
||||||
@@ -53,11 +72,27 @@ class Server():
|
|||||||
self._notifications = {}
|
self._notifications = {}
|
||||||
self._eof_listeners = []
|
self._eof_listeners = []
|
||||||
|
|
||||||
def register_method(self, name, callback, internal):
|
def register_method(self, name, callback, internal, sensitive_params=False):
|
||||||
self._methods[name] = Method(callback, internal)
|
"""
|
||||||
|
Register method
|
||||||
|
:param name:
|
||||||
|
:param callback:
|
||||||
|
:param internal: if True the callback will be processed immediately (synchronously)
|
||||||
|
:param sensitive_params: list of parameters that will by anonymized before logging; if False - no params
|
||||||
|
are considered sensitive, if True - all params are considered sensitive
|
||||||
|
"""
|
||||||
|
self._methods[name] = Method(callback, internal, sensitive_params)
|
||||||
|
|
||||||
def register_notification(self, name, callback, internal):
|
def register_notification(self, name, callback, internal, sensitive_params=False):
|
||||||
self._notifications[name] = Method(callback, internal)
|
"""
|
||||||
|
Register notification
|
||||||
|
:param name:
|
||||||
|
:param callback:
|
||||||
|
:param internal: if True the callback will be processed immediately (synchronously)
|
||||||
|
:param sensitive_params: list of parameters that will by anonymized before logging; if False - no params
|
||||||
|
are considered sensitive, if True - all params are considered sensitive
|
||||||
|
"""
|
||||||
|
self._notifications[name] = Method(callback, internal, sensitive_params)
|
||||||
|
|
||||||
def register_eof(self, callback):
|
def register_eof(self, callback):
|
||||||
self._eof_listeners.append(callback)
|
self._eof_listeners.append(callback)
|
||||||
@@ -73,7 +108,7 @@ class Server():
|
|||||||
self._eof()
|
self._eof()
|
||||||
continue
|
continue
|
||||||
data = data.strip()
|
data = data.strip()
|
||||||
logging.debug("Received data: %s", data)
|
logging.debug("Received %d bytes of data", len(data))
|
||||||
self._handle_input(data)
|
self._handle_input(data)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
@@ -92,42 +127,39 @@ class Server():
|
|||||||
self._send_error(None, error)
|
self._send_error(None, error)
|
||||||
return
|
return
|
||||||
|
|
||||||
logging.debug("Parsed input: %s", request)
|
|
||||||
|
|
||||||
if request.id is not None:
|
if request.id is not None:
|
||||||
self._handle_request(request)
|
self._handle_request(request)
|
||||||
else:
|
else:
|
||||||
self._handle_notification(request)
|
self._handle_notification(request)
|
||||||
|
|
||||||
def _handle_notification(self, request):
|
def _handle_notification(self, request):
|
||||||
logging.debug("Handling notification %s", request)
|
|
||||||
method = self._notifications.get(request.method)
|
method = self._notifications.get(request.method)
|
||||||
if not method:
|
if not method:
|
||||||
logging.error("Received uknown notification: %s", request.method)
|
logging.error("Received unknown notification: %s", request.method)
|
||||||
|
return
|
||||||
|
|
||||||
|
callback, internal, sensitive_params = method
|
||||||
|
self._log_request(request, sensitive_params)
|
||||||
|
|
||||||
callback, internal = method
|
|
||||||
if internal:
|
if internal:
|
||||||
# internal requests are handled immediately
|
# internal requests are handled immediately
|
||||||
callback(**request.params)
|
callback(**request.params)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
asyncio.create_task(callback(**request.params))
|
asyncio.create_task(callback(**request.params))
|
||||||
except Exception as error: #pylint: disable=broad-except
|
except Exception:
|
||||||
logging.error(
|
logging.exception("Unexpected exception raised in notification handler")
|
||||||
"Unexpected exception raised in notification handler: %s",
|
|
||||||
repr(error)
|
|
||||||
)
|
|
||||||
|
|
||||||
def _handle_request(self, request):
|
def _handle_request(self, request):
|
||||||
logging.debug("Handling request %s", request)
|
|
||||||
method = self._methods.get(request.method)
|
method = self._methods.get(request.method)
|
||||||
|
|
||||||
if not method:
|
if not method:
|
||||||
logging.error("Received uknown request: %s", request.method)
|
logging.error("Received unknown request: %s", request.method)
|
||||||
self._send_error(request.id, MethodNotFound())
|
self._send_error(request.id, MethodNotFound())
|
||||||
return
|
return
|
||||||
|
|
||||||
callback, internal = method
|
callback, internal, sensitive_params = method
|
||||||
|
self._log_request(request, sensitive_params)
|
||||||
|
|
||||||
if internal:
|
if internal:
|
||||||
# internal requests are handled immediately
|
# internal requests are handled immediately
|
||||||
response = callback(request.params)
|
response = callback(request.params)
|
||||||
@@ -143,8 +175,9 @@ class Server():
|
|||||||
self._send_error(request.id, MethodNotFound())
|
self._send_error(request.id, MethodNotFound())
|
||||||
except JsonRpcError as error:
|
except JsonRpcError as error:
|
||||||
self._send_error(request.id, error)
|
self._send_error(request.id, error)
|
||||||
except Exception as error: #pylint: disable=broad-except
|
except Exception as e: #pylint: disable=broad-except
|
||||||
logging.error("Unexpected exception raised in plugin handler: %s", repr(error))
|
logging.exception("Unexpected exception raised in plugin handler")
|
||||||
|
self._send_error(request.id, UnknownError(str(e)))
|
||||||
|
|
||||||
asyncio.create_task(handle())
|
asyncio.create_task(handle())
|
||||||
|
|
||||||
@@ -165,7 +198,8 @@ class Server():
|
|||||||
try:
|
try:
|
||||||
line = self._encoder.encode(data)
|
line = self._encoder.encode(data)
|
||||||
logging.debug("Sending data: %s", line)
|
logging.debug("Sending data: %s", line)
|
||||||
self._writer.write((line + "\n").encode("utf-8"))
|
data = (line + "\n").encode("utf-8")
|
||||||
|
self._writer.write(data)
|
||||||
asyncio.create_task(self._writer.drain())
|
asyncio.create_task(self._writer.drain())
|
||||||
except TypeError as error:
|
except TypeError as error:
|
||||||
logging.error(str(error))
|
logging.error(str(error))
|
||||||
@@ -193,25 +227,47 @@ class Server():
|
|||||||
|
|
||||||
self._send(response)
|
self._send(response)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _log_request(request, sensitive_params):
|
||||||
|
params = anonymise_sensitive_params(request.params, sensitive_params)
|
||||||
|
if request.id is not None:
|
||||||
|
logging.info("Handling request: id=%s, method=%s, params=%s", request.id, request.method, params)
|
||||||
|
else:
|
||||||
|
logging.info("Handling notification: method=%s, params=%s", request.method, params)
|
||||||
|
|
||||||
class NotificationClient():
|
class NotificationClient():
|
||||||
def __init__(self, writer, encoder=json.JSONEncoder()):
|
def __init__(self, writer, encoder=json.JSONEncoder()):
|
||||||
self._writer = writer
|
self._writer = writer
|
||||||
self._encoder = encoder
|
self._encoder = encoder
|
||||||
self._methods = {}
|
self._methods = {}
|
||||||
|
|
||||||
def notify(self, method, params):
|
def notify(self, method, params, sensitive_params=False):
|
||||||
|
"""
|
||||||
|
Send notification
|
||||||
|
:param method:
|
||||||
|
:param params:
|
||||||
|
:param sensitive_params: list of parameters that will by anonymized before logging; if False - no params
|
||||||
|
are considered sensitive, if True - all params are considered sensitive
|
||||||
|
"""
|
||||||
notification = {
|
notification = {
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
"method": method,
|
"method": method,
|
||||||
"params": params
|
"params": params
|
||||||
}
|
}
|
||||||
|
self._log(method, params, sensitive_params)
|
||||||
self._send(notification)
|
self._send(notification)
|
||||||
|
|
||||||
def _send(self, data):
|
def _send(self, data):
|
||||||
try:
|
try:
|
||||||
line = self._encoder.encode(data)
|
line = self._encoder.encode(data)
|
||||||
logging.debug("Sending data: %s", line)
|
data = (line + "\n").encode("utf-8")
|
||||||
self._writer.write((line + "\n").encode("utf-8"))
|
logging.debug("Sending %d byte of data", len(data))
|
||||||
|
self._writer.write(data)
|
||||||
asyncio.create_task(self._writer.drain())
|
asyncio.create_task(self._writer.drain())
|
||||||
except TypeError as error:
|
except TypeError as error:
|
||||||
logging.error("Failed to parse outgoing message: %s", str(error))
|
logging.error("Failed to parse outgoing message: %s", str(error))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _log(method, params, sensitive_params):
|
||||||
|
params = anonymise_sensitive_params(params, sensitive_params)
|
||||||
|
logging.info("Sending notification: method=%s, params=%s", method, params)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import logging.handlers
|
||||||
import dataclasses
|
import dataclasses
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
import sys
|
||||||
|
|
||||||
from galaxy.api.jsonrpc import Server, NotificationClient
|
from galaxy.api.jsonrpc import Server, NotificationClient
|
||||||
from galaxy.api.consts import Feature
|
from galaxy.api.consts import Feature
|
||||||
@@ -20,8 +22,10 @@ class JSONEncoder(json.JSONEncoder):
|
|||||||
return super().default(o)
|
return super().default(o)
|
||||||
|
|
||||||
class Plugin():
|
class Plugin():
|
||||||
def __init__(self, platform, reader, writer, handshake_token):
|
def __init__(self, platform, version, reader, writer, handshake_token):
|
||||||
|
logging.info("Creating plugin for platform %s, version %s", platform.value, version)
|
||||||
self._platform = platform
|
self._platform = platform
|
||||||
|
self._version = version
|
||||||
|
|
||||||
self._feature_methods = OrderedDict()
|
self._feature_methods = OrderedDict()
|
||||||
self._active = True
|
self._active = True
|
||||||
@@ -34,7 +38,7 @@ class Plugin():
|
|||||||
self._notification_client = NotificationClient(self._writer, encoder)
|
self._notification_client = NotificationClient(self._writer, encoder)
|
||||||
|
|
||||||
def eof_handler():
|
def eof_handler():
|
||||||
self._active = False
|
self._shutdown()
|
||||||
self._server.register_eof(eof_handler)
|
self._server.register_eof(eof_handler)
|
||||||
|
|
||||||
# internal
|
# internal
|
||||||
@@ -43,7 +47,8 @@ class Plugin():
|
|||||||
self._register_method("ping", self._ping, internal=True)
|
self._register_method("ping", self._ping, internal=True)
|
||||||
|
|
||||||
# implemented by developer
|
# implemented by developer
|
||||||
self._register_method("init_authentication", self.authenticate)
|
self._register_method("init_authentication", self.authenticate, sensitive_params=["stored_credentials"])
|
||||||
|
self._register_method("pass_login_credentials", self.pass_login_credentials)
|
||||||
self._register_method(
|
self._register_method(
|
||||||
"import_owned_games",
|
"import_owned_games",
|
||||||
self.get_owned_games,
|
self.get_owned_games,
|
||||||
@@ -133,7 +138,7 @@ class Plugin():
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def _register_method(self, name, handler, result_name=None, internal=False, feature=None):
|
def _register_method(self, name, handler, result_name=None, internal=False, sensitive_params=False, feature=None):
|
||||||
if internal:
|
if internal:
|
||||||
def method(params):
|
def method(params):
|
||||||
result = handler(**params)
|
result = handler(**params)
|
||||||
@@ -142,7 +147,7 @@ class Plugin():
|
|||||||
result_name: result
|
result_name: result
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
self._server.register_method(name, method, True)
|
self._server.register_method(name, method, True, sensitive_params)
|
||||||
else:
|
else:
|
||||||
async def method(params):
|
async def method(params):
|
||||||
result = await handler(**params)
|
result = await handler(**params)
|
||||||
@@ -151,13 +156,13 @@ class Plugin():
|
|||||||
result_name: result
|
result_name: result
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
self._server.register_method(name, method, False)
|
self._server.register_method(name, method, False, sensitive_params)
|
||||||
|
|
||||||
if feature is not None:
|
if feature is not None:
|
||||||
self._feature_methods.setdefault(feature, []).append(handler)
|
self._feature_methods.setdefault(feature, []).append(handler)
|
||||||
|
|
||||||
def _register_notification(self, name, handler, internal=False, feature=None):
|
def _register_notification(self, name, handler, internal=False, sensitive_params=False, feature=None):
|
||||||
self._server.register_notification(name, handler, internal)
|
self._server.register_notification(name, handler, internal, sensitive_params)
|
||||||
|
|
||||||
if feature is not None:
|
if feature is not None:
|
||||||
self._feature_methods.setdefault(feature, []).append(handler)
|
self._feature_methods.setdefault(feature, []).append(handler)
|
||||||
@@ -166,8 +171,10 @@ class Plugin():
|
|||||||
"""Plugin main coorutine"""
|
"""Plugin main coorutine"""
|
||||||
async def pass_control():
|
async def pass_control():
|
||||||
while self._active:
|
while self._active:
|
||||||
logging.debug("Passing control to plugin")
|
try:
|
||||||
self.tick()
|
self.tick()
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Unexpected exception raised in plugin tick")
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
await asyncio.gather(pass_control(), self._server.run())
|
await asyncio.gather(pass_control(), self._server.run())
|
||||||
@@ -194,7 +201,7 @@ class Plugin():
|
|||||||
"""Notify client to store plugin credentials.
|
"""Notify client to store plugin credentials.
|
||||||
They will be pass to next authencicate calls.
|
They will be pass to next authencicate calls.
|
||||||
"""
|
"""
|
||||||
self._notification_client.notify("store_credentials", credentials)
|
self._notification_client.notify("store_credentials", credentials, sensitive_params=True)
|
||||||
|
|
||||||
def add_game(self, game):
|
def add_game(self, game):
|
||||||
params = {"owned_game" : game}
|
params = {"owned_game" : game}
|
||||||
@@ -267,6 +274,9 @@ class Plugin():
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
async def pass_login_credentials(self, step, credentials, cookies):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def get_owned_games(self):
|
async def get_owned_games(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
@@ -291,7 +301,7 @@ class Plugin():
|
|||||||
async def get_users(self, user_id_list):
|
async def get_users(self, user_id_list):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def send_message(self, room_id, message):
|
async def send_message(self, room_id, message_text):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
async def mark_as_read(self, room_id, last_message_id):
|
async def mark_as_read(self, room_id, last_message_id):
|
||||||
@@ -309,22 +319,34 @@ class Plugin():
|
|||||||
async def get_game_times(self):
|
async def get_game_times(self):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
def create_and_run_plugin(plugin_class, argv):
|
def create_and_run_plugin(plugin_class, argv):
|
||||||
if not issubclass(plugin_class, Plugin):
|
|
||||||
raise TypeError("plugin_class must be subclass of Plugin")
|
|
||||||
if len(argv) < 3:
|
if len(argv) < 3:
|
||||||
raise ValueError("Not enough parameters, required: token, port")
|
logging.critical("Not enough parameters, required: token, port")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
token = argv[1]
|
token = argv[1]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
port = int(argv[2])
|
port = int(argv[2])
|
||||||
except ValueError as e:
|
except ValueError:
|
||||||
raise ValueError("Failed to parse port value, {}".format(e))
|
logging.critical("Failed to parse port value: %s", argv[2])
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
if not (1 <= port <= 65535):
|
if not (1 <= port <= 65535):
|
||||||
raise ValueError("Port value out of range (1, 65535)")
|
logging.critical("Port value out of range (1, 65535)")
|
||||||
|
sys.exit(3)
|
||||||
|
|
||||||
|
if not issubclass(plugin_class, Plugin):
|
||||||
|
logging.critical("plugin_class must be subclass of Plugin")
|
||||||
|
sys.exit(4)
|
||||||
|
|
||||||
async def coroutine():
|
async def coroutine():
|
||||||
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
reader, writer = await asyncio.open_connection("127.0.0.1", port)
|
||||||
plugin = plugin_class(reader, writer, token)
|
plugin = plugin_class(reader, writer, token)
|
||||||
await plugin.run()
|
await plugin.run()
|
||||||
asyncio.run(coroutine())
|
|
||||||
|
try:
|
||||||
|
asyncio.run(coroutine())
|
||||||
|
except Exception:
|
||||||
|
logging.exception("Error while running plugin")
|
||||||
|
sys.exit(5)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from typing import List, Optional
|
from typing import List, Dict, Optional
|
||||||
|
|
||||||
from galaxy.api.consts import LicenseType, LocalGameState, PresenceState
|
from galaxy.api.consts import LicenseType, LocalGameState, PresenceState
|
||||||
|
|
||||||
@@ -8,6 +8,19 @@ class Authentication():
|
|||||||
user_id: str
|
user_id: str
|
||||||
user_name: str
|
user_name: str
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Cookie():
|
||||||
|
name: str
|
||||||
|
value: str
|
||||||
|
domain: Optional[str] = None
|
||||||
|
path: Optional[str] = None
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class NextStep():
|
||||||
|
next_step: str
|
||||||
|
auth_params: Dict[str, str]
|
||||||
|
cookies: Optional[List[Cookie]] = None
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class LicenseInfo():
|
class LicenseInfo():
|
||||||
license_type: LicenseType
|
license_type: LicenseType
|
||||||
@@ -28,8 +41,13 @@ class Game():
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Achievement():
|
class Achievement():
|
||||||
achievement_id: str
|
|
||||||
unlock_time: int
|
unlock_time: int
|
||||||
|
achievement_id: Optional[str] = None
|
||||||
|
achievement_name: Optional[str] = None
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
assert self.achievement_id or self.achievement_name, \
|
||||||
|
"One of achievement_id or achievement_name is required"
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class LocalGame():
|
class LocalGame():
|
||||||
|
|||||||
4
setup.py
4
setup.py
@@ -2,9 +2,9 @@ from setuptools import setup, find_packages
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="galaxy.plugin.api",
|
name="galaxy.plugin.api",
|
||||||
version="0.8",
|
version="0.20",
|
||||||
description="Galaxy python plugin API",
|
description="Galaxy python plugin API",
|
||||||
author='Galaxy team',
|
author='Galaxy team',
|
||||||
author_email='galaxy@gog.com',
|
author_email='galaxy@gog.com',
|
||||||
packages=find_packages()
|
packages=find_packages(exclude=["tests"])
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ def plugin(reader, writer):
|
|||||||
stack.enter_context(patch.object(Plugin, method, new_callable=AsyncMock))
|
stack.enter_context(patch.object(Plugin, method, new_callable=AsyncMock))
|
||||||
for method in methods:
|
for method in methods:
|
||||||
stack.enter_context(patch.object(Plugin, method))
|
stack.enter_context(patch.object(Plugin, method))
|
||||||
yield Plugin(Platform.Generic, reader, writer, "token")
|
yield Plugin(Platform.Generic, "0.1", reader, writer, "token")
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def my_caplog(caplog):
|
def my_caplog(caplog):
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
from pytest import raises
|
||||||
|
|
||||||
from galaxy.api.types import Achievement
|
from galaxy.api.types import Achievement
|
||||||
from galaxy.api.errors import UnknownError
|
from galaxy.api.errors import UnknownError
|
||||||
|
|
||||||
|
def test_initialization_no_unlock_time():
|
||||||
|
with raises(Exception):
|
||||||
|
Achievement(achievement_id="lvl30", achievement_name="Got level 30")
|
||||||
|
|
||||||
|
def test_initialization_no_id_nor_name():
|
||||||
|
with raises(AssertionError):
|
||||||
|
Achievement(unlock_time=1234567890)
|
||||||
|
|
||||||
def test_success(plugin, readline, write):
|
def test_success(plugin, readline, write):
|
||||||
request = {
|
request = {
|
||||||
"jsonrpc": "2.0",
|
"jsonrpc": "2.0",
|
||||||
@@ -15,8 +24,9 @@ def test_success(plugin, readline, write):
|
|||||||
}
|
}
|
||||||
readline.side_effect = [json.dumps(request), ""]
|
readline.side_effect = [json.dumps(request), ""]
|
||||||
plugin.get_unlocked_achievements.return_value = [
|
plugin.get_unlocked_achievements.return_value = [
|
||||||
Achievement("lvl10", 1548421241),
|
Achievement(achievement_id="lvl10", unlock_time=1548421241),
|
||||||
Achievement("lvl20", 1548422395)
|
Achievement(achievement_name="Got level 20", unlock_time=1548422395),
|
||||||
|
Achievement(achievement_id="lvl30", achievement_name="Got level 30", unlock_time=1548495633)
|
||||||
]
|
]
|
||||||
asyncio.run(plugin.run())
|
asyncio.run(plugin.run())
|
||||||
plugin.get_unlocked_achievements.assert_called_with(game_id="14")
|
plugin.get_unlocked_achievements.assert_called_with(game_id="14")
|
||||||
@@ -32,8 +42,13 @@ def test_success(plugin, readline, write):
|
|||||||
"unlock_time": 1548421241
|
"unlock_time": 1548421241
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"achievement_id": "lvl20",
|
"achievement_name": "Got level 20",
|
||||||
"unlock_time": 1548422395
|
"unlock_time": 1548422395
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"achievement_id": "lvl30",
|
||||||
|
"achievement_name": "Got level 30",
|
||||||
|
"unlock_time": 1548495633
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -65,7 +80,7 @@ def test_failure(plugin, readline, write):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def test_unlock_achievement(plugin, write):
|
def test_unlock_achievement(plugin, write):
|
||||||
achievement = Achievement("lvl20", 1548422395)
|
achievement = Achievement(achievement_id="lvl20", unlock_time=1548422395)
|
||||||
|
|
||||||
async def couritine():
|
async def couritine():
|
||||||
plugin.unlock_achievement("14", achievement)
|
plugin.unlock_achievement("14", achievement)
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ from galaxy.api.plugin import Plugin
|
|||||||
from galaxy.api.consts import Platform, Feature
|
from galaxy.api.consts import Platform, Feature
|
||||||
|
|
||||||
def test_base_class():
|
def test_base_class():
|
||||||
plugin = Plugin(Platform.Generic, None, None, None)
|
plugin = Plugin(Platform.Generic, "0.1", None, None, None)
|
||||||
assert plugin.features == []
|
assert plugin.features == []
|
||||||
|
|
||||||
def test_no_overloads():
|
def test_no_overloads():
|
||||||
class PluginImpl(Plugin): #pylint: disable=abstract-method
|
class PluginImpl(Plugin): #pylint: disable=abstract-method
|
||||||
pass
|
pass
|
||||||
|
|
||||||
plugin = PluginImpl(Platform.Generic, None, None, None)
|
plugin = PluginImpl(Platform.Generic, "0.1", None, None, None)
|
||||||
assert plugin.features == []
|
assert plugin.features == []
|
||||||
|
|
||||||
def test_one_method_feature():
|
def test_one_method_feature():
|
||||||
@@ -17,7 +17,7 @@ def test_one_method_feature():
|
|||||||
async def get_owned_games(self):
|
async def get_owned_games(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
plugin = PluginImpl(Platform.Generic, None, None, None)
|
plugin = PluginImpl(Platform.Generic, "0.1", None, None, None)
|
||||||
assert plugin.features == [Feature.ImportOwnedGames]
|
assert plugin.features == [Feature.ImportOwnedGames]
|
||||||
|
|
||||||
def test_multiple_methods_feature_all():
|
def test_multiple_methods_feature_all():
|
||||||
@@ -33,7 +33,7 @@ def test_multiple_methods_feature_all():
|
|||||||
async def get_room_history_from_timestamp(self, room_id, timestamp):
|
async def get_room_history_from_timestamp(self, room_id, timestamp):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
plugin = PluginImpl(Platform.Generic, None, None, None)
|
plugin = PluginImpl(Platform.Generic, "0.1", None, None, None)
|
||||||
assert plugin.features == [Feature.Chat]
|
assert plugin.features == [Feature.Chat]
|
||||||
|
|
||||||
def test_multiple_methods_feature_not_all():
|
def test_multiple_methods_feature_not_all():
|
||||||
@@ -41,5 +41,5 @@ def test_multiple_methods_feature_not_all():
|
|||||||
async def send_message(self, room_id, message):
|
async def send_message(self, room_id, message):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
plugin = PluginImpl(Platform.Generic, None, None, None)
|
plugin = PluginImpl(Platform.Generic, "0.1", None, None, None)
|
||||||
assert plugin.features == []
|
assert plugin.features == []
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ def test_get_capabilites(reader, writer, readline, write):
|
|||||||
"method": "get_capabilities"
|
"method": "get_capabilities"
|
||||||
}
|
}
|
||||||
token = "token"
|
token = "token"
|
||||||
plugin = PluginImpl(Platform.Generic, reader, writer, token)
|
plugin = PluginImpl(Platform.Generic, "0.1", reader, writer, token)
|
||||||
readline.side_effect = [json.dumps(request), ""]
|
readline.side_effect = [json.dumps(request), ""]
|
||||||
asyncio.run(plugin.run())
|
asyncio.run(plugin.run())
|
||||||
response = json.loads(write.call_args[0][0])
|
response = json.loads(write.call_args[0][0])
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ def test_success(plugin, readline, write):
|
|||||||
readline.side_effect = [json.dumps(request), ""]
|
readline.side_effect = [json.dumps(request), ""]
|
||||||
|
|
||||||
plugin.get_local_games.return_value = [
|
plugin.get_local_games.return_value = [
|
||||||
LocalGame("1", "Running"),
|
LocalGame("1", LocalGameState.Running),
|
||||||
LocalGame("2", "Installed")
|
LocalGame("2", LocalGameState.Installed),
|
||||||
|
LocalGame("3", LocalGameState.Installed | LocalGameState.Running)
|
||||||
]
|
]
|
||||||
asyncio.run(plugin.run())
|
asyncio.run(plugin.run())
|
||||||
plugin.get_local_games.assert_called_with()
|
plugin.get_local_games.assert_called_with()
|
||||||
@@ -31,11 +32,15 @@ def test_success(plugin, readline, write):
|
|||||||
"local_games" : [
|
"local_games" : [
|
||||||
{
|
{
|
||||||
"game_id": "1",
|
"game_id": "1",
|
||||||
"local_game_state": "Running"
|
"local_game_state": LocalGameState.Running.value
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"game_id": "2",
|
"game_id": "2",
|
||||||
"local_game_state": "Installed"
|
"local_game_state": LocalGameState.Installed.value
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"game_id": "3",
|
||||||
|
"local_game_state": (LocalGameState.Installed | LocalGameState.Running).value
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@@ -85,7 +90,7 @@ def test_local_game_state_update(plugin, write):
|
|||||||
"params": {
|
"params": {
|
||||||
"local_game": {
|
"local_game": {
|
||||||
"game_id": "1",
|
"game_id": "1",
|
||||||
"local_game_state": "Running"
|
"local_game_state": LocalGameState.Running.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user