maxapi-python 1.2.5__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. maxapi_python-2.0.0.dist-info/METADATA +217 -0
  2. maxapi_python-2.0.0.dist-info/RECORD +140 -0
  3. {maxapi_python-1.2.5.dist-info → maxapi_python-2.0.0.dist-info}/WHEEL +1 -1
  4. pymax/__init__.py +50 -105
  5. pymax/api/__init__.py +17 -0
  6. pymax/api/auth/__init__.py +1 -0
  7. pymax/api/auth/enums.py +17 -0
  8. pymax/api/auth/payloads.py +129 -0
  9. pymax/api/auth/service.py +313 -0
  10. pymax/api/auth/types.py +13 -0
  11. pymax/api/chats/__init__.py +8 -0
  12. pymax/api/chats/enums.py +27 -0
  13. pymax/api/chats/payloads.py +103 -0
  14. pymax/api/chats/service.py +277 -0
  15. pymax/api/facade.py +32 -0
  16. pymax/api/messages/__init__.py +1 -0
  17. pymax/api/messages/enums.py +17 -0
  18. pymax/api/messages/payloads.py +92 -0
  19. pymax/api/messages/service.py +337 -0
  20. pymax/api/models.py +13 -0
  21. pymax/api/response.py +123 -0
  22. pymax/api/self/__init__.py +2 -0
  23. pymax/api/self/enums.py +11 -0
  24. pymax/api/self/payloads.py +41 -0
  25. pymax/api/self/service.py +142 -0
  26. pymax/api/session/__init__.py +1 -0
  27. pymax/api/session/enums.py +10 -0
  28. pymax/api/session/payloads.py +76 -0
  29. pymax/api/session/service.py +72 -0
  30. pymax/api/uploads/__init__.py +1 -0
  31. pymax/api/uploads/models.py +49 -0
  32. pymax/api/uploads/payloads.py +25 -0
  33. pymax/api/uploads/service.py +458 -0
  34. pymax/api/users/__init__.py +2 -0
  35. pymax/api/users/enums.py +12 -0
  36. pymax/api/users/payloads.py +16 -0
  37. pymax/api/users/service.py +124 -0
  38. pymax/app.py +273 -0
  39. pymax/auth/__init__.py +25 -0
  40. pymax/auth/base.py +37 -0
  41. pymax/auth/email.py +0 -0
  42. pymax/auth/models.py +5 -0
  43. pymax/auth/providers.py +127 -0
  44. pymax/auth/qr.py +135 -0
  45. pymax/auth/service.py +25 -0
  46. pymax/auth/sms.py +122 -0
  47. pymax/base.py +204 -0
  48. pymax/client.py +106 -0
  49. pymax/client_web.py +83 -0
  50. pymax/config.py +215 -0
  51. pymax/connection/__init__.py +1 -0
  52. pymax/connection/connection.py +205 -0
  53. pymax/connection/pending.py +46 -0
  54. pymax/connection/readers/__init__.py +2 -0
  55. pymax/connection/readers/base.py +6 -0
  56. pymax/connection/readers/tcp.py +29 -0
  57. pymax/connection/readers/ws.py +14 -0
  58. pymax/dispatch/__init__.py +10 -0
  59. pymax/dispatch/dispatcher.py +222 -0
  60. pymax/dispatch/enums.py +12 -0
  61. pymax/dispatch/mapping.py +73 -0
  62. pymax/dispatch/resolvers.py +52 -0
  63. pymax/dispatch/router.py +216 -0
  64. pymax/exceptions.py +22 -89
  65. pymax/files/__init__.py +9 -0
  66. pymax/files/base.py +82 -0
  67. pymax/files/file.py +76 -0
  68. pymax/files/photo.py +108 -0
  69. pymax/files/static.py +10 -0
  70. pymax/files/video.py +74 -0
  71. pymax/formatting/__init__.py +0 -0
  72. pymax/formatting/markdown.py +217 -0
  73. pymax/infra/__init__.py +1 -0
  74. pymax/infra/auth.py +55 -0
  75. pymax/infra/base.py +15 -0
  76. pymax/infra/chat.py +240 -0
  77. pymax/infra/message.py +252 -0
  78. pymax/infra/protocol.py +9 -0
  79. pymax/infra/self.py +139 -0
  80. pymax/infra/user.py +107 -0
  81. pymax/logging.py +129 -0
  82. pymax/protocol/__init__.py +11 -0
  83. pymax/protocol/base.py +13 -0
  84. pymax/protocol/enums.py +180 -0
  85. pymax/protocol/models.py +33 -0
  86. pymax/protocol/tcp/__init__.py +1 -0
  87. pymax/protocol/tcp/compression.py +97 -0
  88. pymax/protocol/tcp/framing.py +68 -0
  89. pymax/protocol/tcp/payload.py +127 -0
  90. pymax/protocol/tcp/protocol.py +68 -0
  91. pymax/protocol/ws/__init__.py +1 -0
  92. pymax/protocol/ws/protocol.py +27 -0
  93. pymax/py.typed +0 -0
  94. pymax/routers.py +8 -0
  95. pymax/session/__init__.py +3 -0
  96. pymax/session/models.py +11 -0
  97. pymax/session/protocol.py +14 -0
  98. pymax/session/store.py +232 -0
  99. pymax/telemetry/__init__.py +3 -0
  100. pymax/telemetry/navigation.py +181 -0
  101. pymax/telemetry/payloads.py +142 -0
  102. pymax/telemetry/service.py +225 -0
  103. pymax/transport/__init__.py +0 -0
  104. pymax/transport/base.py +14 -0
  105. pymax/transport/tcp.py +93 -0
  106. pymax/transport/websocket.py +50 -0
  107. pymax/types/__init__.py +2 -0
  108. pymax/types/domain/__init__.py +11 -0
  109. pymax/types/domain/attachments/__init__.py +11 -0
  110. pymax/types/domain/attachments/audio.py +35 -0
  111. pymax/types/domain/attachments/call.py +26 -0
  112. pymax/types/domain/attachments/contact.py +32 -0
  113. pymax/types/domain/attachments/control.py +20 -0
  114. pymax/types/domain/attachments/enums.py +27 -0
  115. pymax/types/domain/attachments/file.py +56 -0
  116. pymax/types/domain/attachments/keyboards/__init__.py +1 -0
  117. pymax/types/domain/attachments/keyboards/inline.py +19 -0
  118. pymax/types/domain/attachments/photo.py +45 -0
  119. pymax/types/domain/attachments/share.py +29 -0
  120. pymax/types/domain/attachments/sticker.py +50 -0
  121. pymax/types/domain/attachments/video.py +90 -0
  122. pymax/types/domain/auth.py +161 -0
  123. pymax/types/domain/base.py +17 -0
  124. pymax/types/domain/chat.py +426 -0
  125. pymax/types/domain/element.py +24 -0
  126. pymax/types/domain/enums.py +24 -0
  127. pymax/types/domain/error.py +20 -0
  128. pymax/types/domain/folder.py +74 -0
  129. pymax/types/domain/login.py +35 -0
  130. pymax/types/domain/message.py +378 -0
  131. pymax/types/domain/name.py +20 -0
  132. pymax/types/domain/profile.py +15 -0
  133. pymax/types/domain/session.py +52 -0
  134. pymax/types/domain/sync.py +80 -0
  135. pymax/types/domain/user.py +117 -0
  136. pymax/types/events/__init__.py +3 -0
  137. pymax/types/events/file.py +5 -0
  138. pymax/types/events/message.py +37 -0
  139. pymax/types/events/video.py +5 -0
  140. maxapi_python-1.2.5.dist-info/METADATA +0 -202
  141. maxapi_python-1.2.5.dist-info/RECORD +0 -33
  142. pymax/core.py +0 -398
  143. pymax/crud.py +0 -96
  144. pymax/files.py +0 -138
  145. pymax/filters.py +0 -164
  146. pymax/formatter.py +0 -31
  147. pymax/formatting.py +0 -74
  148. pymax/interfaces.py +0 -558
  149. pymax/mixins/__init__.py +0 -40
  150. pymax/mixins/auth.py +0 -594
  151. pymax/mixins/channel.py +0 -130
  152. pymax/mixins/group.py +0 -458
  153. pymax/mixins/handler.py +0 -285
  154. pymax/mixins/message.py +0 -879
  155. pymax/mixins/scheduler.py +0 -28
  156. pymax/mixins/self.py +0 -259
  157. pymax/mixins/socket.py +0 -306
  158. pymax/mixins/telemetry.py +0 -118
  159. pymax/mixins/user.py +0 -219
  160. pymax/mixins/websocket.py +0 -151
  161. pymax/models.py +0 -8
  162. pymax/navigation.py +0 -187
  163. pymax/payloads.py +0 -403
  164. pymax/protocols.py +0 -123
  165. pymax/static/constant.py +0 -96
  166. pymax/static/enum.py +0 -231
  167. pymax/types.py +0 -1220
  168. pymax/utils.py +0 -90
  169. {maxapi_python-1.2.5.dist-info → maxapi_python-2.0.0.dist-info}/licenses/LICENSE +0 -0
pymax/interfaces.py DELETED
@@ -1,558 +0,0 @@
1
- import asyncio
2
- import contextlib
3
- import json
4
- import logging
5
- import time
6
- import traceback
7
- from abc import abstractmethod
8
- from collections.abc import Awaitable, Callable
9
- from typing import Any
10
-
11
- from typing_extensions import Self
12
-
13
- from pymax.exceptions import SocketNotConnectedError, WebSocketNotConnectedError
14
- from pymax.filters import BaseFilter
15
- from pymax.formatter import ColoredFormatter
16
- from pymax.payloads import BaseWebSocketMessage, SyncPayload, UserAgentPayload
17
- from pymax.protocols import ClientProtocol
18
- from pymax.static.constant import DEFAULT_PING_INTERVAL, DEFAULT_TIMEOUT
19
- from pymax.static.enum import Opcode
20
- from pymax.types import (
21
- Channel,
22
- Chat,
23
- ChatType,
24
- Dialog,
25
- Me,
26
- Message,
27
- MessageStatus,
28
- ReactionCounter,
29
- ReactionInfo,
30
- User,
31
- )
32
- from pymax.utils import MixinsUtils
33
-
34
-
35
- class BaseClient(ClientProtocol):
36
- def _setup_logger(self) -> None:
37
- if not self.logger.handlers:
38
- if not self.logger.level:
39
- self.logger.setLevel(logging.INFO)
40
- handler = logging.StreamHandler()
41
- formatter = ColoredFormatter(
42
- "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
43
- datefmt="%Y-%m-%d %H:%M:%S",
44
- )
45
- handler.setFormatter(formatter)
46
- self.logger.addHandler(handler)
47
-
48
- async def _safe_execute(self, coro, *, context: str = "unknown") -> Any:
49
- try:
50
- return await coro
51
- except Exception as e:
52
- self.logger.error(f"Unhandled exception in {context}: {e}\n{traceback.format_exc()}")
53
-
54
- def _create_safe_task(
55
- self, coro: Awaitable[Any], name: str | None = None
56
- ) -> asyncio.Task[Any | None]:
57
- async def runner():
58
- try:
59
- return await coro
60
- except asyncio.CancelledError:
61
- raise
62
- except Exception as e:
63
- tb = traceback.format_exc()
64
- self.logger.error(f"Unhandled exception in task {name or coro}: {e}\n{tb}")
65
- raise
66
-
67
- task = asyncio.create_task(runner(), name=name)
68
- self._background_tasks.add(task)
69
- return task
70
-
71
- async def _cleanup_client(self) -> None:
72
- for task in list(self._background_tasks):
73
- task.cancel()
74
- try:
75
- await task
76
- except asyncio.CancelledError:
77
- pass
78
- except Exception:
79
- self.logger.debug("Background task raised during cancellation", exc_info=True)
80
- self._background_tasks.discard(task)
81
-
82
- if self._recv_task:
83
- self._recv_task.cancel()
84
- with contextlib.suppress(asyncio.CancelledError):
85
- await self._recv_task
86
- self._recv_task = None
87
-
88
- if self._outgoing_task:
89
- self._outgoing_task.cancel()
90
- with contextlib.suppress(asyncio.CancelledError):
91
- await self._outgoing_task
92
- self._outgoing_task = None
93
-
94
- for fut in self._pending.values():
95
- if not fut.done():
96
- fut.set_exception(WebSocketNotConnectedError())
97
- self._pending.clear()
98
-
99
- if self._ws:
100
- try:
101
- await self._ws.close()
102
- except Exception:
103
- self.logger.debug("Error closing ws during cleanup", exc_info=True)
104
- self._ws = None
105
-
106
- self.is_connected = False
107
- self.logger.info("Client start() cleaned up")
108
-
109
- async def idle(self):
110
- """
111
- Поддерживает клиента в «ожидающем» состоянии до закрытия клиента или иного прерывающего события.
112
-
113
- :return: Никогда не возвращает значение; функция блокирует выполнение.
114
- :rtype: None
115
- """
116
- await asyncio.Event().wait()
117
-
118
- def inspect(self) -> None:
119
- """
120
- Выводит в лог текущий статус клиента для отладки.
121
- """
122
- self.logger.info("Pymax")
123
- self.logger.info("---------")
124
- self.logger.info(f"Connected: {self.is_connected}")
125
- if self.me is not None:
126
- self.logger.info(f"Me: {self.me.names[0].first_name} ({self.me.id})")
127
- else:
128
- self.logger.info("Me: N/A")
129
- self.logger.info(f"Dialogs: {len(self.dialogs)}")
130
- self.logger.info(f"Chats: {len(self.chats)}")
131
- self.logger.info(f"Channels: {len(self.channels)}")
132
- self.logger.info(f"Users cached: {len(self._users)}")
133
- self.logger.info(f"Background tasks: {len(self._background_tasks)}")
134
- self.logger.info(f"Scheduled tasks: {len(self._scheduled_tasks)}")
135
- self.logger.info("---------")
136
-
137
- async def __aenter__(self) -> Self:
138
- self._create_safe_task(self.start(), name="start")
139
- while not self.is_connected:
140
- await asyncio.sleep(0.05)
141
- return self
142
-
143
- async def __aexit__(self, exc_type, exc, tb) -> None:
144
- await self.close()
145
-
146
- @abstractmethod
147
- async def login_with_code(self, temp_token: str, code: str, start: bool = False) -> None:
148
- pass
149
-
150
- @abstractmethod
151
- async def _post_login_tasks(self, sync: bool = True) -> None:
152
- pass
153
-
154
- @abstractmethod
155
- async def _wait_forever(self) -> None:
156
- pass
157
-
158
- @abstractmethod
159
- async def start(self) -> None:
160
- pass
161
-
162
- @abstractmethod
163
- async def close(self) -> None:
164
- pass
165
-
166
-
167
- class BaseTransport(ClientProtocol):
168
- @abstractmethod
169
- async def connect(
170
- self, user_agent: UserAgentPayload | None = None
171
- ) -> dict[str, Any] | None: ...
172
-
173
- @abstractmethod
174
- async def _send_and_wait(
175
- self,
176
- opcode: Opcode,
177
- payload: dict[str, Any],
178
- cmd: int = 0,
179
- timeout: float = DEFAULT_TIMEOUT,
180
- ) -> dict[str, Any]: ...
181
-
182
- @abstractmethod
183
- async def _recv_loop(self) -> None: ...
184
-
185
- def _make_message(
186
- self, opcode: Opcode, payload: dict[str, Any], cmd: int = 0
187
- ) -> dict[str, Any]:
188
- self._seq += 1
189
-
190
- msg = BaseWebSocketMessage(
191
- ver=11,
192
- cmd=cmd,
193
- seq=self._seq,
194
- opcode=opcode.value,
195
- payload=payload,
196
- ).model_dump(by_alias=True)
197
-
198
- self.logger.debug("make_message opcode=%s cmd=%s seq=%s", opcode, cmd, self._seq)
199
- return msg
200
-
201
- async def _send_interactive_ping(self) -> None:
202
- while self.is_connected:
203
- try:
204
- await self._send_and_wait(
205
- opcode=Opcode.PING,
206
- payload={"interactive": True},
207
- cmd=0,
208
- )
209
- self.logger.debug("Interactive ping sent successfully")
210
- except SocketNotConnectedError:
211
- self.logger.debug("Socket disconnected, exiting ping loop")
212
- break
213
- except Exception:
214
- self.logger.warning("Interactive ping failed")
215
- await asyncio.sleep(DEFAULT_PING_INTERVAL)
216
-
217
- async def _handshake(self, user_agent: UserAgentPayload) -> dict[str, Any]:
218
- self.logger.debug(
219
- "Sending handshake with user_agent keys=%s",
220
- user_agent.model_dump(by_alias=True).keys(),
221
- )
222
-
223
- user_agent_json = user_agent.model_dump(by_alias=True)
224
- resp = await self._send_and_wait(
225
- opcode=Opcode.SESSION_INIT,
226
- payload={"deviceId": str(self._device_id), "userAgent": user_agent_json},
227
- )
228
-
229
- if resp.get("payload", {}).get("error"):
230
- MixinsUtils.handle_error(resp)
231
-
232
- self.logger.info("Handshake completed")
233
- return resp
234
-
235
- async def _process_message_handler(
236
- self,
237
- handler: Callable[[Message], Any],
238
- filter: BaseFilter[Message] | None,
239
- message: Message,
240
- ):
241
- result = None
242
- if filter:
243
- if filter(message):
244
- result = handler(message)
245
- else:
246
- return
247
- else:
248
- result = handler(message)
249
- if asyncio.iscoroutine(result):
250
- self._create_safe_task(result, name=f"handler-{handler.__name__}")
251
-
252
- def _parse_json(self, raw: Any) -> dict[str, Any] | None:
253
- try:
254
- return json.loads(raw)
255
- except Exception:
256
- self.logger.warning("JSON parse error", exc_info=True)
257
- return None
258
-
259
- def _handle_pending(self, seq: int | None, data: dict) -> bool:
260
- if isinstance(seq, int):
261
- fut = self._pending.get(seq)
262
- if fut and not fut.done():
263
- fut.set_result(data)
264
- self.logger.debug("Matched response for pending seq=%s", seq)
265
- return True
266
- return False
267
-
268
- async def _handle_incoming_queue(self, data: dict[str, Any]) -> None:
269
- if self._incoming:
270
- try:
271
- self._incoming.put_nowait(data)
272
- except asyncio.QueueFull:
273
- self.logger.warning(
274
- "Incoming queue full; dropping message seq=%s", data.get("seq")
275
- )
276
-
277
- async def _handle_file_upload(self, data: dict[str, Any]) -> None:
278
- if data.get("opcode") != Opcode.NOTIF_ATTACH:
279
- return
280
- payload = data.get("payload", {})
281
- for key in ("fileId", "videoId"):
282
- id_ = payload.get(key)
283
- if id_ is not None:
284
- fut = self._file_upload_waiters.pop(id_, None)
285
- if fut and not fut.done():
286
- fut.set_result(data)
287
- self.logger.debug("Fulfilled file upload waiter for %s=%s", key, id_)
288
-
289
- async def _send_notification_response(self, chat_id: int, message_id: str) -> None:
290
- if self._socket is not None and self.is_connected:
291
- return
292
- await self._send_and_wait(
293
- opcode=Opcode.NOTIF_MESSAGE,
294
- payload={"chatId": chat_id, "messageId": message_id},
295
- cmd=0,
296
- )
297
- self.logger.debug(
298
- "Sent NOTIF_MESSAGE_RECEIVED for chat_id=%s message_id=%s", chat_id, message_id
299
- )
300
-
301
- async def _handle_message_notifications(self, data: dict) -> None:
302
- if data.get("opcode") != Opcode.NOTIF_MESSAGE.value:
303
- return
304
- payload = data.get("payload", {})
305
- msg = Message.from_dict(payload)
306
- if not msg:
307
- return
308
-
309
- if msg.chat_id and msg.id:
310
- await self._send_notification_response(msg.chat_id, str(msg.id))
311
-
312
- handlers_map = {
313
- MessageStatus.EDITED: self._on_message_edit_handlers,
314
- MessageStatus.REMOVED: self._on_message_delete_handlers,
315
- }
316
- if msg.status and msg.status in handlers_map:
317
- for handler, filter in handlers_map[msg.status]:
318
- await self._process_message_handler(handler, filter, msg)
319
- if msg.status is None:
320
- for handler, filter in self._on_message_handlers:
321
- await self._process_message_handler(handler, filter, msg)
322
-
323
- async def _handle_reactions(self, data: dict):
324
- if data.get("opcode") != Opcode.NOTIF_MSG_REACTIONS_CHANGED:
325
- return
326
-
327
- payload = data.get("payload", {})
328
- chat_id = payload.get("chatId")
329
- message_id = payload.get("messageId")
330
-
331
- if not (chat_id and message_id):
332
- return
333
-
334
- total_count = payload.get("totalCount")
335
- your_reaction = payload.get("yourReaction")
336
- counters = [ReactionCounter.from_dict(c) for c in payload.get("counters", [])]
337
-
338
- reaction_info = ReactionInfo(
339
- total_count=total_count,
340
- your_reaction=your_reaction,
341
- counters=counters,
342
- )
343
-
344
- for handler in self._on_reaction_change_handlers:
345
- try:
346
- result = handler(message_id, chat_id, reaction_info)
347
- if asyncio.iscoroutine(result):
348
- await result
349
- except Exception as e:
350
- self.logger.exception("Error in on_reaction_change_handler: %s", e)
351
-
352
- async def _handle_chat_updates(self, data: dict) -> None:
353
- if data.get("opcode") != Opcode.NOTIF_CHAT:
354
- return
355
-
356
- payload = data.get("payload", {})
357
- chat_data = payload.get("chat", {})
358
- chat = Chat.from_dict(chat_data)
359
- if not chat:
360
- return
361
-
362
- for handler in self._on_chat_update_handlers:
363
- try:
364
- result = handler(chat)
365
- if asyncio.iscoroutine(result):
366
- await result
367
- except Exception as e:
368
- self.logger.exception("Error in on_chat_update_handler: %s", e)
369
-
370
- async def _handle_raw_receive(self, data: dict[str, Any]) -> None:
371
- for handler in self._on_raw_receive_handlers:
372
- try:
373
- result = handler(data)
374
- if asyncio.iscoroutine(result):
375
- await result
376
- except Exception as e:
377
- self.logger.exception("Error in on_raw_receive_handler: %s", e)
378
-
379
- async def _dispatch_incoming(self, data: dict[str, Any]) -> None:
380
- await self._handle_raw_receive(data)
381
- await self._handle_file_upload(data)
382
- await self._handle_message_notifications(data)
383
- await self._handle_reactions(data)
384
- await self._handle_chat_updates(data)
385
-
386
- def _log_task_exception(self, fut: asyncio.Future[Any]) -> None:
387
- try:
388
- fut.result()
389
- except asyncio.CancelledError:
390
- pass
391
- except Exception as e:
392
- self.logger.exception("Error retrieving task exception: %s", e)
393
-
394
- async def _queue_message(
395
- self,
396
- opcode: int,
397
- payload: dict[str, Any],
398
- cmd: int = 0,
399
- timeout: float = DEFAULT_TIMEOUT,
400
- max_retries: int = 3,
401
- ) -> None:
402
- if self._outgoing is None:
403
- self.logger.warning("Outgoing queue not initialized")
404
- return
405
-
406
- message = {
407
- "opcode": opcode,
408
- "payload": payload,
409
- "cmd": cmd,
410
- "timeout": timeout,
411
- "retry_count": 0,
412
- "max_retries": max_retries,
413
- }
414
-
415
- await self._outgoing.put(message)
416
- self.logger.debug("Message queued for sending")
417
-
418
- async def _outgoing_loop(self) -> None:
419
- while self.is_connected:
420
- try:
421
- if self._outgoing is None:
422
- await asyncio.sleep(0.1)
423
- continue
424
-
425
- if self._circuit_breaker:
426
- if time.time() - self._last_error_time > 60:
427
- self._circuit_breaker = False
428
- self._error_count = 0
429
- self.logger.info("Circuit breaker reset")
430
- else:
431
- await asyncio.sleep(5)
432
- continue
433
-
434
- message = await self._outgoing.get() # TODO: persistent msg q mb?
435
- if not message:
436
- continue
437
-
438
- retry_count = message.get("retry_count", 0)
439
- max_retries = message.get("max_retries", 3)
440
-
441
- try:
442
- await self._send_and_wait(
443
- opcode=message["opcode"],
444
- payload=message["payload"],
445
- cmd=message.get("cmd", 0),
446
- timeout=message.get("timeout", DEFAULT_TIMEOUT),
447
- )
448
- self.logger.debug("Message sent successfully from queue")
449
- self._error_count = max(0, self._error_count - 1)
450
- except Exception as e:
451
- self._error_count += 1
452
- self._last_error_time = time.time()
453
-
454
- if self._error_count > 10:
455
- self._circuit_breaker = True
456
- self.logger.warning(
457
- "Circuit breaker activated due to %d consecutive errors",
458
- self._error_count,
459
- )
460
- await self._outgoing.put(message)
461
- continue
462
-
463
- retry_delay = self._get_retry_delay(e, retry_count)
464
- self.logger.warning(
465
- "Failed to send message from queue: %s (delay: %ds)",
466
- e,
467
- retry_delay,
468
- )
469
-
470
- if retry_count < max_retries:
471
- message["retry_count"] = retry_count + 1
472
- await asyncio.sleep(retry_delay)
473
- await self._outgoing.put(message)
474
- else:
475
- self.logger.error(
476
- "Message failed after %d retries, dropping",
477
- max_retries,
478
- )
479
-
480
- except Exception:
481
- self.logger.exception("Error in outgoing loop")
482
- await asyncio.sleep(1)
483
-
484
- def _get_retry_delay(self, error: Exception, retry_count: int) -> float:
485
- if isinstance(error, (ConnectionError, OSError)):
486
- return 1.0
487
- elif isinstance(error, TimeoutError):
488
- return 5.0
489
- elif isinstance(error, WebSocketNotConnectedError):
490
- return 2.0
491
- else:
492
- return float(2**retry_count)
493
-
494
- async def _sync(self, user_agent: UserAgentPayload | None = None) -> None:
495
- self.logger.info("Starting initial sync")
496
-
497
- if user_agent is None:
498
- user_agent = self.headers or UserAgentPayload()
499
-
500
- payload = SyncPayload(
501
- interactive=True,
502
- token=self._token,
503
- chats_sync=0,
504
- contacts_sync=0,
505
- presence_sync=0,
506
- drafts_sync=0,
507
- chats_count=40,
508
- user_agent=user_agent,
509
- ).model_dump(by_alias=True)
510
- try:
511
- data = await self._send_and_wait(opcode=Opcode.LOGIN, payload=payload)
512
- raw_payload = data.get("payload", {})
513
-
514
- if error := raw_payload.get("error"):
515
- MixinsUtils.handle_error(data)
516
-
517
- for raw_chat in raw_payload.get("chats", []):
518
- try:
519
- if raw_chat.get("type") == ChatType.DIALOG.value:
520
- self.dialogs.append(Dialog.from_dict(raw_chat))
521
- elif raw_chat.get("type") == ChatType.CHAT.value:
522
- self.chats.append(Chat.from_dict(raw_chat))
523
- elif raw_chat.get("type") == ChatType.CHANNEL.value:
524
- self.channels.append(Channel.from_dict(raw_chat))
525
- except Exception:
526
- self.logger.exception("Error parsing chat entry")
527
-
528
- for raw_user in raw_payload.get("contacts", []):
529
- try:
530
- user = User.from_dict(raw_user)
531
- if user:
532
- self.contacts.append(user)
533
- except Exception:
534
- self.logger.exception("Error parsing contact entry")
535
-
536
- if raw_payload.get("profile", {}).get("contact"):
537
- self.me = Me.from_dict(raw_payload.get("profile", {}).get("contact", {}))
538
-
539
- self.logger.info(
540
- "Sync completed: dialogs=%d chats=%d channels=%d",
541
- len(self.dialogs),
542
- len(self.chats),
543
- len(self.channels),
544
- )
545
-
546
- except Exception as e:
547
- self.logger.exception("Sync failed")
548
- self.is_connected = False
549
- if self._ws:
550
- await self._ws.close()
551
- self._ws = None
552
- raise
553
-
554
- async def _get_chat(self, chat_id: int) -> Chat | None:
555
- for chat in self.chats:
556
- if chat.id == chat_id:
557
- return chat
558
- return None
pymax/mixins/__init__.py DELETED
@@ -1,40 +0,0 @@
1
- from .auth import AuthMixin
2
- from .channel import ChannelMixin
3
- from .group import GroupMixin
4
- from .handler import HandlerMixin
5
- from .message import MessageMixin
6
- from .scheduler import SchedulerMixin
7
- from .self import SelfMixin
8
- from .socket import SocketMixin
9
- from .telemetry import TelemetryMixin
10
- from .user import UserMixin
11
- from .websocket import WebSocketMixin
12
-
13
-
14
- class ApiMixin(
15
- AuthMixin,
16
- HandlerMixin,
17
- UserMixin,
18
- ChannelMixin,
19
- SelfMixin,
20
- MessageMixin,
21
- TelemetryMixin,
22
- GroupMixin,
23
- SchedulerMixin,
24
- ):
25
- pass
26
-
27
-
28
- __all__ = [
29
- "ApiMixin",
30
- "AuthMixin",
31
- "ChannelMixin",
32
- "HandlerMixin",
33
- "MessageMixin",
34
- "SchedulerMixin",
35
- "SelfMixin",
36
- "SocketMixin",
37
- "TelemetryMixin",
38
- "UserMixin",
39
- "WebSocketMixin",
40
- ]