1212 datefmt = "%Y-%m-%d %H:%M:%S" ,
1313)
1414
15- logger = logging .getLogger (__name__ )
16- logger .setLevel (level = "DEBUG" )
15+ logger = logging .getLogger ("sumo.wrapper" )
1716
1817TENANT = "3aa4a235-b6e2-48d5-9195-7fcf05b459b0"
1918
@@ -30,6 +29,7 @@ def __init__(
3029 authority = AUTHORITY_URI ,
3130 client_credentials = None ,
3231 writeback = False ,
32+ verbosity = "CRITICAL" ,
3333 ):
3434
3535 logger .debug ("Initialize Auth" )
@@ -62,13 +62,22 @@ def __init__(
6262 if self ._cache_available ():
6363 if not self .accounts :
6464 logger .debug ("Token cache found but have no accounts" )
65- raise RuntimeError (
66- "The locally stored token has no accounts. "
67- "Please check your access or run 'sumo_login' to re-create your token."
68- )
65+ if self .writeback :
66+ logger .debug ("Writeback is True, running device_code" )
67+ self ._oauth_device_code ()
68+ else :
69+ raise RuntimeError (
70+ "The locally stored token has no accounts. "
71+ "Please check your access or run 'sumo_login' to re-create your token."
72+ )
6973 else :
7074 logger .debug ("There are accounts. Calling _oauth_get_token_silent()" )
71- self ._oauth_get_token_silent ()
75+ if not self ._oauth_get_token_silent ():
76+ logger .debug ("self._oauth_get_token_silent returned False" )
77+ if self .writeback :
78+ logger .debug ("self.writeback is True, calling device_code" )
79+ self ._oauth_device_code ()
80+
7281 else :
7382 logger .debug ("No token cache found, reauthenticate" )
7483 self ._oauth_device_code ()
@@ -117,8 +126,10 @@ def _oauth_get_token_silent(self):
117126 elif "error" in self .result :
118127 logger .info ("Error getting access token" )
119128 logger .debug (self .result ["error" ])
129+ return False
120130 else :
121131 logger .info ("Failed getting access token" )
132+ return False
122133
123134 self ._set_expiring_date (int (self .result ["expires_in" ]))
124135
@@ -127,6 +138,8 @@ def _oauth_get_token_silent(self):
127138
128139 logger .debug ("_oauth_get_token_silent() has finished" )
129140
141+ return True
142+
130143 def _set_expiring_date (self , time_left , threshold = 60 ):
131144 """
132145 Defines the access token expiring date. Sets a threshold to update the token before it expires
@@ -163,7 +176,7 @@ def _oauth_device_code(self):
163176 "Fail to create device flow. Err: %s" % json .dumps (flow , indent = 4 )
164177 )
165178 else :
166- logger . debug ( "flow[message] is %s" , flow ["message" ])
179+ print ( flow ["message" ])
167180
168181 self .result = self .app .acquire_token_by_device_flow (flow )
169182 try :
0 commit comments