Skip to content

add no auth api call method. fix api versions bug. #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ pytest = "*"

[scripts]
test = "python -m pytest tests/"
lint = "flake8"
24 changes: 17 additions & 7 deletions evasdk/eva_http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self, host_ip, api_token, custom_logger=None, request_timeout=5, re


def api_call_with_auth(self, *args, **kwargs):

r = self.__api_request(*args, **kwargs)

# Try creating a new session if we get an auth error and retrying the failed request
Expand All @@ -56,20 +57,29 @@ def api_call_with_auth(self, *args, **kwargs):

return r

def api_call_no_auth(self, method, path, payload=None, **kwargs):
return self.__api_request(method, path, payload=payload, add_auth=False, **kwargs)

def __api_request(self, method, path, payload=None, headers={}, timeout=None, version='v1', add_auth=True):
if headers:
headers = headers.copy()

if add_auth:
headers['Authorization'] = f'Bearer {self.session_token}'

def __api_request(self, method, path, payload=None, headers=None, timeout=None):
if not headers:
headers = {'Authorization': 'Bearer {}'.format(self.session_token)}
api_path = path
if version is not None:
api_path = f'{version}/{path}'

return requests.request(
method, 'http://{}/api/v1/{}'.format(self.host_ip, path),
method, 'http://{}/api/{}'.format(self.host_ip, api_path),
data=payload, headers=headers,
timeout=(timeout or self.request_timeout),
)

# API VERSIONS
def api_versions(self):
r = self.__api_request('GET', 'versions')
r = self.api_call_no_auth('GET', 'versions', version=None)
if r.status_code != 200:
eva_error('api_versions request error', r)
return r.json()
Expand All @@ -93,8 +103,7 @@ def auth_renew_session(self):

def auth_create_session(self):
self.__logger.debug('Creating session token')
# Bypass api_call_with_auth to avoid getting in a 401 loop
r = self.__api_request('POST', 'auth', payload=json.dumps({'token': self.api_token}), headers={})
r = self.api_call_no_auth('POST', 'auth', payload=json.dumps({'token': self.api_token}))

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

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

if r.status_code != 204:
Expand Down