From 11ca31155d6b689ba1421121a51848635795e487 Mon Sep 17 00:00:00 2001 From: Tyler Tian Date: Sun, 1 Nov 2020 23:19:15 -0500 Subject: [PATCH] Improve get_deferred_field and GroupChatMember Improve get_deferred_field to first check if the field already exists in the object and set the field in the object afterwards. Add a get_name() method to GroupChatMember and change as_member() to be async as it needs to get additional data. --- pyryver/objects.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pyryver/objects.py b/pyryver/objects.py index a4d6f45..b00bc52 100644 --- a/pyryver/objects.py +++ b/pyryver/objects.py @@ -313,13 +313,20 @@ async def get_deferred_field(self, field: str, field_type: str) -> typing.Union[ :return: The expanded value of this field as an object or a list of objects. :raises ValueError: When the field cannot be expanded. """ + constructor = TYPES_DICT[field_type] + # First check if the field is present + if field in self._data and "__deferred" not in self._data[field]: + if "results" in self._data[field]: + return [constructor(self._ryver, obj_data) for obj_data in self._data[field]["results"]] + else: + return constructor(self._ryver, self._data[field]) url = self.get_api_url(expand=field, select=field) async with self._ryver._session.get(url) as resp: data = (await resp.json())["d"]["results"][field] if "__deferred" in data: raise ValueError( "Cannot obtain field! The field cannot be expanded for some reason.") - constructor = TYPES_DICT[field_type] + self._data[field] = data # Check if the result should be a list if "results" in data: return [constructor(self._ryver, obj_data) for obj_data in data["results"]] @@ -1680,13 +1687,19 @@ def get_role(self) -> str: """ return self._data["role"] - def as_user(self) -> User: + async def as_user(self) -> User: """ Get this member as a :py:class:`User` object. :return: The member as a ``User`` object. """ - return User(self._ryver, self._data["member"]) + return await self.get_deferred_field("member", TYPE_USER) + + def get_name(self) -> str: + """ + Get the display name of this member. + """ + return self._data["extras"]["displayName"] def is_admin(self) -> bool: """