Skip to content

Commit

Permalink
Merge pull request #3 from thec0sm0s/quart
Browse files Browse the repository at this point in the history
Fix unclosed OAuth2Session objects.
  • Loading branch information
jnawk authored Oct 4, 2020
2 parents 5995e94 + d1f8e1b commit 914b421
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion quart_discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
]


__version__ = "1.0.0"
__version__ = "1.0.1"
41 changes: 21 additions & 20 deletions quart_discord/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ async def get_authorization_token() -> dict:
raise NotImplementedError

async def _fetch_token(self, state):
discord = await self._make_session(state=state)
return await discord.fetch_token(
configs.DISCORD_TOKEN_URL,
client_secret=self.__client_secret,
authorization_response=request.url
)
async with await self._make_session(state=state) as discord:
return await discord.fetch_token(
configs.DISCORD_TOKEN_URL,
client_secret=self.__client_secret,
authorization_response=request.url
)

async def _make_session(self, token: str = None, state: str = None, scope: list = None) -> OAuth2Session:
"""A low level method used for creating OAuth2 session.
Expand Down Expand Up @@ -136,20 +136,21 @@ async def request(self, route: str, method="GET", data=None, oauth=True, **kwarg
"""
route = configs.DISCORD_API_BASE_URL + route
discord = await self._make_session()
async with (await discord.request(
method, route, data, **kwargs
) if oauth else aiohttp.request(method, route, data=data, **kwargs)) as response:

if response.status == 401:
raise exceptions.Unauthorized
if response.status == 429:
raise exceptions.RateLimited(response)

try:
return await response.json()
except aiohttp.ContentTypeError:
return await response.text()
async with await self._make_session() as discord:
async with (
await discord.request(method, route, data, **kwargs)
if oauth else aiohttp.request(method, route, data=data, **kwargs)
) as response:

if response.status == 401:
raise exceptions.Unauthorized
if response.status == 429:
raise exceptions.RateLimited(response)

try:
return await response.json()
except aiohttp.ContentTypeError:
return await response.text()

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.
Expand Down
7 changes: 4 additions & 3 deletions quart_discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ async def create_session(

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)
authorization_url, state = discord_session.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)
async with await self._make_session(scope=scope, state=state) as discord_:
authorization_url, state = discord_.authorization_url(configs.DISCORD_AUTHORIZATION_BASE_URL)

self.__save_state(state)

Expand Down Expand Up @@ -174,7 +174,8 @@ def revoke(self):

async def authorized(self):
"""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
async def fetch_user() -> models.User:
Expand Down

0 comments on commit 914b421

Please sign in to comment.