@@ -39,6 +39,7 @@ def __init__(self, host_ip, api_token, custom_logger=None, request_timeout=5, re
39
39
40
40
41
41
def api_call_with_auth (self , * args , ** kwargs ):
42
+
42
43
r = self .__api_request (* args , ** kwargs )
43
44
44
45
# Try creating a new session if we get an auth error and retrying the failed request
@@ -56,20 +57,29 @@ def api_call_with_auth(self, *args, **kwargs):
56
57
57
58
return r
58
59
60
+ def api_call_no_auth (self , method , path , payload = None , ** kwargs ):
61
+ return self .__api_request (method , path , payload = payload , add_auth = False , ** kwargs )
62
+
63
+ def __api_request (self , method , path , payload = None , headers = {}, timeout = None , version = 'v1' , add_auth = True ):
64
+ if headers :
65
+ headers = headers .copy ()
66
+
67
+ if add_auth :
68
+ headers ['Authorization' ] = f'Bearer { self .session_token } '
59
69
60
- def __api_request ( self , method , path , payload = None , headers = None , timeout = None ):
61
- if not headers :
62
- headers = { 'Authorization' : 'Bearer {}' . format ( self . session_token )}
70
+ api_path = path
71
+ if version is not None :
72
+ api_path = f' { version } / { path } '
63
73
64
74
return requests .request (
65
- method , 'http://{}/api/v1/ {}' .format (self .host_ip , path ),
75
+ method , 'http://{}/api/{}' .format (self .host_ip , api_path ),
66
76
data = payload , headers = headers ,
67
77
timeout = (timeout or self .request_timeout ),
68
78
)
69
79
70
80
# API VERSIONS
71
81
def api_versions (self ):
72
- r = self .__api_request ('GET' , 'versions' )
82
+ r = self .api_call_no_auth ('GET' , 'versions' , version = None )
73
83
if r .status_code != 200 :
74
84
eva_error ('api_versions request error' , r )
75
85
return r .json ()
@@ -93,8 +103,7 @@ def auth_renew_session(self):
93
103
94
104
def auth_create_session (self ):
95
105
self .__logger .debug ('Creating session token' )
96
- # Bypass api_call_with_auth to avoid getting in a 401 loop
97
- r = self .__api_request ('POST' , 'auth' , payload = json .dumps ({'token' : self .api_token }), headers = {})
106
+ r = self .api_call_no_auth ('POST' , 'auth' , payload = json .dumps ({'token' : self .api_token }))
98
107
99
108
if r .status_code != 200 :
100
109
eva_error ('auth_create_session request error' , r )
@@ -107,6 +116,7 @@ def auth_create_session(self):
107
116
108
117
def auth_invalidate_session (self ):
109
118
self .__logger .debug ('Invalidating session token {}' .format (self .session_token ))
119
+ # Bypass api_call_with_auth to avoid getting in a 401 loop
110
120
r = self .__api_request ('DELETE' , 'auth' )
111
121
112
122
if r .status_code != 204 :
0 commit comments