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