Skip to content

Commit 56aefcb

Browse files
committed
chore: updated README to give an example implementation that falls back to interactive log in, when trying to re-use the saved session fails
Fixes #54
1 parent 23cec3b commit 56aefcb

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ class WSApiTest:
6969
username = input("Wealthsimple username (email): ")
7070
session = keyring.get_password(f"{keyring_service_name}.{username}", "session")
7171
if session:
72-
session = WSAPISession.from_json(session)
72+
try:
73+
# 3a. Use the save session to instantiate the API object
74+
ws = WealthsimpleAPI.from_token(session, persist_session_fct, username)
75+
# persist_session_fct is needed here, because the session may be updated if the access token expired, and thus this function will be called to save the new session
76+
except Exception as e:
77+
print(f"Error trying to use stored tokens: {e}")
78+
session = None
79+
# Continue below with a regular log-in
80+
7381
if not session:
82+
# 3b. Need to log-in interactively
7483
username = None
7584
password = None
7685
otp_answer = None
@@ -95,10 +104,8 @@ class WSApiTest:
95104
print("Login failed. Try again.")
96105
username = None
97106
password = None
98-
99-
# 3. Use the session object to instantiate the API object
100-
ws = WealthsimpleAPI.from_token(session, persist_session_fct, username)
101-
# persist_session_fct is needed here too, because the session may be updated if the access token expired, and thus this function will be called to save the new session
107+
# Use the new session object to instantiate the API object
108+
ws = WealthsimpleAPI.from_token(session, persist_session_fct, username)
102109

103110
# Optionally define functions to cache market data, if you want transactions' descriptions and accounts balances to show the security's symbol instead of its ID
104111
# eg. sec-s-e7947deb977341ff9f0ddcf13703e9a6 => TSX:XEQT

0 commit comments

Comments
 (0)