Add MANY property to automatically return list from fetch_from_api when many of these models exists

This commit is contained in:
thec0sm0s
2020-05-12 12:35:09 +05:30
parent 6365ab461a
commit 782d09246f
4 changed files with 28 additions and 10 deletions

View File

@@ -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.

View File

@@ -78,6 +78,7 @@ class UserConnection(DiscordModelsBase):
"""
MANY = True
ROUTE = "/users/@me/connections"
def __init__(self, payload):

View File

@@ -21,6 +21,7 @@ class Guild(DiscordModelsBase):
"""
MANY = True
ROUTE = "/users/@me/guilds"
def __init__(self, payload):