diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-10-16 21:50:32 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-10-16 21:50:32 +0300 |
commit | 750221cf5fa115f15e4e582fd1661e2b4aeed30d (patch) | |
tree | 2973ad6364d69547f48c9bd9b636597d2c74b17f /icktool | |
parent | 947bec6efd7c0753901c08fe4a854ddea96caa5a (diff) | |
download | ick2-750221cf5fa115f15e4e582fd1661e2b4aeed30d.tar.gz |
Fix: show error messages more systematically
Diffstat (limited to 'icktool')
-rwxr-xr-x | icktool | 47 |
1 files changed, 43 insertions, 4 deletions
@@ -88,11 +88,23 @@ class Icktool(cliapp.Application): def cmd_version(self, args): api = self._new_api() - self._prettyson(api.get('/version')) + code, text = api.get('/version') + if code != 200: + sys.stderr.write('HTTP status {}\n'.format(code)) + sys.stderr.write(text) + sys.exit(1) + obj = json.loads(text) + self._prettyson(obj) def cmd_list_projects(self, args): api = self._new_api() - self._prettyson(api.get('/projects')) + code, text = api.get('/version') + if code != 200: + sys.stderr.write('HTTP status {}\n'.format(code)) + sys.stderr.write(text) + sys.exit(1) + obj = json.loads(text) + self._prettyson(obj) def cmd_create_project(self, args): obj = self._read_object() @@ -115,7 +127,22 @@ class Icktool(cliapp.Application): def cmd_show_project(self, args): name = args[0] api = self._new_api() - self._prettyson(api.get('/projects/{}'.format(name))) + code, text = api.get('/projects/{}'.format(name)) + if code != 200: + sys.stderr.write('HTTP status {}\n'.format(code)) + sys.stderr.write(text) + sys.exit(1) + obj = json.loads(text) + self._prettyson(obj) + + def cmd_delete_project(self, args): + name = args[0] + api = self._new_api() + code, text = api.delete('/projects/{}'.format(name)) + if code != 200: + sys.stderr.write('HTTP status {}\n'.format(code)) + sys.stderr.write(text) + sys.exit(1) def _new_token(self): scopes = self.settings['scope'] @@ -169,7 +196,7 @@ class API: 'Authorization': 'Bearer {}'.format(self._token), } r = requests.get(full_url, headers=headers, verify=self._verify) - return r.json() + return r.status_code, r.text def post(self, path, obj): assert self._url is not None @@ -195,6 +222,18 @@ class API: full_url, json=obj, headers=headers, verify=self._verify) return r.status_code, r.text + def delete(self, path): + assert self._url is not None + assert self._token is not None + + full_url = '{}{}'.format(self._url, path) + headers = { + 'Authorization': 'Bearer {}'.format(self._token), + } + r = requests.delete( + full_url, headers=headers, verify=self._verify) + return r.status_code, r.text + class TokenGenerator: |