mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2026-02-04 10:14:15 +02:00
🔧 Fix unclosed OAuth2Session objects. Make use of the _make_session context manager.
This commit is contained in:
@@ -64,12 +64,12 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
async def _fetch_token(self, state):
|
async def _fetch_token(self, state):
|
||||||
discord = await self._make_session(state=state)
|
async with await self._make_session(state=state) as discord:
|
||||||
return await discord.fetch_token(
|
return await discord.fetch_token(
|
||||||
configs.DISCORD_TOKEN_URL,
|
configs.DISCORD_TOKEN_URL,
|
||||||
client_secret=self.__client_secret,
|
client_secret=self.__client_secret,
|
||||||
authorization_response=request.url
|
authorization_response=request.url
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _make_session(self, token: str = None, state: str = None, scope: list = None) -> OAuth2Session:
|
async def _make_session(self, token: str = None, state: str = None, scope: list = None) -> OAuth2Session:
|
||||||
"""A low level method used for creating OAuth2 session.
|
"""A low level method used for creating OAuth2 session.
|
||||||
@@ -136,20 +136,21 @@ class DiscordOAuth2HttpClient(abc.ABC):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
route = configs.DISCORD_API_BASE_URL + route
|
route = configs.DISCORD_API_BASE_URL + route
|
||||||
discord = await self._make_session()
|
async with await self._make_session() as discord:
|
||||||
async with (await discord.request(
|
async with (
|
||||||
method, route, data, **kwargs
|
await discord.request(method, route, data, **kwargs)
|
||||||
) if oauth else aiohttp.request(method, route, data=data, **kwargs)) as response:
|
if oauth else aiohttp.request(method, route, data=data, **kwargs)
|
||||||
|
) as response:
|
||||||
|
|
||||||
if response.status == 401:
|
if response.status == 401:
|
||||||
raise exceptions.Unauthorized
|
raise exceptions.Unauthorized
|
||||||
if response.status == 429:
|
if response.status == 429:
|
||||||
raise exceptions.RateLimited(response)
|
raise exceptions.RateLimited(response)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return await response.json()
|
return await response.json()
|
||||||
except aiohttp.ContentTypeError:
|
except aiohttp.ContentTypeError:
|
||||||
return await response.text()
|
return await response.text()
|
||||||
|
|
||||||
async def bot_request(self, route: str, method="GET", **kwargs) -> typing.Union[dict, str]:
|
async def bot_request(self, route: str, method="GET", **kwargs) -> typing.Union[dict, str]:
|
||||||
"""Make HTTP request to specified endpoint with bot token as authorization headers.
|
"""Make HTTP request to specified endpoint with bot token as authorization headers.
|
||||||
|
|||||||
@@ -95,8 +95,8 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
|
|
||||||
state = jwt.encode(data or dict(), current_app.config["SECRET_KEY"]).decode(encoding="utf-8")
|
state = jwt.encode(data or dict(), current_app.config["SECRET_KEY"]).decode(encoding="utf-8")
|
||||||
|
|
||||||
discord_session = await self._make_session(scope=scope, state=state)
|
async with await self._make_session(scope=scope, state=state) as discord_:
|
||||||
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
authorization_url, state = discord_.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
|
||||||
|
|
||||||
self.__save_state(state)
|
self.__save_state(state)
|
||||||
|
|
||||||
@@ -174,7 +174,8 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
|||||||
|
|
||||||
async def authorized(self):
|
async def authorized(self):
|
||||||
"""A boolean indicating whether current session has authorization token or not."""
|
"""A boolean indicating whether current session has authorization token or not."""
|
||||||
return (await self._make_session()).authorized
|
async with await self._make_session() as discord_:
|
||||||
|
return discord_.authorized
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def fetch_user() -> models.User:
|
async def fetch_user() -> models.User:
|
||||||
|
|||||||
Reference in New Issue
Block a user