Skip to content

Commit

Permalink
feat: add client.me property that calls v1/user (lazily) (#51)
Browse files Browse the repository at this point in the history
* feat: add client.me property that calls v1/user (lazily)

* No caching, keep it simple
  • Loading branch information
nealrichardson authored Feb 26, 2024
1 parent 26c2730 commit 4e46494
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/posit/connect/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .auth import Auth
from .config import Config
from .users import Users
from .users import User, Users


class Client:
Expand Down Expand Up @@ -35,14 +35,20 @@ def __init__(
# Store the Session object.
self.session = session

# Place to cache the server settings
self.server_settings = None
# Internal attributes to hold settings we fetch lazily
self._server_settings = None

@property
def connect_version(self):
if self.server_settings is None:
self.server_settings = self.get("server_settings").json()
return self.server_settings["version"]
if self._server_settings is None:
self._server_settings = self.get("server_settings").json()
return self._server_settings["version"]

@property
def me(self) -> User:
url = urls.append_path(self.config.url, "v1/user")
response = self.session.get(url)
return User(**response.json())

@property
def users(self) -> Users:
Expand Down
22 changes: 22 additions & 0 deletions src/posit/connect/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ def test_connect_version(self):
)
assert client.connect_version == "2024.01.0"

@responses.activate
def test_me_request(self):
responses.get(
"https://connect.example/__api__/v1/user",
json={
"email": "carlos@connect.example",
"username": "carlos12",
"first_name": "Carlos",
"last_name": "User",
"user_role": "publisher",
"created_time": "2019-09-09T15:24:32Z",
"updated_time": "2022-03-02T20:25:06Z",
"active_time": "2020-05-11T16:58:45Z",
"confirmed": True,
"locked": False,
"guid": "20a79ce3-6e87-4522-9faf-be24228800a4",
},
)

con = Client(api_key="12345", url="https://connect.example/")
assert con.me["username"] == "carlos12"

def test_request(self, MockSession):
api_key = "foobar"
url = "http://foo.bar/__api__"
Expand Down

0 comments on commit 4e46494

Please sign in to comment.