Skip to content

Commit f5fdd16

Browse files
committed
fix: 🐛 Fix Background Login
- Only sleep after successful login
1 parent c2cd56c commit f5fdd16

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/bitpin/clients/async_client.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
from .core import CoreClient
99
from .. import types as t
1010
from .. import enums
11-
from ..exceptions import APIException, RequestException
11+
from ..exceptions import (
12+
APIException,
13+
RequestException,
14+
)
1215
from .._utils import get_loop
1316

1417

@@ -317,8 +320,8 @@ async def _background_relogin_task(self) -> None: # type: ignore[override]
317320

318321
while True:
319322
try:
320-
await asyncio.sleep(self._background_relogin_interval)
321323
await self.login()
324+
await asyncio.sleep(self._background_relogin_interval)
322325
except Exception: # pylint: disable=broad-except
323326
continue
324327

@@ -327,23 +330,22 @@ async def _background_refresh_token_task(self) -> None: # type: ignore[override
327330

328331
while True:
329332
try:
330-
await asyncio.sleep(self._background_refresh_token_interval)
331333
await self.refresh_access_token()
334+
await asyncio.sleep(self._background_refresh_token_interval)
332335
except Exception: # pylint: disable=broad-except
333336
continue
334337

335338
async def _handle_login(self) -> None: # type: ignore[override]
336339
"""Handle login."""
337340

338341
if self.api_key and self.api_secret:
339-
if self.refresh_token is None or self.access_token is None:
340-
await self.login()
342+
await self.login()
341343

342344
if self._background_relogin:
343-
self.loop.create_task(self._background_relogin_task())
345+
self.loop.create_task(self._background_relogin_task()) # noqa
344346

345347
if self._background_refresh_token:
346-
self.loop.create_task(self._background_refresh_token_task())
348+
self.loop.create_task(self._background_refresh_token_task()) # noqa
347349

348350
async def login(self, **kwargs) -> t.LoginResponse: # type: ignore[no-untyped-def, override]
349351
"""
@@ -407,7 +409,9 @@ async def get_user_info(self, **kwargs) -> t.DictStrAny: # type: ignore[no-unty
407409

408410
return await self._get(self.USER_INFO_URL, signed=True, **kwargs)
409411

410-
async def get_currencies_info(self, page: int = 1, **kwargs) -> t.DictStrAny: # type: ignore[no-untyped-def, override]
412+
async def get_currencies_info( # type: ignore[no-untyped-def, override]
413+
self, page: int = 1, **kwargs
414+
) -> t.DictStrAny:
411415
"""
412416
Get currencies info.
413417
@@ -490,7 +494,9 @@ async def get_orderbook( # type: ignore[no-untyped-def, override]
490494
self.ORDERBOOK_URL.format(market_id, str(type)), version=self.PUBLIC_API_VERSION_2, **kwargs
491495
)
492496

493-
async def get_recent_trades(self, market_id: int, **kwargs) -> t.TradeResponse: # type: ignore[no-untyped-def, override]
497+
async def get_recent_trades( # type: ignore[no-untyped-def, override]
498+
self, market_id: int, **kwargs
499+
) -> t.TradeResponse:
494500
"""
495501
Get recent trades.
496502
@@ -579,7 +585,9 @@ async def create_order( # type: ignore[no-untyped-def, override]
579585
kwargs["json"] = {k: v for k, v in locals().items() if v is not None and k not in ("self", "kwargs")}
580586
return await self._post(self.ORDERS_URL, signed=True, **kwargs) # type: ignore[return-value]
581587

582-
async def cancel_order(self, order_id: str, **kwargs) -> t.CancelOrderResponse: # type: ignore[no-untyped-def, override]
588+
async def cancel_order( # type: ignore[no-untyped-def, override]
589+
self, order_id: str, **kwargs
590+
) -> t.CancelOrderResponse:
583591
"""
584592
Cancel order.
585593

src/bitpin/clients/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from .core import CoreClient
88
from .. import types as t
99
from .. import enums
10-
from ..exceptions import APIException, RequestException
10+
from ..exceptions import (
11+
APIException,
12+
RequestException,
13+
)
1114

1215

1316
class Client(CoreClient):
@@ -251,8 +254,7 @@ def _handle_login(self) -> None:
251254
"""Handle login."""
252255

253256
if self.api_key and self.api_secret:
254-
if self.refresh_token is None or self.access_token is None:
255-
self.login()
257+
self.login()
256258

257259
if self._background_relogin:
258260
Thread(target=self._background_relogin_task, daemon=True).start()
@@ -265,8 +267,8 @@ def _background_relogin_task(self) -> None:
265267

266268
while True:
267269
try:
268-
time.sleep(self._background_relogin_interval)
269270
self.login()
271+
time.sleep(self._background_relogin_interval)
270272
except Exception: # pylint: disable=broad-except
271273
continue
272274

@@ -275,8 +277,8 @@ def _background_refresh_token_task(self) -> None:
275277

276278
while True:
277279
try:
278-
time.sleep(self._background_refresh_token_interval)
279280
self.refresh_access_token()
281+
time.sleep(self._background_refresh_token_interval)
280282
except Exception: # pylint: disable=broad-except
281283
continue
282284

0 commit comments

Comments
 (0)