diff options
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) |