Skip to content

Commit 629056f

Browse files
committed
Only init session once on HTTP
1 parent 08ce6cc commit 629056f

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

twitchio/authentication/oauth.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from typing import TYPE_CHECKING, ClassVar
3030

3131
from ..http import HTTPClient, Route
32+
from ..utils import MISSING
3233
from .payloads import *
3334

3435

@@ -55,7 +56,7 @@ def __init__(
5556
client_secret: str,
5657
redirect_uri: str | None = None,
5758
scopes: Scopes | None = None,
58-
session: aiohttp.ClientSession | None = None,
59+
session: aiohttp.ClientSession = MISSING,
5960
) -> None:
6061
super().__init__(session=session, client_id=client_id)
6162

twitchio/authentication/tokens.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from ..exceptions import HTTPException, InvalidTokenException
3838
from ..http import HTTPAsyncIterator, PaginatedConverter
3939
from ..types_.tokens import TokenMappingData
40+
from ..utils import MISSING
4041
from .oauth import OAuth
4142
from .payloads import ClientCredentialsPayload, ValidateTokenPayload
4243
from .scopes import Scopes
@@ -61,7 +62,7 @@ def __init__(
6162
client_secret: str,
6263
redirect_uri: str | None = None,
6364
scopes: Scopes | None = None,
64-
session: aiohttp.ClientSession | None = None,
65+
session: aiohttp.ClientSession = MISSING,
6566
nested_key: str | None = None,
6667
) -> None:
6768
super().__init__(

twitchio/http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,12 @@ async def __anext__(self) -> T:
377377

378378

379379
class HTTPClient:
380-
__slots__ = ("_client_id", "_session", "_should_close", "user_agent")
380+
__slots__ = ("_client_id", "_session", "_session_set", "_should_close", "user_agent")
381381

382382
def __init__(self, session: aiohttp.ClientSession = MISSING, *, client_id: str) -> None:
383383
self._session: aiohttp.ClientSession = session
384384
self._should_close: bool = session is MISSING
385+
self._session_set: bool = False
385386

386387
self._client_id: str = client_id
387388

@@ -395,6 +396,11 @@ def headers(self) -> dict[str, str]:
395396
return {"User-Agent": self.user_agent, "Client-ID": self._client_id}
396397

397398
async def _init_session(self) -> None:
399+
if self._session_set:
400+
return
401+
402+
self._session_set = True
403+
398404
if self._session is not MISSING:
399405
self._session.headers.update(self.headers)
400406
return
@@ -408,6 +414,7 @@ def clear(self) -> None:
408414
"Clearing %s session. A new session will be created on the next request.", self.__class__.__qualname__
409415
)
410416
self._session = MISSING
417+
self._session_set = False
411418

412419
async def close(self) -> None:
413420
if not self._should_close:

0 commit comments

Comments
 (0)