@@ -69,6 +69,7 @@ def create_session():
69
69
scope = scope ,
70
70
redirect_uri = redirect_uri ,
71
71
)
72
+ return session
72
73
73
74
# Generate correct URLs
74
75
auth_url = base_url + "oauth2/authorize"
@@ -101,8 +102,16 @@ def api_call(command, *args):
101
102
"""
102
103
return api_base_url + api_calls [command ].format (* args )
103
104
105
+ def check_for_active_session ():
106
+ """ Checks if a session object has been created and returns an Error otherwise."""
107
+ if session is None :
108
+ raise RuntimeError ("Session is not yet initialized, use connection."
109
+ "session = connection.create_session()" )
110
+
111
+
104
112
#%%--------------------------- Oauth communiucation ----------------------------
105
113
114
+
106
115
def get_authorization_url ():
107
116
""" Generate the URL at which an OAuth2 token for the OSF can be requested
108
117
with which OpenSesame can be allowed access to the user's account.
@@ -111,23 +120,28 @@ def get_authorization_url():
111
120
-------
112
121
string : The complete uri for the api endpoint
113
122
"""
123
+ check_for_active_session ()
114
124
return session .authorization_url (auth_url )
115
125
116
126
def parse_token_from_url (url ):
117
127
""" Parse token from url fragment """
128
+ check_for_active_session ()
118
129
token = session .token_from_fragment (url )
119
130
# Call logged_in function to notify event listeners that user is logged in
120
131
if is_authorized ():
121
132
return token
122
133
else :
123
134
logging .debug ("ERROR: Token received, but user not authorized" )
124
135
136
+
125
137
def is_authorized ():
126
138
""" Convenience function simply returning OAuth2Session.authorized. """
139
+ check_for_active_session ()
127
140
return session .authorized
128
141
129
142
def token_valid ():
130
143
""" Checks if OAuth token is present, and if so, if it has not expired yet. """
144
+ check_for_active_session ()
131
145
if not hasattr (session ,"token" ) or not session .token :
132
146
return False
133
147
return session .token ["expires_at" ] > time .time ()
@@ -204,6 +218,7 @@ def func_wrapper(*args, **kwargs):
204
218
def logout ():
205
219
""" Logs out the user, and resets the global session object. """
206
220
global session
221
+ check_for_active_session ()
207
222
resp = session .post (logout_url ,{
208
223
"token" : session .access_token
209
224
})
0 commit comments