diff options
author | Serena Feng <feng.xiaowei@zte.com.cn> | 2018-03-14 12:59:53 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2018-03-14 12:59:53 +0000 |
commit | bd27d9c8a76887b129ba79117b727f0f0428d106 (patch) | |
tree | 52468a2f3342415b3e92af671bcadaad2abd54a9 /testapi/testapi-client/testapiclient/utils | |
parent | 29b0c572ba4609df208b7e716a42335a32f686d9 (diff) | |
parent | c761a572ae14368ad002a911d07d14c5c5c1b703 (diff) |
Merge "bugfix: TestAPI Cookie cannot be found"
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) | 63 | ||||
-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 |
4 files changed, 23 insertions, 80 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/http_client.py b/testapi/testapi-client/testapiclient/utils/clientmanager.py index 359dd14..7e4e630 100644 --- a/testapi/testapi-client/testapiclient/utils/http_client.py +++ b/testapi/testapi-client/testapiclient/utils/clientmanager.py @@ -1,32 +1,35 @@ import httplib import json +import os +import urllib import requests -from testapiclient.utils import user - -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() + + def auth(self): + 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': self.cli_options.u, + 'pass': self.cli_options.p, + 'form_id': 'user_login' + } + response = self.session.post(hostname, data) + if "login" in response.text: + raise Exception('Authenticate failed') 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 +51,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 +61,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/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 |