diff options
Diffstat (limited to 'testapi/testapi-client/testapiclient/utils/clientmanager.py')
-rw-r--r-- | testapi/testapi-client/testapiclient/utils/clientmanager.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/testapi/testapi-client/testapiclient/utils/clientmanager.py b/testapi/testapi-client/testapiclient/utils/clientmanager.py new file mode 100644 index 0000000..7e4e630 --- /dev/null +++ b/testapi/testapi-client/testapiclient/utils/clientmanager.py @@ -0,0 +1,63 @@ +import httplib +import json +import os +import urllib + +import requests + + +class ClientManager(object): + headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} + + def __init__(self, cli_options=None): + self.cli_options = cli_options + self.session = requests.Session() + + def auth(self): + hostname = '{}{}{}'.format(os.environ.get('testapi_cas_auth_url'), + urllib.quote(os.environ.get('testapi_url')), + os.environ.get('testapi_cas_signin_return')) + data = { + 'name': self.cli_options.u, + 'pass': self.cli_options.p, + 'form_id': 'user_login' + } + response = self.session.post(hostname, data) + if "login" in response.text: + raise Exception('Authenticate failed') + + def get(self, url): + return self._parse_response('Get', + self._request('get', url, + headers=self.headers)) + + def post(self, url, data): + return self._parse_response('Create', + self._request('post', url, + data=json.dumps(data), + headers=self.headers)) + + def put(self, url, data): + return self._parse_response('Update', + 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._parse_response('Delete', + self._request('delete', url, + data=data, + headers=self.headers)) + + def _request(self, method, *args, **kwargs): + return getattr(self.session, method)(*args, **kwargs) + + def _raise_failure(self, op, response): + raise Exception('{} failed: {}'.format(op, response.reason)) + + def _parse_response(self, op, response): + if response.status_code == httplib.OK: + return response.json() if op != 'Delete' else None + else: + self._raise_failure(op, response) |