mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-12-01 13:44:16 +00:00
rename sdk_token to pat to follow hub convention
This commit is contained in:
3
openbb_sdk/sdk/core/README.md
vendored
3
openbb_sdk/sdk/core/README.md
vendored
@@ -291,6 +291,9 @@ You can login to your OpenBB Hub account and save your credentials there to acce
|
||||
```python
|
||||
from openbb import obb
|
||||
|
||||
# Login with personal access token
|
||||
obb.account.login(pat="your_pat", remember_me=True) # pragma: allowlist secret
|
||||
|
||||
# Login with email, password or SDK token
|
||||
obb.account.login(email="your_email", password="your_password", remember_me=True) # pragma: allowlist secret
|
||||
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException
|
||||
from jose import JWTError, jwt
|
||||
from jose import JWTError
|
||||
from jose.exceptions import ExpiredSignatureError
|
||||
from jose.jwt import decode, get_unverified_header
|
||||
from openbb_core.app.model.credentials import Credentials
|
||||
from openbb_core.app.model.hub.features_keys import FeaturesKeys
|
||||
from openbb_core.app.model.hub.hub_session import HubSession
|
||||
@@ -34,16 +36,16 @@ class HubService:
|
||||
self,
|
||||
email: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
sdk_token: Optional[str] = None,
|
||||
pat: Optional[str] = None,
|
||||
) -> HubSession:
|
||||
"""Connect to Hub."""
|
||||
if email and password:
|
||||
self._session = self.get_session_from_email_password(email, password)
|
||||
return self._session
|
||||
if sdk_token:
|
||||
self._session = self.get_session_from_sdk_token(sdk_token)
|
||||
if pat:
|
||||
self._session = self.get_session_from_sdk_token(pat)
|
||||
return self._session
|
||||
raise OpenBBError("Please provide 'email' and 'password' or 'sdk_token'")
|
||||
raise OpenBBError("Please provide 'email' and 'password' or 'pat'")
|
||||
|
||||
def disconnect(self) -> bool:
|
||||
"""Disconnect from Hub."""
|
||||
@@ -119,14 +121,14 @@ class HubService:
|
||||
def check_token_expiration(cls, token: str) -> None:
|
||||
"""Check token expiration, raises exception if expired."""
|
||||
try:
|
||||
header_data = jwt.get_unverified_header(token)
|
||||
jwt.decode(
|
||||
header_data = get_unverified_header(token)
|
||||
decode(
|
||||
token,
|
||||
key="secret",
|
||||
algorithms=[header_data["alg"]],
|
||||
options={"verify_signature": False, "verify_exp": True},
|
||||
)
|
||||
except jwt.ExpiredSignatureError as e:
|
||||
except ExpiredSignatureError as e:
|
||||
raise OpenBBError("SDK personal access token expired.") from e
|
||||
except JWTError as e:
|
||||
raise OpenBBError("Failed to decode SDK token.") from e
|
||||
|
||||
@@ -28,10 +28,10 @@ class Account:
|
||||
self,
|
||||
email: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
sdk_token: Optional[str] = None,
|
||||
pat: Optional[str] = None,
|
||||
) -> HubService:
|
||||
"""Create hub service to handle connection."""
|
||||
if email is None and password is None and sdk_token is None:
|
||||
if email is None and password is None and pat is None:
|
||||
session_file = Path(self._openbb_directory, ".sdk_hub_session.json")
|
||||
if not session_file.exists():
|
||||
raise OpenBBError("Session not found.")
|
||||
@@ -43,14 +43,14 @@ class Account:
|
||||
hs = HubService(hub_session)
|
||||
else:
|
||||
hs = HubService()
|
||||
hs.connect(email, password, sdk_token)
|
||||
hs.connect(email, password, pat)
|
||||
return hs
|
||||
|
||||
def login(
|
||||
self,
|
||||
email: Optional[str] = None,
|
||||
password: Optional[str] = None,
|
||||
sdk_token: Optional[str] = None,
|
||||
pat: Optional[str] = None,
|
||||
remember_me: bool = False,
|
||||
) -> UserSettings:
|
||||
"""Login to hub.
|
||||
@@ -61,8 +61,8 @@ class Account:
|
||||
Email address, by default None
|
||||
password : Optional[str], optional
|
||||
Password, by default None
|
||||
sdk_token : Optional[str], optional
|
||||
SDK token, by default None
|
||||
pat : Optional[str], optional
|
||||
Personal access token, by default None
|
||||
remember_me : bool, optional
|
||||
Remember me, by default False
|
||||
|
||||
@@ -71,7 +71,7 @@ class Account:
|
||||
UserSettings
|
||||
User settings: profile, credentials, preferences
|
||||
"""
|
||||
hs = self._create_hub_service(email, password, sdk_token)
|
||||
hs = self._create_hub_service(email, password, pat)
|
||||
incoming = hs.pull()
|
||||
updated = UserService.update_default(incoming)
|
||||
self._container._command_runner_session = CommandRunnerSession(
|
||||
|
||||
@@ -46,7 +46,7 @@ def test_connect_with_sdk_token():
|
||||
HubService, "get_session_from_sdk_token", return_value=mock_hub_session
|
||||
):
|
||||
hub_service = HubService()
|
||||
result = hub_service.connect(sdk_token="sdk_token")
|
||||
result = hub_service.connect(pat="pat")
|
||||
|
||||
assert result == mock_hub_session
|
||||
assert hub_service.session == mock_hub_session
|
||||
@@ -56,7 +56,7 @@ def test_connect_without_credentials():
|
||||
"""Test connect without credentials."""
|
||||
hub_service = HubService()
|
||||
with pytest.raises(
|
||||
OpenBBError, match="Please provide 'email' and 'password' or 'sdk_token'"
|
||||
OpenBBError, match="Please provide 'email' and 'password' or 'pat'"
|
||||
):
|
||||
hub_service.connect()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user