From cc29ae1cd41d1f403511730d5ba44dded967fb12 Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Fri, 9 Mar 2018 15:53:12 +0800 Subject: unify message prompt Change-Id: I31a10c0aa92a72fdbd76475e6f618f63fef9b925 Signed-off-by: SerenaFeng --- testapi/testapi-client/testapiclient/command.py | 11 +++++- .../testapi-client/testapiclient/http_client.py | 45 +++++++++------------- testapi/testapi-client/testapiclient/pods.py | 19 ++++----- testapi/testapi-client/testapiclient/projects.py | 25 +++++------- 4 files changed, 45 insertions(+), 55 deletions(-) (limited to 'testapi/testapi-client/testapiclient') diff --git a/testapi/testapi-client/testapiclient/command.py b/testapi/testapi-client/testapiclient/command.py index 2864f9a..62a8b86 100644 --- a/testapi/testapi-client/testapiclient/command.py +++ b/testapi/testapi-client/testapiclient/command.py @@ -14,6 +14,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.text)]) + class Lister(command.Command): @@ -24,6 +29,10 @@ 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 response.text + class ShowOne(command.Command): - pass + def show(self, response): + print response.json() if response.status_code < 300 else response.text diff --git a/testapi/testapi-client/testapiclient/http_client.py b/testapi/testapi-client/testapiclient/http_client.py index aa6ef11..8a33a8c 100644 --- a/testapi/testapi-client/testapiclient/http_client.py +++ b/testapi/testapi-client/testapiclient/http_client.py @@ -24,51 +24,44 @@ class HTTPClient(object): 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) + return requests.get(url) def post(self, url, data): - return self._session_request('post', url, - data=json.dumps(data), - headers=HTTPClient.headers) + return self._request('post', url, + data=json.dumps(data), + headers=self.headers) def put(self, url, data): - return self._session_request('put', url, - data=json.dumps(data), - headers=HTTPClient.headers).text + return self._request('put', url, + data=json.dumps(data), + headers=self.headers) 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 + 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 http_request(method, *args, **kwargs): +def _request(method, *args, **kwargs): client = HTTPClient.get_Instance() return getattr(client, method)(*args, **kwargs) def get(url): - return http_request('get', url) + return _request('get', url) def post(url, data): - return http_request('post', url, data) + return _request('post', url, data) def put(url, data): - return http_request('put', url, data) + return _request('put', url, data) def delete(url, data=None): - return http_request('delete', url, data) + return _request('delete', url, data) diff --git a/testapi/testapi-client/testapiclient/pods.py b/testapi/testapi-client/testapiclient/pods.py index bad0c31..85c19e4 100644 --- a/testapi/testapi-client/testapiclient/pods.py +++ b/testapi/testapi-client/testapiclient/pods.py @@ -1,7 +1,7 @@ import json from testapiclient import command -from testapiclient import http_client +from testapiclient import http_client as client from testapiclient import identity from testapiclient import url_parse @@ -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): @@ -41,8 +39,7 @@ class PodGetOne(command.ShowOne): 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): @@ -61,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): @@ -80,4 +74,5 @@ class PodDelete(command.Command): @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/projects.py index ad42293..7f98b56 100644 --- a/testapi/testapi-client/testapiclient/projects.py +++ b/testapi/testapi-client/testapiclient/projects.py @@ -1,7 +1,7 @@ import json from testapiclient import command -from testapiclient import http_client +from testapiclient import http_client as client from testapiclient import identity from testapiclient import url_parse @@ -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)) -- cgit 1.2.3-korg