@@ -222,52 +222,35 @@ def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = No
222222 if self .version :
223223 s , m , i = self .version .split ("." )
224224 success = False
225-
226- if secret and (int (s ) < 3 or (int (s ) == 3 and int (m ) < 5 )):
225+
226+ if not (secret ) or self .graphname :
227+ if self .graphname :
228+ _json = {"graph" : self .graphname }
227229 try :
228- # /gsql/v1/tokens endpoint only supported on version >=4.1 and replaced /requesttoken
229- _json = {"secret" : secret , "graph" : self .graphname }
230- if lifetime :
231- _json ["lifetime" ] = str (lifetime )
232- res = requests .request ("POST" , self .gsUrl +
233- "/gsql/v1/tokens" , verify = False , json = _json , headers = {"X-User-Agent" : "pyTigerGraph" })
230+ res = self ._post (self .restppUrl + "/requesttoken" , authMode = "pwd" , data = str (_json ), resKey = "results" )
231+ mainVer = 3
234232
235- # if /gsql/v1/tokens endpoint doesn't exist then try old endpoint
236- if res .status_code == 404 :
237- res = requests .request ("GET" , self .restppUrl +
238- "/requesttoken?secret=" + secret +
239- ("&lifetime=" + str (lifetime ) if lifetime else "" ), verify = False )
240- mainVer = 3 # Can't use _verGreaterThan4_0 to check version since you need to set a token for that
241- else :
242- mainVer = 4
243- res = json .loads (res .text )
244-
245- if not res ["error" ]:
246- success = True
233+ # The old endpoint doesn't exist (since on TigerGraph Ver >=4.1). Use new endpoint
234+ # have to handle it in this order since _req changes the url to the new url path if first request fails
247235 except Exception as e :
248- raise e
249- elif not (success ) and not (secret ):
250- _json = {"graph" : self .graphname }
251- try :
252- res = self ._post (self .gsUrl +
253- "/gsql/v1/tokens" , data = _json , authMode = "pwd" , jsonData = True , resKey = None )
254- mainVer = 4
255-
256- # The new endpoint doesn't exist (since on TigerGraph Ver <4.1). Use old endpoint
257- except requests .exceptions .HTTPError as e :
258- if e .response .status_code == 404 :
259- res = self ._post (self .restppUrl + "/requesttoken" , authMode = "pwd" , data = str ({"graph" : self .graphname }), resKey = "results" )
260- mainVer = 3
261- if e .response .status_code == 400 :
262- raise TigerGraphException ("Error requesting token. Check if the connection's graphname is correct." , 400 )
263- else :
264- raise e
236+ try :
237+ res = self ._post (self .gsUrl + "/gsql/v1/tokens" ,
238+ data = _json ,
239+ authMode = "pwd" ,
240+ jsonData = True ,
241+ resKey = None )
242+ mainVer = 4
243+ except requests .exceptions .HTTPError as e :
244+ if e .response .status_code == 404 :
245+ raise TigerGraphException (
246+ "Error requesting token. Check if the connection's graphname is correct and that REST authentication is enabled." ,
247+ 404
248+ )
249+ else :
250+ raise e
251+ pass
265252 success = True
266- elif not (success ) and (int (s ) < 3 or (int (s ) == 3 and int (m ) < 5 )):
267- raise TigerGraphException ("Cannot request a token with username/password for versions < 3.5." )
268-
269-
270- if not success and mainVer == 3 :
253+ elif secret :
271254 try :
272255 data = {"secret" : secret }
273256
@@ -276,8 +259,24 @@ def getToken(self, secret: str = None, setToken: bool = True, lifetime: int = No
276259
277260 res = json .loads (requests .post (self .restppUrl + "/requesttoken" ,
278261 data = json .dumps (data ), verify = False ).text )
279- except Exception as e :
280- raise e
262+ success = True
263+ mainVer = 3
264+ except Exception as e : # case of less than version 3.5
265+ try :
266+ res = requests .request ("GET" , self .restppUrl +
267+ "/requesttoken?secret=" + secret +
268+ ("&lifetime=" + str (lifetime ) if lifetime else "" ), verify = False )
269+ mainVer = 3 # Can't use _verGreaterThan4_0 to check version since you need to set a token for that
270+
271+ res = json .loads (res .text )
272+
273+ if not res ["error" ]:
274+ success = True
275+ except :
276+ raise e
277+ else :
278+ raise TigerGraphException ("Cannot request a token with username/password for versions < 3.5." )
279+
281280
282281
283282 if not res .get ("error" ):
@@ -511,4 +510,5 @@ def deleteToken(self, secret, token=None, skipNA=True) -> bool:
511510
512511 return True
513512
513+
514514 raise TigerGraphException (res ["message" ], (res ["code" ] if "code" in res else None ))
0 commit comments