mirror of
https://github.com/yawaflua/Flask-Discord.git
synced 2025-12-10 04:19:31 +02:00
Add MANY property to automatically return list from fetch_from_api when many of these models exists
This commit is contained in:
@@ -72,7 +72,8 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
||||
"""A boolean indicating whether current session has authorization token or not."""
|
||||
return self._make_session().authorized
|
||||
|
||||
def fetch_user(self) -> models.User:
|
||||
@staticmethod
|
||||
def fetch_user() -> models.User:
|
||||
"""This method requests for data of current user from discord and returns user object.
|
||||
|
||||
Returns
|
||||
@@ -80,9 +81,10 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
||||
flask_discord.models.User
|
||||
|
||||
"""
|
||||
return models.User(self.request("/users/@me"))
|
||||
return models.User.fetch_from_api()
|
||||
|
||||
def fetch_connections(self) -> list:
|
||||
@staticmethod
|
||||
def fetch_connections() -> list:
|
||||
"""Requests and returns connections of current user from discord.
|
||||
|
||||
Returns
|
||||
@@ -91,10 +93,10 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
||||
List of :py:class:`flask_discord.models.UserConnection` objects.
|
||||
|
||||
"""
|
||||
connections_payload = self.request("/users/@me/connections")
|
||||
return [models.UserConnection(payload) for payload in connections_payload]
|
||||
return models.UserConnection.fetch_from_api()
|
||||
|
||||
def fetch_guilds(self) -> list:
|
||||
@staticmethod
|
||||
def fetch_guilds() -> list:
|
||||
"""Requests and returns guilds of current user from discord.
|
||||
|
||||
Returns
|
||||
@@ -103,5 +105,4 @@ class DiscordOAuth2Session(_http.DiscordOAuth2HttpClient):
|
||||
List of :py:class:`flask_discord.models.Guild` objects.
|
||||
|
||||
"""
|
||||
guilds_payload = self.request("/users/@me/guilds")
|
||||
return [models.Guild(payload) for payload in guilds_payload]
|
||||
return models.Guild.fetch_from_api()
|
||||
|
||||
@@ -14,6 +14,8 @@ class DiscordModelsMeta(ABCMeta):
|
||||
|
||||
class DiscordModelsBase(metaclass=DiscordModelsMeta):
|
||||
|
||||
MANY = False
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, payload):
|
||||
self._payload = payload
|
||||
@@ -28,8 +30,21 @@ class DiscordModelsBase(metaclass=DiscordModelsMeta):
|
||||
|
||||
@classmethod
|
||||
def fetch_from_api(cls):
|
||||
"""A class method which returns instance of this model by implicitly making an API call to Discord."""
|
||||
return cls(cls._request(cls.ROUTE))
|
||||
"""A class method which returns an instance or list of instances of this model by implicitly making an
|
||||
API call to Discord.
|
||||
|
||||
Returns
|
||||
-------
|
||||
cls
|
||||
An instance of this model itself.
|
||||
[cls, ...]
|
||||
List of instances of this model when many of these models exist.
|
||||
|
||||
"""
|
||||
payload = cls._request(cls.ROUTE)
|
||||
if cls.MANY:
|
||||
return [cls(_) for _ in payload]
|
||||
return cls(payload)
|
||||
|
||||
def to_json(self):
|
||||
"""A utility method which returns raw payload object as it was received from discord.
|
||||
|
||||
@@ -78,6 +78,7 @@ class UserConnection(DiscordModelsBase):
|
||||
|
||||
"""
|
||||
|
||||
MANY = True
|
||||
ROUTE = "/users/@me/connections"
|
||||
|
||||
def __init__(self, payload):
|
||||
|
||||
@@ -21,6 +21,7 @@ class Guild(DiscordModelsBase):
|
||||
|
||||
"""
|
||||
|
||||
MANY = True
|
||||
ROUTE = "/users/@me/guilds"
|
||||
|
||||
def __init__(self, payload):
|
||||
|
||||
Reference in New Issue
Block a user