Skip to content

Commit e622663

Browse files
IsaacHookIsaac Hook
and
Isaac Hook
authored
add no auth api call method. fix api versions bug. (#58)
* add no auth api call method. fix api versions bug. * updated PR based on feedback * more feedback more changes Co-authored-by: Isaac Hook <ih@automata.tech>
1 parent b3fd0c1 commit e622663

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ pytest = "*"
1616

1717
[scripts]
1818
test = "python -m pytest tests/"
19+
lint = "flake8"

evasdk/eva_http_client.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(self, host_ip, api_token, custom_logger=None, request_timeout=5, re
3939

4040

4141
def api_call_with_auth(self, *args, **kwargs):
42+
4243
r = self.__api_request(*args, **kwargs)
4344

4445
# 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):
5657

5758
return r
5859

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}'
5969

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}'
6373

6474
return requests.request(
65-
method, 'http://{}/api/v1/{}'.format(self.host_ip, path),
75+
method, 'http://{}/api/{}'.format(self.host_ip, api_path),
6676
data=payload, headers=headers,
6777
timeout=(timeout or self.request_timeout),
6878
)
6979

7080
# API VERSIONS
7181
def api_versions(self):
72-
r = self.__api_request('GET', 'versions')
82+
r = self.api_call_no_auth('GET', 'versions', version=None)
7383
if r.status_code != 200:
7484
eva_error('api_versions request error', r)
7585
return r.json()
@@ -93,8 +103,7 @@ def auth_renew_session(self):
93103

94104
def auth_create_session(self):
95105
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}))
98107

99108
if r.status_code != 200:
100109
eva_error('auth_create_session request error', r)
@@ -107,6 +116,7 @@ def auth_create_session(self):
107116

108117
def auth_invalidate_session(self):
109118
self.__logger.debug('Invalidating session token {}'.format(self.session_token))
119+
# Bypass api_call_with_auth to avoid getting in a 401 loop
110120
r = self.__api_request('DELETE', 'auth')
111121

112122
if r.status_code != 204:

0 commit comments

Comments
 (0)