diff options
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils')
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/clientmanager.py (renamed from testapi/testapi-client/testapiclient/utils/http_client.py) | 78 | ||||
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/command.py | 11 | ||||
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/identity.py | 38 | ||||
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/urlparse.py (renamed from testapi/testapi-client/testapiclient/utils/url_parse.py) | 0 | ||||
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/user.py | 2 |
5 files changed, 39 insertions, 90 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/http_client.py b/testapi/testapi-client/testapiclient/utils/clientmanager.py index 359dd14..4401231 100644 --- a/testapi/testapi-client/testapiclient/utils/http_client.py +++ b/testapi/testapi-client/testapiclient/utils/clientmanager.py @@ -1,32 +1,52 @@ import httplib import json +import os +import urllib +import logging import requests -from testapiclient.utils import user +LOG = logging.getLogger(__name__) -class HTTPClient(object): - - __instance = None +class ClientManager(object): headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} - @staticmethod - def get_Instance(): - """ Static access method. """ - if HTTPClient.__instance is None: - HTTPClient() - return HTTPClient.__instance - - def __init__(self): - """ Virtually private constructor. """ - if HTTPClient.__instance is not None: - raise Exception("This class is a singleton!") - else: - HTTPClient.__instance = self + def __init__(self, cli_options=None): + self.cli_options = cli_options + self.session = requests.Session() + self._auth_completed = False + + @property + def auth_required(self): + return self._auth() + + def _auth(self): + return { + 'name': self.cli_options.u, + 'pass': self.cli_options.p + } if self.cli_options.u else None + + def auth(self): + + if self._auth_completed: + return + + hostname = '{}{}{}'.format(os.environ.get('testapi_cas_auth_url'), + urllib.quote(os.environ.get('testapi_url')), + os.environ.get('testapi_cas_signin_return')) + data = self._auth() + data.update({'form_id': 'user_login'}) + LOG.debug('authenticating.....') + response = self.session.post(hostname, data) + if "login" in response.text: + raise Exception('Authenticate failed') + self._auth_completed = True def get(self, url): - return self._parse_response('Get', requests.get(url)) + return self._parse_response('Get', + self._request('get', url, + headers=self.headers)) def post(self, url, data): return self._parse_response('Create', @@ -48,7 +68,7 @@ class HTTPClient(object): headers=self.headers)) def _request(self, method, *args, **kwargs): - return getattr(user.User.session, method)(*args, **kwargs) + return getattr(self.session, method)(*args, **kwargs) def _raise_failure(self, op, response): raise Exception('{} failed: {}'.format(op, response.reason)) @@ -58,23 +78,3 @@ class HTTPClient(object): return response.json() if op != 'Delete' else None else: self._raise_failure(op, response) - - -def _request(method, *args, **kwargs): - return getattr(HTTPClient.get_Instance(), method)(*args, **kwargs) - - -def get(url): - return _request('get', url) - - -def post(url, data): - return _request('post', url, data) - - -def put(url, data): - return _request('put', url, data) - - -def delete(url, data=None): - return _request('delete', url, data) diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py index 9614acf..b9d1ce8 100644 --- a/testapi/testapi-client/testapiclient/utils/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -20,17 +20,6 @@ class CommandMeta(abc.ABCMeta): @six.add_metaclass(CommandMeta) class Command(command.Command): - - def get_parser(self, prog_name): - parser = super(Command, self).get_parser(prog_name) - parser.add_argument('-u', - type=str, - help='Username for authentication') - parser.add_argument('-p', - type=str, - help='Password for authentication') - return parser - def run(self, parsed_args): self.log.debug('run(%s)', parsed_args) return super(Command, self).run(parsed_args) diff --git a/testapi/testapi-client/testapiclient/utils/identity.py b/testapi/testapi-client/testapiclient/utils/identity.py deleted file mode 100644 index a00dd87..0000000 --- a/testapi/testapi-client/testapiclient/utils/identity.py +++ /dev/null @@ -1,38 +0,0 @@ -import functools -import os -import urllib - -import requests - -from testapiclient.utils import user - - -def _authenticate(username, password): - session = requests.Session() - hostname = '{}{}{}'.format(os.environ.get('testapi_cas_auth_url'), - urllib.quote(os.environ.get('testapi_url')), - os.environ.get('testapi_cas_signin_return')) - data = { - 'name': username, - 'pass': password, - 'form_id': 'user_login' - } - response = session.post(hostname, data) - if "login" not in response.text: - user.User.session = session - return response - - -def authenticate(xstep): - @functools.wraps(xstep) - def wrapper(self, parsed_args): - if(user.User.session is None): - username = parsed_args.u - password = parsed_args.p - if(username and password): - response = _authenticate(username, password) - if "login" in response.text: - print "Authentication has failed." - return - return xstep(self, parsed_args) - return wrapper diff --git a/testapi/testapi-client/testapiclient/utils/url_parse.py b/testapi/testapi-client/testapiclient/utils/urlparse.py index 9f99a46..9f99a46 100644 --- a/testapi/testapi-client/testapiclient/utils/url_parse.py +++ b/testapi/testapi-client/testapiclient/utils/urlparse.py diff --git a/testapi/testapi-client/testapiclient/utils/user.py b/testapi/testapi-client/testapiclient/utils/user.py deleted file mode 100644 index 7e72163..0000000 --- a/testapi/testapi-client/testapiclient/utils/user.py +++ /dev/null @@ -1,2 +0,0 @@ -class User(): - session = None |