diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-10-16 22:10:53 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-10-16 22:10:53 +0300 |
commit | f65510f616325c1b340d8f7f5d2b6d2d6f90acfb (patch) | |
tree | 82d2aa9ca2b6752abdcd963cccb72f09b9f18b29 /icktool | |
parent | 750221cf5fa115f15e4e582fd1661e2b4aeed30d (diff) | |
download | ick2-f65510f616325c1b340d8f7f5d2b6d2d6f90acfb.tar.gz |
Refactor: make it easier to add resource types
Diffstat (limited to 'icktool')
-rwxr-xr-x | icktool | 87 |
1 files changed, 53 insertions, 34 deletions
@@ -97,52 +97,28 @@ class Icktool(cliapp.Application): self._prettyson(obj) def cmd_list_projects(self, args): - api = self._new_api() - 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) + rc = self._new_rc('/projects', 'project') + self._prettyson(rc.list()) def cmd_create_project(self, args): + rc = self._new_rc('/projects', 'project') obj = self._read_object() - api = self._new_api() - code, text = api.post('/projects', obj) - if code != 201: - sys.stderr.write('HTTP status {}\n'.format(code)) - sys.stderr.write(text) - sys.exit(1) + rc.create(obj) def cmd_update_project(self, args): + rc = self._new_rc('/projects', 'project') obj = self._read_object() - api = self._new_api() - code, text = api.put('/projects/{}'.format(obj['project']), obj) - if code != 200: - sys.stderr.write('HTTP status {}\n'.format(code)) - sys.stderr.write(text) - sys.exit(1) + rc.update(obj) def cmd_show_project(self, args): + rc = self._new_rc('/projects', 'project') name = args[0] - api = self._new_api() - 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) + self._prettyson(rc.show(name)) def cmd_delete_project(self, args): + rc = self._new_rc('/projects', 'project') 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) + rc.delete(name) def _new_token(self): scopes = self.settings['scope'] @@ -163,6 +139,10 @@ class Icktool(cliapp.Application): api.set_verify(self.settings['verify-tls']) return api + def _new_rc(self, path, field_name): + api = self._new_api() + return ResourceCommands(path, api, field_name) + def _prettyson(self, obj): json.dump(obj, sys.stdout, indent=4) sys.stdout.write('\n') @@ -235,6 +215,45 @@ class API: return r.status_code, r.text +class ResourceCommands: + + def __init__(self, path, api, name_field): + self._path = path + self._api = api + self._name = name_field + + def list(self): + code, text = self._api.get(self._path) + self._report(code, 200, text) + return json.loads(text) + + def create(self, obj): + code, text = self._api.post(self._path, obj) + self._report(code, 201, text) + + def update(self, obj): + code, text = self._api.put(self._id_path(obj[self._name]), obj) + self._report(code, 200, text) + + def show(self, name): + code, text = self._api.get(self._id_path(name)) + self._report(code, 200, text) + return json.loads(text) + + def delete(self, name): + code, text = self._api.delete(self._id_path(name)) + self._report(code, 200, text) + + def _id_path(self, name): + return '{}/{}'.format(self._path, name) + + def _report(self, code, expected, text): + if code != expected: + sys.stderr.write('HTTP status {}\n'.format(code)) + sys.stderr.write(text) + sys.exit(1) + + class TokenGenerator: def __init__(self): |