diff options
14 files changed, 140 insertions, 170 deletions
diff --git a/testapi/testapi-client/__init__.py b/testapi/testapi-client/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/testapi/testapi-client/__init__.py diff --git a/testapi/testapi-client/setup.cfg b/testapi/testapi-client/setup.cfg index 672c9ae..72a5a57 100644 --- a/testapi/testapi-client/setup.cfg +++ b/testapi/testapi-client/setup.cfg @@ -14,43 +14,18 @@ console_scripts = testapi = testapiclient.main:main testapi = - auth = testapiclient.auth:Auth - pod create = testapiclient.pods:PodCreate - pod get = testapiclient.pods:PodGet - pod delete = testapiclient.pods:PodDelete - pod getone = testapiclient.pods:PodGetOne + auth = testapiclient.cli.auth:Auth + pod create = testapiclient.cli.pods:PodCreate + pod get = testapiclient.cli.pods:PodGet + pod delete = testapiclient.cli.pods:PodDelete + pod getone = testapiclient.cli.pods:PodGetOne + + project create = testapiclient.cli.projects:ProjectCreate + project get = testapiclient.cli.projects:ProjectGet + project getone = testapiclient.cli.projects:ProjectGetOne + project delete = testapiclient.cli.projects:ProjectDelete + project put = testapiclient.cli.projects:ProjectPut - project create = testapiclient.projects:ProjectCreate - project get = testapiclient.projects:ProjectGet - project getone = testapiclient.projects:ProjectGetOne - project delete = testapiclient.projects:ProjectDelete - project put = testapiclient.projects:ProjectPut - - testcase create = testapiclient.testcase:TestCaseCreate - testcase get = testapiclient.testcase:TestCaseGet - testcase delete = testapiclient.testcase:TestCaseDelete - testcase put = testapiclient.testcase:TestCasePut - - scenario create = testapiclient.scenario:ScenarioCreate - scenario get = testapiclient.scenario:ScenarioGet - scenario delete = testapiclient.scenario:ScenarioDelete - scenario put = testapiclient.scenario:ScenarioPut - - scenario addscore = testapiclient.scenario:ScenarioAddScore - - scenario addyi = testapiclient.scenario:ScenarioAddTI - - scenario addcustom = testapiclient.scenario:ScenarioAddCustom - scenario updatecustom = testapiclient.scenario:ScenarioUpdateCustom - scenario deletecustom = testapiclient.scenario:ScenarioDeleteCustom - - scenario addproject = testapiclient.scenario:ScenarioAddProject - scenario deleteproject = testapiclient.scenario:ScenarioDeleteProject - - scenario addversion = testapiclient.scenario:ScenarioAddVersion - scenario deleteversion = testapiclient.scenario:ScenarioDeleteVersion - - result get = testapiclient.results:ResultGet [egg_info] tag_build = tag_date = 0 diff --git a/testapi/testapi-client/testapiclient/cli/__init__.py b/testapi/testapi-client/testapiclient/cli/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/testapi/testapi-client/testapiclient/cli/__init__.py diff --git a/testapi/testapi-client/testapiclient/auth.py b/testapi/testapi-client/testapiclient/cli/auth.py index 3728498..434c55a 100644 --- a/testapi/testapi-client/testapiclient/auth.py +++ b/testapi/testapi-client/testapiclient/cli/auth.py @@ -1,5 +1,5 @@ -from testapiclient import command -from testapiclient import identity +from testapiclient.utils import command +from testapiclient.utils import identity class Auth(command.Command): diff --git a/testapi/testapi-client/testapiclient/pods.py b/testapi/testapi-client/testapiclient/cli/pods.py index c49f254..cdedc3e 100644 --- a/testapi/testapi-client/testapiclient/pods.py +++ b/testapi/testapi-client/testapiclient/cli/pods.py @@ -1,9 +1,9 @@ import json -from testapiclient import command -from testapiclient import http_client -from testapiclient import identity -from testapiclient import url_parse +from testapiclient.utils import command +from testapiclient.utils import http_client as client +from testapiclient.utils import identity +from testapiclient.utils import url_parse def pods_url(): @@ -25,9 +25,7 @@ class PodGet(command.Lister): return parser def take_action(self, parsed_args): - pods = http_client.get(self.filter_by_name(pods_url(), - parsed_args)) - print pods + self.show(client.get(self.filter_by_name(pods_url(), parsed_args))) class PodGetOne(command.ShowOne): @@ -35,15 +33,13 @@ class PodGetOne(command.ShowOne): def get_parser(self, prog_name): parser = super(PodGetOne, self).get_parser(prog_name) - parser.add_argument('-name', + parser.add_argument('name', default='', - help='Find pod using name', - required=True) + help='Find pod using name') return parser def take_action(self, parsed_args): - pods = http_client.get(pod_url(parsed_args)) - print pods + self.show(client.get(pod_url(parsed_args))) class PodCreate(command.Command): @@ -62,11 +58,8 @@ class PodCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - response = http_client.post(pods_url(), parsed_args.pod) - if response.status_code == 200: - print "Pod has been successfully created!" - else: - print response.text + self.show('Create', + client.post(pods_url(), parsed_args.pod)) class PodDelete(command.Command): @@ -74,12 +67,12 @@ class PodDelete(command.Command): def get_parser(self, prog_name): parser = super(PodDelete, self).get_parser(prog_name) - parser.add_argument('-name', + parser.add_argument('name', type=str, - required=True, help='Delete pods using name') return parser @identity.authenticate def take_action(self, parsed_args): - print http_client.delete(pod_url(parsed_args)) + self.show('Delete', + client.delete(pod_url(parsed_args))) diff --git a/testapi/testapi-client/testapiclient/projects.py b/testapi/testapi-client/testapiclient/cli/projects.py index ad42293..113b030 100644 --- a/testapi/testapi-client/testapiclient/projects.py +++ b/testapi/testapi-client/testapiclient/cli/projects.py @@ -1,13 +1,13 @@ import json -from testapiclient import command -from testapiclient import http_client -from testapiclient import identity -from testapiclient import url_parse +from testapiclient.utils import command +from testapiclient.utils import http_client as client +from testapiclient.utils import identity +from testapiclient.utils import url_parse def projects_url(): - url_parse.resource_join('projects') + return url_parse.resource_join('projects') def project_url(parsed_args): @@ -24,9 +24,7 @@ class ProjectGet(command.Lister): return parser def take_action(self, parsed_args): - projects = http_client.get(self.filter_name(projects_url(), - parsed_args)) - print projects + self.show(client.get(self.filter_name(projects_url(), parsed_args))) class ProjectGetOne(command.ShowOne): @@ -40,8 +38,7 @@ class ProjectGetOne(command.ShowOne): return parser def take_action(self, parsed_args): - project = http_client.get(project_url(parsed_args)) - print project + self.show(client.get(project_url(parsed_args))) class ProjectCreate(command.Command): @@ -57,11 +54,8 @@ class ProjectCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - response = http_client.post(projects_url(), parsed_args.project) - if response.status_code == 200: - print "Project has been successfully created!" - else: - print response.text + self.show('Create', + client.post(projects_url(), parsed_args.project)) class ProjectDelete(command.Command): @@ -76,8 +70,8 @@ class ProjectDelete(command.Command): @identity.authenticate def take_action(self, parsed_args): - projects = http_client.delete(project_url(parsed_args)) - print projects + self.show('Delete', + client.delete(project_url(parsed_args))) class ProjectPut(command.Command): @@ -97,6 +91,5 @@ class ProjectPut(command.Command): @identity.authenticate def take_action(self, parsed_args): - projects = http_client.put(project_url(parsed_args), - parsed_args.project) - print projects + self.show('Update', + client.put(project_url(parsed_args), parsed_args.project)) diff --git a/testapi/testapi-client/testapiclient/http_client.py b/testapi/testapi-client/testapiclient/http_client.py deleted file mode 100644 index 59b4f8e..0000000 --- a/testapi/testapi-client/testapiclient/http_client.py +++ /dev/null @@ -1,74 +0,0 @@ -import json - -import requests -from testapiclient 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): - r = requests.get(url) - if r.status_code == 200: - return r.json() - else: - return r.text - - def _session_request(self, method, *args, **kwargs): - return getattr(user.User.session, method)(*args, **kwargs) - - def post(self, url, data): - return self._session_request('post', url, - data=json.dumps(data), - headers=HTTPClient.headers) - - def put(self, url, data): - return self._session_request('put', url, - data=json.dumps(data), - headers=HTTPClient.headers).text - - def delete(self, url, *args): - if(args.__len__ > 0): - r = self._session_request('delete', url, - data=json.dumps(args[0]), - headers=HTTPClient.headers) - else: - r = self._session_request('delete', url) - return r.text - - -def http_request(method, *args, **kwargs): - client = HTTPClient.get_Instance() - return getattr(client, method)(*args, **kwargs) - - -def get(url): - return http_request('get', url) - - -def post(url, data): - return http_request('post', url, data) - - -def put(url, data): - return http_request('put', url, data) - - -def delete(url, data=None): - return http_request('delete', url, data) diff --git a/testapi/testapi-client/testapiclient/main.py b/testapi/testapi-client/testapiclient/main.py index 6c6cf56..dfa6284 100644 --- a/testapi/testapi-client/testapiclient/main.py +++ b/testapi/testapi-client/testapiclient/main.py @@ -1,21 +1,21 @@ import sys -from cliff.app import App -from cliff.commandmanager import CommandManager +from cliff import app +from cliff import commandmanager import requests -from testapiclient import user +from testapiclient.utils import user -class TestAPIClient(App): +class TestAPIClient(app.App): def __init__(self): super(TestAPIClient, self).__init__( description='TestAPI Client', version='0.1', - command_manager=CommandManager('testapi'), + command_manager=commandmanager.CommandManager('testapi'), deferred_help=True, - ) + ) user.User.session = requests.Session() def initialize_app(self, argv): 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/command.py b/testapi/testapi-client/testapiclient/utils/command.py index 2864f9a..f9c75a7 100644 --- a/testapi/testapi-client/testapiclient/command.py +++ b/testapi/testapi-client/testapiclient/utils/command.py @@ -1,5 +1,6 @@ from cliff import command -from testapiclient import url_parse + +from testapiclient.utils import url_parse class Command(command.Command): @@ -14,6 +15,11 @@ class Command(command.Command): return parser + def show(self, request, response): + print ' '.join([request, + 'success' if response.status_code < 300 + else 'failed: {}'.format(response.reason)]) + class Lister(command.Command): @@ -24,6 +30,12 @@ class Lister(command.Command): return query_url() if parsed_args.name else url + def show(self, response): + print response.json() if response.status_code < 300 \ + else 'Get failed: {}'.format(response.reason) + class ShowOne(command.Command): - pass + def show(self, response): + print response.json() if response.status_code < 300 \ + else 'Get failed: {}'.format(response.reason) 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/identity.py b/testapi/testapi-client/testapiclient/utils/identity.py index 03b3ac5..2aeb87a 100644 --- a/testapi/testapi-client/testapiclient/identity.py +++ b/testapi/testapi-client/testapiclient/utils/identity.py @@ -3,7 +3,8 @@ import os import urllib import requests -from testapiclient import user + +from testapiclient.utils import user def _authenticate(username, password): @@ -17,19 +18,21 @@ def _authenticate(username, password): 'form_id': 'user_login' } response = session.post(hostname, data) - user.User.session = session + if "login" not in response.text: + user.User.session = session return response def authenticate(xstep): @functools.wraps(xstep) def wrapper(self, parsed_args): - 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." - else: - xstep(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/url_parse.py b/testapi/testapi-client/testapiclient/utils/url_parse.py index 08f7a63..08f7a63 100644 --- a/testapi/testapi-client/testapiclient/url_parse.py +++ b/testapi/testapi-client/testapiclient/utils/url_parse.py diff --git a/testapi/testapi-client/testapiclient/user.py b/testapi/testapi-client/testapiclient/utils/user.py index 7e72163..7e72163 100644 --- a/testapi/testapi-client/testapiclient/user.py +++ b/testapi/testapi-client/testapiclient/utils/user.py |