Skip to content

Commit 9dc6518

Browse files
get rid of session manager
1 parent 81489af commit 9dc6518

File tree

1 file changed

+104
-117
lines changed

1 file changed

+104
-117
lines changed

kraken/base_api/__init__.py

Lines changed: 104 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -172,109 +172,18 @@ def check_batch_status(self: ErrorHandler, data: dict) -> dict:
172172
return data
173173

174174

175-
class SessionManager:
176-
"""
177-
Manages the requests-based session for the sync Spot and Futures clients.
178-
179-
Kraken rejects requests that are older than a certain time without further
180-
information. To avoid this, the session manager creates a new session
181-
every 5 minutes.
182-
"""
183-
184-
HEADERS: Final[dict] = {"User-Agent": "btschwertfeger/python-kraken-sdk"}
185-
186-
def __init__(
187-
self: SessionManager,
188-
*,
189-
proxy: str | None = None,
190-
max_session_age: int = 300,
191-
) -> None:
192-
"""
193-
Initialize the session manager.
194-
195-
:param max_session_age: Maximum session age in seconds
196-
:type max_session_age: int
197-
:param proxy: Proxy URL
198-
:type proxy: str, optional
199-
"""
200-
self.__proxy: str = proxy
201-
self.__max_session_age: int = max_session_age
202-
self.__session: requests.Session = self.create_new_session()
203-
self.__session_start_time: float = time.time()
204-
205-
def create_new_session(self: SessionManager) -> requests.Session:
206-
"""Create a new session."""
207-
session = requests.Session()
208-
session.headers.update(self.HEADERS)
209-
if self.__proxy is not None:
210-
session.proxies.update(
211-
{
212-
"http": self.__proxy,
213-
"https": self.__proxy,
214-
},
215-
)
216-
return session
217-
218-
def get_session(self: SessionManager) -> requests.Session:
219-
"""Get a valid session, recreating if the current one is too old."""
220-
if time.time() - self.__session_start_time > self.__max_session_age:
221-
self.__session.close() # Close the old session
222-
self.__session = self.create_new_session()
223-
self.__session_start_time = time.time()
224-
return self.__session
225-
226-
227-
class AsyncSessionManager:
228-
"""
229-
Manages aiohttp-based sessions for the async Spot and Futures clients.
230-
231-
Kraken rejects requests that are older than a certain time without further
232-
information. To avoid this, the session manager creates a new session
233-
every 5 minutes.
234-
"""
235-
236-
HEADERS: Final[dict] = {"User-Agent": "btschwertfeger/python-kraken-sdk"}
237-
238-
def __init__(
239-
self: AsyncSessionManager,
240-
*,
241-
proxy: str | None = None,
242-
max_session_age: int = 300,
243-
) -> None:
244-
"""
245-
Initialize the session manager.
246-
247-
:param max_session_age: Maximum session age in seconds
248-
:type max_session_age: int
249-
:param proxy: Proxy URL
250-
:type proxy: str, optional
251-
"""
252-
self.__proxy: str = proxy
253-
self.__max_session_age: int = max_session_age
254-
self.__session: aiohttp.ClientSession = self.create_new_session()
255-
self.__session_start_time: float = time.time()
256-
257-
def create_new_session(self: AsyncSessionManager) -> aiohttp.ClientSession:
258-
"""Create a new session."""
259-
return aiohttp.ClientSession(headers=self.HEADERS, proxy=self.__proxy)
260-
261-
async def get_session(self: AsyncSessionManager) -> aiohttp.ClientSession:
262-
"""Get a valid session, recreating if the current one is too old."""
263-
if time.time() - self.__session_start_time > self.__max_session_age:
264-
await self.__session.close()
265-
self.__session = self.create_new_session()
266-
self.__session_start_time = time.time()
267-
return self.__session
268-
269-
270175
class SpotClient:
271176
"""
272-
This class is the base for all Spot clients, handles un-/signed
273-
requests and returns exception handled results.
177+
This class is the base for all Spot clients, handles un-/signed requests and
178+
returns exception handled results.
274179
275180
If you are facing timeout errors on derived clients, you can make use of the
276181
``TIMEOUT`` attribute to deviate from the default ``10`` seconds.
277182
183+
Kraken sometimes rejects requests that are older than a certain time without
184+
further information. To avoid this, the session manager creates a new
185+
session every 5 minutes.
186+
278187
:param key: Spot API public key (default: ``""``)
279188
:type key: str, optional
280189
:param secret: Spot API secret key (default: ``""``)
@@ -287,6 +196,8 @@ class SpotClient:
287196

288197
URL: str = "https://api.kraken.com"
289198
TIMEOUT: int = 10
199+
MAX_SESSION_AGE: int = 300 # seconds
200+
HEADERS: Final[dict] = {"User-Agent": "btschwertfeger/python-kraken-sdk"}
290201

291202
def __init__( # nosec: B107
292203
self: SpotClient,
@@ -304,8 +215,28 @@ def __init__( # nosec: B107
304215
self._secret: str = secret
305216
self._use_custom_exceptions: bool = use_custom_exceptions
306217
self._err_handler: ErrorHandler = ErrorHandler()
307-
self.session_manager: SessionManager = SessionManager(proxy=proxy)
308-
self.__session: requests.Session = self.session_manager.create_new_session()
218+
self.__proxy: str | None = proxy
219+
self.__session_start_time: float
220+
self.__create_new_session()
221+
222+
def __create_new_session(self: SpotClient) -> None:
223+
"""Create a new session."""
224+
self.__session = requests.Session()
225+
self.__session.headers.update(self.HEADERS)
226+
if self.__proxy is not None:
227+
self.__session.proxies.update(
228+
{
229+
"http": self.__proxy,
230+
"https": self.__proxy,
231+
},
232+
)
233+
self.__session_start_time = time.time()
234+
235+
def __check_renew_session(self: SpotClient) -> None:
236+
"""Check if the session is too old and renew if necessary."""
237+
if time.time() - self.__session_start_time > self.MAX_SESSION_AGE:
238+
self.__session.close() # Close the old session
239+
self.__create_new_session()
309240

310241
def _prepare_request(
311242
self: SpotClient,
@@ -428,7 +359,7 @@ def request( # noqa: PLR0913 # pylint: disable=too-many-arguments
428359
)
429360

430361
timeout: int = self.TIMEOUT if timeout != 10 else timeout # type: ignore[no-redef]
431-
self.__session = self.session_manager.get_session()
362+
self.__check_renew_session()
432363

433364
if method in {"GET", "DELETE"}:
434365
return self.__check_response_data(
@@ -558,6 +489,10 @@ class SpotAsyncClient(SpotClient):
558489
If you are facing timeout errors on derived clients, you can make use of the
559490
``TIMEOUT`` attribute to deviate from the default ``10`` seconds.
560491
492+
Kraken sometimes rejects requests that are older than a certain time without
493+
further information. To avoid this, the session manager creates a new
494+
session every 5 minutes.
495+
561496
:param key: Spot API public key (default: ``""``)
562497
:type key: str, optional
563498
:param secret: Spot API secret key (default: ``""``)
@@ -583,10 +518,21 @@ def __init__( # nosec: B107
583518
url=url,
584519
use_custom_exceptions=use_custom_exceptions,
585520
)
586-
self.session_manager: AsyncSessionManager = AsyncSessionManager(proxy=proxy) # type: ignore[assignment]
587-
self.__session: aiohttp.ClientSession = (
588-
self.session_manager.create_new_session()
589-
)
521+
self.__proxy: str | None = proxy
522+
self.__session_start_time: float
523+
self.__session: aiohttp.ClientSession
524+
self.__create_new_session()
525+
526+
def __create_new_session(self: SpotAsyncClient) -> None:
527+
"""Create a new session."""
528+
self.__session = aiohttp.ClientSession(headers=self.HEADERS, proxy=self.__proxy)
529+
self.__session_start_time = time.time()
530+
531+
async def __check_renew_session(self: SpotAsyncClient) -> None:
532+
"""Check if the session is too old and renew if necessary."""
533+
if time.time() - self.__session_start_time > self.MAX_SESSION_AGE:
534+
await self.__session.close() # Close the old session
535+
self.__create_new_session()
590536

591537
async def request( # type: ignore[override] # pylint: disable=invalid-overridden-method,too-many-arguments # noqa: PLR0913
592538
self: SpotAsyncClient,
@@ -642,7 +588,7 @@ async def request( # type: ignore[override] # pylint: disable=invalid-overridde
642588
extra_params=extra_params,
643589
)
644590
timeout: int = self.TIMEOUT if timeout != 10 else timeout # type: ignore[no-redef]
645-
self.__session = await self.session_manager.get_session()
591+
await self.__check_renew_session()
646592

647593
if method in {"GET", "DELETE"}:
648594
return await self.__check_response_data( # type: ignore[return-value]
@@ -731,22 +677,28 @@ class NFTClient(SpotClient):
731677

732678
class FuturesClient:
733679
"""
734-
The base class for all Futures clients handles un-/signed requests
735-
and returns exception handled results.
680+
The base class for all Futures clients handles un-/signed requests and
681+
returns exception handled results.
736682
737683
If you are facing timeout errors on derived clients, you can make use of the
738684
``TIMEOUT`` attribute to deviate from the default ``10`` seconds.
739685
740686
If the sandbox environment is chosen, the keys must be generated from here:
741687
https://demo-futures.kraken.com/settings/api
742688
689+
Kraken sometimes rejects requests that are older than a certain time without
690+
further information. To avoid this, the session manager creates a new
691+
session every 5 minutes.
692+
743693
:param key: Futures API public key (default: ``""``)
744694
:type key: str, optional
745695
:param secret: Futures API secret key (default: ``""``)
746696
:type secret: str, optional
747-
:param url: The URL to access the Futures Kraken API (default: https://futures.kraken.com)
697+
:param url: The URL to access the Futures Kraken API (default:
698+
https://futures.kraken.com)
748699
:type url: str, optional
749-
:param sandbox: If set to ``True`` the URL will be https://demo-futures.kraken.com (default: ``False``)
700+
:param sandbox: If set to ``True`` the URL will be
701+
https://demo-futures.kraken.com (default: ``False``)
750702
:type sandbox: bool, optional
751703
:param proxy: proxy URL, may contain authentication information
752704
:type proxy: str, optional
@@ -755,6 +707,8 @@ class FuturesClient:
755707
URL: str = "https://futures.kraken.com"
756708
SANDBOX_URL: str = "https://demo-futures.kraken.com"
757709
TIMEOUT: int = 10
710+
HEADERS: Final[dict] = {"User-Agent": "btschwertfeger/python-kraken-sdk"}
711+
MAX_SESSION_AGE: int = 300 # seconds
758712

759713
def __init__( # nosec: B107
760714
self: FuturesClient,
@@ -780,8 +734,30 @@ def __init__( # nosec: B107
780734
self._use_custom_exceptions: bool = use_custom_exceptions
781735

782736
self._err_handler: ErrorHandler = ErrorHandler()
783-
self.session_manager: SessionManager = SessionManager(proxy=proxy)
784-
self.__session: requests.Session = self.session_manager.create_new_session()
737+
738+
self.__proxy: str | None = proxy
739+
self.__session_start_time: float
740+
self.__session: aiohttp.ClientSession
741+
self.__create_new_session()
742+
743+
def __create_new_session(self: FuturesClient) -> None:
744+
"""Create a new session."""
745+
self.__session = requests.Session()
746+
self.__session.headers.update(self.HEADERS)
747+
if self.__proxy is not None:
748+
self.__session.proxies.update(
749+
{
750+
"http": self.__proxy,
751+
"https": self.__proxy,
752+
},
753+
)
754+
self.__session_start_time = time.time()
755+
756+
def __check_renew_session(self: FuturesClient) -> None:
757+
"""Check if the session is too old and renew if necessary."""
758+
if time.time() - self.__session_start_time > self.MAX_SESSION_AGE:
759+
self.__session.close() # Close the old session
760+
self.__create_new_session()
785761

786762
def _prepare_request(
787763
self: FuturesClient,
@@ -887,7 +863,7 @@ def request( # pylint: disable=too-many-arguments
887863
extra_params=extra_params,
888864
)
889865
timeout: int = self.TIMEOUT if timeout == 10 else timeout # type: ignore[no-redef]
890-
self.__session = self.session_manager.get_session()
866+
self.__check_renew_session()
891867

892868
if method in {"GET", "DELETE"}:
893869
return self.__check_response_data(
@@ -1050,10 +1026,21 @@ def __init__( # nosec: B107
10501026
sandbox=sandbox,
10511027
use_custom_exceptions=use_custom_exceptions,
10521028
)
1053-
self.session_manager: AsyncSessionManager = AsyncSessionManager(proxy=proxy) # type: ignore[assignment]
1054-
self.__session: aiohttp.ClientSession = (
1055-
self.session_manager.create_new_session()
1056-
)
1029+
self.__proxy: str | None = proxy
1030+
self.__session_start_time: float
1031+
self.__session: aiohttp.ClientSession
1032+
self.__create_new_session()
1033+
1034+
def __create_new_session(self: FuturesAsyncClient) -> None:
1035+
"""Create a new session."""
1036+
self.__session = aiohttp.ClientSession(headers=self.HEADERS, proxy=self.__proxy)
1037+
self.__session_start_time = time.time()
1038+
1039+
async def __check_renew_session(self: FuturesAsyncClient) -> None:
1040+
"""Check if the session is too old and renew if necessary."""
1041+
if time.time() - self.__session_start_time > self.MAX_SESSION_AGE:
1042+
await self.__session.close() # Close the old session
1043+
self.__create_new_session()
10571044

10581045
async def request( # type: ignore[override] # pylint: disable=arguments-differ,invalid-overridden-method
10591046
self: FuturesAsyncClient,
@@ -1075,7 +1062,7 @@ async def request( # type: ignore[override] # pylint: disable=arguments-differ,
10751062
)
10761063

10771064
timeout = self.TIMEOUT if timeout != 10 else timeout
1078-
self.__session = await self.session_manager.get_session()
1065+
await self.__check_renew_session()
10791066

10801067
if method in {"GET", "DELETE"}:
10811068
return await self.__check_response_data(

0 commit comments

Comments
 (0)