Skip to content

Commit

Permalink
Add a basicauth client that doesn't use any magical token or session …
Browse files Browse the repository at this point in the history
…auth.

No-Issue

Signed-off-by: James Tanner <[email protected]>
  • Loading branch information
jctanner committed Aug 21, 2024
1 parent bc3837a commit e55907d
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions galaxykit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
client.py contains the wrapping interface for all the other modules (aside from cli.py)
"""

import json
import logging
import platform
import sys
Expand Down Expand Up @@ -539,3 +540,54 @@ def ui_ee_endpoint_prefix(self):
else:
self._ui_ee_endpoint_prefix = "_ui/v1/"
return self._ui_ee_endpoint_prefix


class BasicAuthClient(GalaxyClient):
'''Simplified basic auth through galaxykit.'''

def __init__(self, galaxy_root, username, password):
self.galaxy_root = galaxy_root
self.username = username
self.password = password
self.auth = (self.username, self.password)

def _payload(self, *args, **kwargs):
return self._http(*args, **kwargs)

def _http(self, method, path, *args, **kwargs):

# relative vs absolute url paths ...
if not path.startswith('/'):
url = urljoin(self.galaxy_root.rstrip("/") + "/", path)
else:
parsed = urlparse(self.galaxy_root)
url = self.galaxy_root.replace(parsed.path, '')
url = urljoin(url.rstrip('/'), path)

kwargs['auth'] = self.auth
kwargs['verify'] = False

# munge body back to json if possible ...
if kwargs.get('body'):
try:
kwargs['json'] = json.loads(kwargs['body'])
kwargs.pop('body')
except Exception:
kwargs['data'] = kwargs['body']
kwargs.pop('body')

# not all endpoints return json
want_json = True
if 'want_json' in kwargs:
want_json = kwargs['want_json']
kwargs.pop('want_json')

func = getattr(requests, method)
rr = func(url, **kwargs)

try:
if want_json:
return rr.json()
return rr.text
except Exception:
raise GalaxyClientError(rr.text, rr.status_code)

0 comments on commit e55907d

Please sign in to comment.