summaryrefslogtreecommitdiffstats
path: root/testapi/testapi-client/testapiclient/utils
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-09 16:35:22 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2018-03-09 18:06:09 +0800
commit1a95c1e6b33d9b3efcfd92e1de64feee7e9b5c68 (patch)
tree61488ece232657c6693425eabc07ad6ca4b3af8e /testapi/testapi-client/testapiclient/utils
parentcc29ae1cd41d1f403511730d5ba44dded967fb12 (diff)
restructure testapiclient project
Change-Id: I13d24ce7b436f203a66fe14f4930e58b3ab1193c Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils')
-rw-r--r--testapi/testapi-client/testapiclient/utils/__init__.py0
-rw-r--r--testapi/testapi-client/testapiclient/utils/command.py39
-rw-r--r--testapi/testapi-client/testapiclient/utils/http_client.py68
-rw-r--r--testapi/testapi-client/testapiclient/utils/identity.py38
-rw-r--r--testapi/testapi-client/testapiclient/utils/url_parse.py22
-rw-r--r--testapi/testapi-client/testapiclient/utils/user.py2
6 files changed, 169 insertions, 0 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/__init__.py b/testapi/testapi-client/testapiclient/utils/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/__init__.py
diff --git a/testapi/testapi-client/testapiclient/utils/command.py b/testapi/testapi-client/testapiclient/utils/command.py
new file mode 100644
index 0000000..95f1fb0
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/command.py
@@ -0,0 +1,39 @@
+from cliff import command
+
+from testapiclient.utils import url_parse
+
+
+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 show(self, request, response):
+ print ' '.join([request,
+ 'success' if response.status_code < 300
+ else 'failed: {}'.format(response.text)])
+
+
+class Lister(command.Command):
+
+ @staticmethod
+ def filter_by_name(url, parsed_args):
+ def query_url():
+ return url_parse.query_join(url, name=parsed_args.name)
+
+ return query_url() if parsed_args.name else url
+
+ def show(self, response):
+ print response.json() if response.status_code < 300 else response.text
+
+
+class ShowOne(command.Command):
+ def show(self, response):
+ print response.json() if response.status_code < 300 else response.text
diff --git a/testapi/testapi-client/testapiclient/utils/http_client.py b/testapi/testapi-client/testapiclient/utils/http_client.py
new file mode 100644
index 0000000..6be33ee
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/http_client.py
@@ -0,0 +1,68 @@
+import json
+
+import requests
+
+from testapiclient.utils import user
+
+
+class HTTPClient(object):
+
+ __instance = None
+ 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 get(self, url):
+ return requests.get(url)
+
+ def post(self, url, data):
+ return self._request('post', url,
+ data=json.dumps(data),
+ headers=self.headers)
+
+ def put(self, url, data):
+ return self._request('put', url,
+ data=json.dumps(data),
+ headers=self.headers)
+
+ def delete(self, url, *args):
+ data = json.dumps(args[0]) if len(args) > 0 else None
+ return self._request('delete', url,
+ data=data,
+ headers=self.headers)
+
+ def _request(self, method, *args, **kwargs):
+ return getattr(user.User.session, method)(*args, **kwargs)
+
+
+def _request(method, *args, **kwargs):
+ client = HTTPClient.get_Instance()
+ return getattr(client, 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
new file mode 100644
index 0000000..2aeb87a
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/identity.py
@@ -0,0 +1,38 @@
+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
+ xstep(self, parsed_args)
+ return wrapper
diff --git a/testapi/testapi-client/testapiclient/utils/url_parse.py b/testapi/testapi-client/testapiclient/utils/url_parse.py
new file mode 100644
index 0000000..08f7a63
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/url_parse.py
@@ -0,0 +1,22 @@
+import os
+
+from six.moves.urllib import parse
+
+
+def path_join(base, *urls):
+ def _path_join(base, url):
+ if not base.endswith('/'):
+ base += '/'
+ return parse.urljoin(base, url)
+
+ urls = (base,) + urls
+ return reduce(_path_join, urls)
+
+
+def query_join(base, **queries):
+ return base + '?' + parse.urlencode(queries)
+
+
+def resource_join(url):
+ testapi_url = os.environ.get('testapi_url')
+ return path_join(testapi_url, url)
diff --git a/testapi/testapi-client/testapiclient/utils/user.py b/testapi/testapi-client/testapiclient/utils/user.py
new file mode 100644
index 0000000..7e72163
--- /dev/null
+++ b/testapi/testapi-client/testapiclient/utils/user.py
@@ -0,0 +1,2 @@
+class User():
+ session = None