Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYNPY-1186] When a username is a number, getUserProfile cannot retrieve the user #1014

Merged
70 changes: 70 additions & 0 deletions synapseclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,76 @@ def invalidateAPIKey(self):
if self._is_logged_in():
self.restDELETE("/secretKey", endpoint=self.authEndpoint)

@functools.lru_cache()
def get_user_name_profile(
BWMac marked this conversation as resolved.
Show resolved Hide resolved
self,
id: Union[str, UserProfile, TeamMember] = None,
sessionToken: str = None,
) -> UserProfile:
"""
Get the details about a Synapse user.
Retrieves information on the current user if 'id' is omitted or is empty string.
:param id: The userName, UserProfile, or TeamMember of a user
:param sessionToken: The session token to use to find the user profile
:returns: The user profile for the user of interest.

Example::
my_profile = syn.get_user_name_profile()
BWMac marked this conversation as resolved.
Show resolved Hide resolved
freds_profile = syn.get_user_name_profile('fredcommo')
"""
if id:
if isinstance(id, collections.abc.Mapping) and "ownerId" in id:
id = id.ownerId
elif isinstance(id, TeamMember):
id = id.member.ownerId
elif isinstance(id, str):
principals = self._findPrincipals(id)
if len(principals) == 1:
id = principals[0]["ownerId"]
else:
for principal in principals:
if principal.get("userName", None).lower() == id.lower():
id = principal["ownerId"]
break
else: # no break
raise ValueError('Can\'t find user "%s": ' % id)
else:
raise TypeError("id must be a string, UserProfile, or TeamMember")
else:
id = ""
uri = f"/userProfile/{id}"
return UserProfile(
**self.restGET(
uri, headers={"sessionToken": sessionToken} if sessionToken else None
)
)

@functools.lru_cache()
def get_user_id_profile(
self,
id: int = None,
sessionToken: str = None,
) -> UserProfile:
"""
Get the details about a Synapse user.
Retrieves information on the current user if 'id' is omitted or is empty string.
:param id: The ownerId of a user
BryanFauble marked this conversation as resolved.
Show resolved Hide resolved
:param sessionToken: The session token to use to find the user profile
:returns: The user profile for the user of interest.

Example::
my_profile = syn.getUserProfile()
BWMac marked this conversation as resolved.
Show resolved Hide resolved
freds_profile = syn.getUserProfile('fredcommo')
BWMac marked this conversation as resolved.
Show resolved Hide resolved
"""
if not id:
BWMac marked this conversation as resolved.
Show resolved Hide resolved
id = ""
uri = f"/userProfile/{id}"
return UserProfile(
**self.restGET(
uri, headers={"sessionToken": sessionToken} if sessionToken else None
)
)

@functools.lru_cache()
BWMac marked this conversation as resolved.
Show resolved Hide resolved
def getUserProfile(
self,
Expand Down
Loading