From 50303ed3d17797c918d2743220be80af1445344f Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Wed, 7 Mar 2018 17:06:22 +0800 Subject: simply http client process Change-Id: I85b87be7d57b7c3ecb087b27729bc86934283366 Signed-off-by: SerenaFeng --- testapi/testapi-client/testapiclient/httpClient.py | 51 --------------- .../testapi-client/testapiclient/http_client.py | 72 ++++++++++++++++++++++ testapi/testapi-client/testapiclient/pods.py | 6 +- testapi/testapi-client/testapiclient/projects.py | 7 +-- 4 files changed, 74 insertions(+), 62 deletions(-) delete mode 100644 testapi/testapi-client/testapiclient/httpClient.py create mode 100644 testapi/testapi-client/testapiclient/http_client.py (limited to 'testapi') diff --git a/testapi/testapi-client/testapiclient/httpClient.py b/testapi/testapi-client/testapiclient/httpClient.py deleted file mode 100644 index f8683b9..0000000 --- a/testapi/testapi-client/testapiclient/httpClient.py +++ /dev/null @@ -1,51 +0,0 @@ -import json - -import requests - - -class HTTPClient(): - - __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 post(self, url, session, data): - r = session.post(url, - data=json.dumps(data), - headers=HTTPClient.headers) - return r - - def put(self, url, session, data): - r = session.put(url, - data=json.dumps(data), - headers=HTTPClient.headers) - return r.text - - def delete(self, url, session, *args): - if(args.__len__ > 0): - r = session.delete(url, - data=json.dumps(args[0]), - headers=HTTPClient.headers) - else: - r = session.delete(url) - return r.text diff --git a/testapi/testapi-client/testapiclient/http_client.py b/testapi/testapi-client/testapiclient/http_client.py new file mode 100644 index 0000000..a2bd65d --- /dev/null +++ b/testapi/testapi-client/testapiclient/http_client.py @@ -0,0 +1,72 @@ +import json + +import requests + + +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 post(self, url, session, data): + r = session.post(url, + data=json.dumps(data), + headers=HTTPClient.headers) + return r + + def put(self, url, session, data): + r = session.put(url, + data=json.dumps(data), + headers=HTTPClient.headers) + return r.text + + def delete(self, url, session, *args): + if(args.__len__ > 0): + r = session.delete(url, + data=json.dumps(args[0]), + headers=HTTPClient.headers) + else: + r = session.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, session, data): + return http_request('post', url, session, data) + + +def put(url, session, data): + return http_request('put', url, session, data) + + +def delete(url, session, data): + return http_request('delete', url, session, data) diff --git a/testapi/testapi-client/testapiclient/pods.py b/testapi/testapi-client/testapiclient/pods.py index 64bb74d..0864c22 100644 --- a/testapi/testapi-client/testapiclient/pods.py +++ b/testapi/testapi-client/testapiclient/pods.py @@ -2,7 +2,7 @@ import json from testapiclient import command from testapiclient import config -from testapiclient import httpClient +from testapiclient import http_client from testapiclient import identity from testapiclient import user @@ -20,7 +20,6 @@ class PodGet(command.Lister): return parser def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() url = PODS_URL if(parsed_args.name): url = PODS_URL + "?name=" + parsed_args.name @@ -40,7 +39,6 @@ class PodGetOne(command.ShowOne): return parser def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() pods = http_client.get(PODS_URL + "/" + parsed_args.name) print pods @@ -61,7 +59,6 @@ class PodCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() response = http_client.post(PODS_URL, user.User.session, parsed_args.pod) @@ -84,6 +81,5 @@ class PodDelete(command.Command): @identity.authenticate def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() print http_client.delete(PODS_URL + "/" + parsed_args.name, user.User.session) diff --git a/testapi/testapi-client/testapiclient/projects.py b/testapi/testapi-client/testapiclient/projects.py index cd55e74..ee2e884 100644 --- a/testapi/testapi-client/testapiclient/projects.py +++ b/testapi/testapi-client/testapiclient/projects.py @@ -2,7 +2,7 @@ import json from testapiclient import command from testapiclient import config -from testapiclient import httpClient +from testapiclient import http_client from testapiclient import identity from testapiclient import user @@ -19,7 +19,6 @@ class ProjectGet(command.Lister): return parser def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() url = PROJECTS_URL if parsed_args.name: url = url + "?name=" + parsed_args.name @@ -38,7 +37,6 @@ class ProjectGetOne(command.ShowOne): return parser def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() url = PROJECTS_URL + "/" + parsed_args.name project = http_client.get(url) print project @@ -57,7 +55,6 @@ class ProjectCreate(command.Command): @identity.authenticate def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() response = http_client.post(ProjectCreate.projects_url, user.User.session, parsed_args.project) @@ -79,7 +76,6 @@ class ProjectDelete(command.Command): @identity.authenticate def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() projects = http_client.delete( PROJECTS_URL + "/" + parsed_args.name, user.User.session) @@ -103,7 +99,6 @@ class ProjectPut(command.Command): @identity.authenticate def take_action(self, parsed_args): - http_client = httpClient.HTTPClient.get_Instance() projects = http_client.put( PROJECTS_URL + "/" + parsed_args.name, user.User.session, -- cgit 1.2.3-korg