Skip to content

Commit 3389733

Browse files
velovixBryceBeagle
andauthored
Add method for GET /api/cloud_user (#19)
Adds a method for an API that allows clients to get information on the currently logged-in cloud user. The server returns a 404 when a user is not logged in, but this method returns None in this case instead. Co-authored-by: ignormies <bryce.beagle@aotu.ai>
1 parent 2196afa commit 3389733

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

brainframe/api/bf_errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ class UnauthorizedTokensError(BaseAPIError):
308308
"""The provided tokens do not correctly authorize a BrainFrame Cloud user"""
309309

310310

311+
@_server_origin_error()
312+
class CloudUserNotFoundError(BaseAPIError):
313+
"""No cloud user is logged in"""
314+
315+
311316
class ServerNotReadyError(BaseAPIError):
312317
"""The client was able to communicate with the server, but the server had
313318
not completed startup or was in an invalid state"""

brainframe/api/stub.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class BrainFrameAPI(stubs.AlertStubMixin,
2222
stubs.PremisesStubMixin,
2323
stubs.UserStubMixin,
2424
stubs.LicenseStubMixIn,
25-
stubs.CloudTokensStubMixin):
25+
stubs.CloudTokensStubMixin,
26+
stubs.CloudUsersStubMixIn):
2627
"""Provides access to BrainFrame API endpoints."""
2728

2829
def __init__(self, server_url=None, credentials: Tuple[str, str] = None):

brainframe/api/stubs/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
from .users import UserStubMixin
1414
from .licenses import LicenseStubMixIn
1515
from .cloud_tokens import CloudTokensStubMixin
16+
from .cloud_users import CloudUsersStubMixIn
1617
from .base_stub import BaseStub
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import Optional
2+
3+
from brainframe.api.bf_codecs import CloudUserInfo
4+
from brainframe.api.bf_errors import CloudUserNotFoundError
5+
6+
from .base_stub import BaseStub, DEFAULT_TIMEOUT
7+
8+
9+
class CloudUsersStubMixIn(BaseStub):
10+
def get_current_cloud_user(self, timeout=DEFAULT_TIMEOUT) \
11+
-> Optional[CloudUserInfo]:
12+
"""Gets information on the cloud user that's currently logged in.
13+
14+
:param timeout: The timeout to use for this request
15+
:return: Information on the cloud user, or None if no user is logged in
16+
"""
17+
req = f"/api/cloud_user"
18+
try:
19+
data, _ = self._get_json(req, timeout=timeout)
20+
except CloudUserNotFoundError:
21+
return None
22+
else:
23+
return CloudUserInfo.from_dict(data)

0 commit comments

Comments
 (0)