From 24bc6ec52f4063906e1f36a591f1c4d9eeaedad2 Mon Sep 17 00:00:00 2001 From: montezdesousa <79287829+montezdesousa@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:17:39 +0000 Subject: [PATCH] Rename session file (#5771) * rename session file * rename method * Update README.md * placed as note * revert readme change --------- Co-authored-by: Pratyush Shukla --- openbb_platform/README.md | 3 +++ .../platform/core/openbb_core/app/service/hub_service.py | 4 ++-- .../platform/core/openbb_core/app/static/account.py | 8 +++++--- .../platform/core/tests/app/service/test_hub_service.py | 6 +++--- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/openbb_platform/README.md b/openbb_platform/README.md index 008cb09286b..1b25f89ee66 100644 --- a/openbb_platform/README.md +++ b/openbb_platform/README.md @@ -66,6 +66,9 @@ Set your keys at [OpenBB Hub](https://my.openbb.co/app/sdk/api-keys) and get you ```python >>> from openbb import obb >>> openbb.account.login(pat="OPENBB_PAT") + +>>> # Persist changes in OpenBB Hub +>>> obb.account.save() ``` ### 2. Runtime diff --git a/openbb_platform/platform/core/openbb_core/app/service/hub_service.py b/openbb_platform/platform/core/openbb_core/app/service/hub_service.py index 882ecf1ff43..e9a95011cd9 100644 --- a/openbb_platform/platform/core/openbb_core/app/service/hub_service.py +++ b/openbb_platform/platform/core/openbb_core/app/service/hub_service.py @@ -51,7 +51,7 @@ class HubService: self._session = self._get_session_from_email_password(email, password) return self._session if pat: - self._session = self._get_session_from_sdk_token(pat) + self._session = self._get_session_from_platform_token(pat) return self._session raise OpenBBError("Please provide 'email' and 'password' or 'pat'") @@ -123,7 +123,7 @@ class HubService: detail = response.json().get("detail", None) raise HTTPException(status_code, detail) - def _get_session_from_sdk_token(self, token: str) -> HubSession: + def _get_session_from_platform_token(self, token: str) -> HubSession: """Get session from Platform personal access token.""" if not token: raise OpenBBError("Platform personal access token not found.") diff --git a/openbb_platform/platform/core/openbb_core/app/static/account.py b/openbb_platform/platform/core/openbb_core/app/static/account.py index f6d605bdd2d..dc9a2837b54 100644 --- a/openbb_platform/platform/core/openbb_core/app/static/account.py +++ b/openbb_platform/platform/core/openbb_core/app/static/account.py @@ -23,6 +23,8 @@ class Account: save refresh""" + SESSION_FILE = ".hub_session.json" + def __init__(self, base_app: "BaseApp"): self._base_app = base_app self._openbb_directory = ( @@ -66,7 +68,7 @@ class Account: ) -> HubService: """Create hub service to handle connection.""" if email is None and password is None and pat is None: - session_file = Path(self._openbb_directory, ".sdk_hub_session.json") + session_file = Path(self._openbb_directory, self.SESSION_FILE) if not session_file.exists(): raise OpenBBError("Session not found.") @@ -112,7 +114,7 @@ class Account: self._base_app._command_runner.user_settings = updated if remember_me: Path(self._openbb_directory).mkdir(parents=False, exist_ok=True) - session_file = Path(self._openbb_directory, ".sdk_hub_session.json") + session_file = Path(self._openbb_directory, self.SESSION_FILE) with open(session_file, "w") as f: if not hs.session: raise OpenBBError("Not connected to hub.") @@ -178,7 +180,7 @@ class Account: hs = HubService(hub_session) hs.disconnect() - session_file = Path(self._openbb_directory, ".sdk_hub_session.json") + session_file = Path(self._openbb_directory, self.SESSION_FILE) if session_file.exists(): session_file.unlink() diff --git a/openbb_platform/platform/core/tests/app/service/test_hub_service.py b/openbb_platform/platform/core/tests/app/service/test_hub_service.py index 1fa1e439241..e649720b766 100644 --- a/openbb_platform/platform/core/tests/app/service/test_hub_service.py +++ b/openbb_platform/platform/core/tests/app/service/test_hub_service.py @@ -47,7 +47,7 @@ def test_connect_with_sdk_token(): with patch( "requests.post", return_value=MagicMock(status_code=200, json=lambda: {}) ), patch.object( - HubService, "_get_session_from_sdk_token", return_value=mock_hub_session + HubService, "_get_session_from_platform_token", return_value=mock_hub_session ): hub_service = HubService() result = hub_service.connect(pat="pat") @@ -86,7 +86,7 @@ def test_get_session_from_email_password(): assert isinstance(result, HubSession) -def test_get_session_from_sdk_token(): +def test_get_session_from_platform_token(): """Test get session from Platform personal access token.""" with patch( @@ -112,7 +112,7 @@ def test_get_session_from_sdk_token(): "-JLZnFzyJ6dlHBZnkjQT2tfaaefxnTdAlSmToQwxGykvuatmI7L0wztPQ" ) - result = HubService()._get_session_from_sdk_token(mock_token) + result = HubService()._get_session_from_platform_token(mock_token) assert isinstance(result, HubSession)