Skip to content

Commit

Permalink
Improve get_deferred_field and GroupChatMember
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tylertian123 committed Nov 2, 2020
1 parent ab3f9f9 commit 11ca311
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pyryver/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand Down Expand Up @@ -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:
"""
Expand Down

0 comments on commit 11ca311

Please sign in to comment.