Skip to content

Commit

Permalink
--wip-- [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
tdstein committed Mar 14, 2024
1 parent 3cac2ee commit 7ba9950
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
40 changes: 34 additions & 6 deletions src/posit/connect/users.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from dataclasses import dataclass
from dataclasses import dataclass, asdict
from datetime import datetime
from typing import Callable, List
from typing import Callable, List, Optional

from requests import Session

Expand All @@ -24,7 +24,34 @@ class User:
updated_time: datetime
active_time: datetime
confirmed: bool
locked: bool
is_locked: bool

asdf: Optional[bool]


@property
def locked(self):
from warnings import warn

warn("this is a deprecation notice", DeprecationWarning)
return self.is_locked

@locked.setter
def locked(self, value):
self.locked = value

@classmethod
def from_dict(cls, instance: dict) -> User:
field_names = {"locked": "is_locked"}
instance = {field_names.get(k, k): v for k, v in instance.items()}
return cls(**instance)

def asdict(self) -> dict:
field_names = {"is_locked": "locked"}
return {
**asdict(self),
**{field_names.get(k, k): v for k, v in asdict(self).items()},
}


class Users:
Expand All @@ -37,7 +64,8 @@ def find(
self, filter: Callable[[User], bool] = lambda _: True, page_size=_MAX_PAGE_SIZE
) -> List[User]:
results = Paginator(self.session, self.url, page_size=page_size).get_all()
return [User(**user) for user in results if filter(User(**user))]
users = (User.from_dict(result) for result in results)
return [user for user in users if filter(user)]

def find_one(
self, filter: Callable[[User], bool] = lambda _: True, page_size=_MAX_PAGE_SIZE
Expand All @@ -46,12 +74,12 @@ def find_one(
while pager.total is None or pager.seen < pager.total:
result = pager.get_next_page()
for u in result:
user = User(**u)
user = User.from_dict(u)
if filter(user):
return user
return None

def get(self, id: str) -> User:
url = urls.append_path(self.url, id)
response = self.session.get(url)
return User(**response.json())
return User.from_dict(response.json())
19 changes: 11 additions & 8 deletions tinkering.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from posit.connect import Client

with Client() as client:
print(client.get("v1/users"))
print(client.users.get("f55ca95d-ce52-43ed-b31b-48dc4a07fe13"))

users = client.users
users = users.find(lambda user: user["first_name"].startswith("T"))
users = users.find(lambda user: user["last_name"].startswith("S"))
user = users.find_one(lambda user: user["user_role"] == "administrator")
print(user)
user = client.users.get("f55ca95d-ce52-43ed-b31b-48dc4a07fe13")
print(user.locked)
print(user.is_locked)

users = client.users.find()

# This method of conversion provides forward compatibility against the API as fields are removed. This would require
import pandas

users = (user.asdict() for user in users)
print(pandas.DataFrame(users))

0 comments on commit 7ba9950

Please sign in to comment.