Skip to content

Commit

Permalink
refactor: consistent update method signature
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Mar 22, 2024
1 parent e2cdaef commit 6645d5a
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
13 changes: 12 additions & 1 deletion src/posit/connect/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,18 @@ def update(
...

@overload
def update(self, *args, **kwargs) -> None: ...
def update(self, *args, **kwargs) -> None:
"""
Update the content item.
Args:
*args
**kwargs
Returns:
None
"""
...

def update(self, *args, **kwargs) -> None:
"""
Expand Down
83 changes: 51 additions & 32 deletions src/posit/connect/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import annotations
from typing import List, Optional
from typing import List, Optional, overload


import requests
Expand Down Expand Up @@ -57,39 +57,58 @@ def confirmed(self) -> bool:
def locked(self) -> bool:
return self.get("locked") # type: ignore

def _update(self, body):
if len(body) == 0:
return
response = self.session.put(self.url, json=body)
super().update(**response.json())

def update( # type: ignore
@overload
def update(
self,
# Not all properties are settable, so we enumerate them here
# (also for type-hinting purposes)
email: Optional[str] = None,
username: Optional[str] = None,
first_name: Optional[str] = None,
last_name: Optional[str] = None,
user_role: Optional[str] = None,
# TODO(#100): in the API, this goes via POST /v1/users/{guid}/lock
# accept it here and make that request? Or add a .lock() method?
# locked: Optional[bool] = None,
email: Optional[str] = ...,
username: Optional[str] = ...,
first_name: Optional[str] = ...,
last_name: Optional[str] = ...,
user_role: Optional[str] = ...,
) -> None:
kwargs = {}
if email is not None:
kwargs["email"] = email
if username is not None:
kwargs["username"] = username
if first_name is not None:
kwargs["first_name"] = first_name
if last_name is not None:
kwargs["last_name"] = last_name
if user_role is not None:
kwargs["user_role"] = user_role
# if locked is not None:
# kwargs["locked"] = locked
self._update(kwargs)
"""
Update the user.
Args:
email (Optional[str]): The user's email address.
username (Optional[str]): The user's username.
first_name (Optional[str]): The user's first name.
last_name (Optional[str]): The user's last name.
user_role (Optional[str]): The user's role.
Returns:
None
"""
...

@overload
def update(self, *args, **kwargs) -> None:
"""
Update the user.
Args:
*args
**kwargs
Returns:
None
"""
...

def update(self, *args, **kwargs) -> None:
"""
Update the user.
Args:
*args
**kwargs
Returns:
None
"""
body = dict(*args, **kwargs)
response = self.session.put(self.url, json=body)
super().update(**response.json())


class Users(Resources[User]):
Expand Down

0 comments on commit 6645d5a

Please sign in to comment.