fix: move mutable object initialization to constructor

master
sati.ac 2023-09-08 15:00:53 +03:00
parent 526597decf
commit 4ec4bb9aac
3 changed files with 17 additions and 10 deletions

View File

@ -4,7 +4,7 @@ build-backend = "hatchling.build"
[project] [project]
name = "sati-ac" name = "sati-ac"
version = "0.1.0" version = "0.1.1"
authors = [ authors = [
{ name="sati.ac", email="sati.ac@proton.me" } { name="sati.ac", email="sati.ac@proton.me" }
] ]
@ -19,4 +19,4 @@ classifiers = [
dependencies = ["websockets"] dependencies = ["websockets"]
[project.urls] [project.urls]
"Homepage" = "https://git.sati.ac/sati.ac/sati-py" "Homepage" = "https://git.sati.ac/sati.ac/sati-py"

View File

@ -25,7 +25,7 @@ class Sati:
_socket: SatiSocket _socket: SatiSocket
_project_id: int _project_id: int
_awaited_tasks: dict = {} _awaited_tasks: dict
def __init__( def __init__(
self, self,
@ -35,6 +35,7 @@ class Sati:
project_id: int = 0, project_id: int = 0,
debug = False debug = False
): ):
self._awaited_tasks = {}
self._socket = SatiSocket(token, reconnection_interval, url, debug) self._socket = SatiSocket(token, reconnection_interval, url, debug)
self._project_id = project_id self._project_id = project_id
self._socket.on('taskUpdate', self._process_task) self._socket.on('taskUpdate', self._process_task)
@ -63,8 +64,8 @@ class Sati:
else: else:
fut.set_exception(UnableToSolveTask(task)) fut.set_exception(UnableToSolveTask(task))
def destroy(self): async def destroy(self):
self._socket.close() await self._socket.close()
async def get_balance(self) -> float: async def get_balance(self) -> float:
return (await self._socket.call('getBalance')).balance return (await self._socket.call('getBalance')).balance

View File

@ -36,11 +36,11 @@ class SatiSocket:
__reconnection_interval: float __reconnection_interval: float
__connector_ref: asyncio.Task __connector_ref: asyncio.Task
__id_counter: int = 0 __id_counter: int = 0
__awaited_replies: dict = {} __awaited_replies: dict
__error = None __error = None
__queue = [] __queue: list
__url: str __url: str
__event_handlers = {} __event_handlers: dict
__debug: bool __debug: bool
def __init__( def __init__(
@ -50,6 +50,9 @@ class SatiSocket:
url = 'wss://api.sati.ac/ws', url = 'wss://api.sati.ac/ws',
debug = False debug = False
): ):
self.__awaited_replies = {}
self.__queue = []
self.__event_handlers = {}
self.__token = token self.__token = token
self.__reconnection_interval = reconnection_interval self.__reconnection_interval = reconnection_interval
self.__url = url self.__url = url
@ -117,7 +120,9 @@ class SatiSocket:
if msg_type in ( 'auth', 'call' ): if msg_type in ( 'auth', 'call' ):
return await fut return await fut
async def call(self, method: str, data: dict = {}) -> SatiDict: async def call(self, method: str, data: dict | None = None) -> SatiDict:
if data == None: data = {}
if self.__state == STATE_CONNECTED: if self.__state == STATE_CONNECTED:
resp = await self.__send('call', { resp = await self.__send('call', {
'method': method, 'method': method,
@ -154,8 +159,9 @@ class SatiSocket:
del self.__awaited_replies[key] del self.__awaited_replies[key]
raise ex raise ex
def close(self): async def close(self):
self.__connector_ref.cancel() self.__connector_ref.cancel()
await self.__socket.close()
def on(self, event: str, handler: typing.Callable[[SatiDict], None]): def on(self, event: str, handler: typing.Callable[[SatiDict], None]):
if event not in self.__event_handlers: if event not in self.__event_handlers: