diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2018-03-09 16:35:22 +0800 |
---|---|---|
committer | SerenaFeng <feng.xiaowei@zte.com.cn> | 2018-03-09 18:06:09 +0800 |
commit | 1a95c1e6b33d9b3efcfd92e1de64feee7e9b5c68 (patch) | |
tree | 61488ece232657c6693425eabc07ad6ca4b3af8e /testapi/testapi-client/testapiclient/utils/http_client.py | |
parent | cc29ae1cd41d1f403511730d5ba44dded967fb12 (diff) |
restructure testapiclient project
Change-Id: I13d24ce7b436f203a66fe14f4930e58b3ab1193c
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils/http_client.py')
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/http_client.py | 68 |
1 files changed, 68 insertions, 0 deletions
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) |