Asynchronize the world

This commit is contained in:
thec0sm0s
2020-09-30 13:12:53 +05:30
parent 256d6cae81
commit bc15de5119
9 changed files with 74 additions and 70 deletions

View File

@@ -22,20 +22,20 @@ class DiscordModelsBase(metaclass=DiscordModelsMeta):
self._payload = payload
@staticmethod
def _request(*args, **kwargs):
async def _request(*args, **kwargs):
"""A shorthand to :py:func:quart_discord.request`. It uses Quart current_app local proxy to get the
Quart-Discord client.
"""
return current_app.discord.request(*args, **kwargs)
return await current_app.discord.request(*args, **kwargs)
@staticmethod
def _bot_request(*args, **kwargs):
async def _bot_request(*args, **kwargs):
"""A shorthand to :py:func:quart_discord.bot_request`."""
return current_app.discord.bot_request(*args, **kwargs)
return await current_app.discord.bot_request(*args, **kwargs)
@classmethod
def fetch_from_api(cls):
async def fetch_from_api(cls):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord.
@@ -48,7 +48,7 @@ class DiscordModelsBase(metaclass=DiscordModelsMeta):
"""
request_method = cls._bot_request if cls.BOT else cls._request
payload = request_method(cls.ROUTE)
payload = await request_method(cls.ROUTE)
if cls.MANY:
return [cls(_) for _ in payload]
return cls(payload)

View File

@@ -57,7 +57,7 @@ class UserConnection(DiscordModelsBase):
return bool(self.visibility)
@classmethod
def fetch_from_api(cls, cache=True):
async def fetch_from_api(cls, cache=True):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord. If an instance of :py:class:`quart_discord.User` exists in the users internal cache
who are attached to these connections then, the cached property :py:attr:`quart_discord.User.connections`
@@ -74,7 +74,7 @@ class UserConnection(DiscordModelsBase):
List of instances of :py:class:`quart_discord.UserConnection` to which this user belongs.
"""
connections = super().fetch_from_api()
connections = await super().fetch_from_api()
if cache:
user = current_app.discord.users_cache.get(current_app.discord.user_id)

View File

@@ -66,7 +66,7 @@ class Guild(DiscordModelsBase):
return configs.DISCORD_GUILD_ICON_BASE_URL.format(guild_id=self.id, icon_hash=self.icon_hash)
@classmethod
def fetch_from_api(cls, cache=True):
async def fetch_from_api(cls, cache=True):
"""A class method which returns an instance or list of instances of this model by implicitly making an
API call to Discord. If an instance of :py:class:`quart_discord.User` exists in the users internal cache
who belongs to these guilds then, the cached property :py:attr:`quart_discord.User.guilds` is updated.
@@ -82,7 +82,7 @@ class Guild(DiscordModelsBase):
List of instances of :py:class:`quart_discord.Guild` to which this user belongs.
"""
guilds = super().fetch_from_api()
guilds = await super().fetch_from_api()
if cache:
user = current_app.discord.users_cache.get(current_app.discord.user_id)

View File

@@ -125,7 +125,7 @@ class User(DiscordModelsBase):
return False
@classmethod
def fetch_from_api(cls, guilds=False, connections=False):
async def fetch_from_api(cls, guilds=False, connections=False):
"""A class method which returns an instance of this model by implicitly making an
API call to Discord. The user returned from API will always be cached and update in internal cache.
@@ -144,14 +144,14 @@ class User(DiscordModelsBase):
An instance of this model itself.
[cls, ...]
List of instances of this model when many of these models exist."""
self = super().fetch_from_api()
self = await super().fetch_from_api()
current_app.discord.users_cache.update({self.id: self})
session["DISCORD_USER_ID"] = self.id
if guilds:
self.fetch_guilds()
await self.fetch_guilds()
if connections:
self.fetch_connections()
await self.fetch_connections()
return self
@@ -169,7 +169,7 @@ class User(DiscordModelsBase):
"""
return current_app.discord.users_cache.get(current_app.discord.user_id)
def add_to_guild(self, guild_id) -> dict:
async def add_to_guild(self, guild_id) -> dict:
"""Method to add user to the guild, provided OAuth2 session has already been created with ``guilds.join`` scope.
Parameters
@@ -189,12 +189,12 @@ class User(DiscordModelsBase):
"""
try:
data = {"access_token": current_app.discord.get_authorization_token()["access_token"]}
data = {"access_token": await current_app.discord.get_authorization_token()["access_token"]}
except KeyError:
raise exceptions.Unauthorized
return self._bot_request(f"/guilds/{guild_id}/members/{self.id}", method="PUT", json=data) or dict()
return await self._bot_request(f"/guilds/{guild_id}/members/{self.id}", method="PUT", json=data) or dict()
def fetch_guilds(self) -> list:
async def fetch_guilds(self) -> list:
"""A method which makes an API call to Discord to get user's guilds. It prepares the internal guilds cache
and returns list of all guilds the user is member of.
@@ -204,10 +204,10 @@ class User(DiscordModelsBase):
List of :py:class:`quart_discord.Guilds` instances.
"""
self._guilds = {guild.id: guild for guild in Guild.fetch_from_api(cache=False)}
self._guilds = {guild.id: guild for guild in await Guild.fetch_from_api(cache=False)}
return self.guilds
def fetch_connections(self) -> list:
async def fetch_connections(self) -> list:
"""A method which makes an API call to Discord to get user's connections. It prepares the internal connection
cache and returns list of all connection instances.
@@ -217,7 +217,7 @@ class User(DiscordModelsBase):
A list of :py:class:`quart_discord.UserConnection` instances.
"""
self.connections = UserConnection.fetch_from_api(cache=False)
self.connections = await UserConnection.fetch_from_api(cache=False)
return self.connections