diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-08-03 10:23:37 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-08-03 10:23:37 +0300 |
commit | 77e2be064ee4601045ca623f6006dbb8a35f058e (patch) | |
tree | adfa41a80be5972968d676975dfb5a6f94313b04 | |
parent | ea6b87df9d5b8a39ad6fc43ba2d595c7fd1b17ee (diff) | |
download | qvisqve-77e2be064ee4601045ca623f6006dbb8a35f058e.tar.gz |
Refactor: reduce repetitiveness
-rwxr-xr-x | qvisqvetool | 52 |
1 files changed, 20 insertions, 32 deletions
diff --git a/qvisqvetool b/qvisqvetool index 1fffab2..90bfd70 100755 --- a/qvisqvetool +++ b/qvisqvetool @@ -37,6 +37,9 @@ import urllib3 import qvisqve +json_content_type = 'application/json' + + class QvisqveTool(cliapp.Application): def add_settings(self): @@ -80,16 +83,11 @@ class QvisqveTool(cliapp.Application): self.output.write('{}\n'.format(scope)) def cmd_get_token(self, args): - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) + api, token = self.get_api() self.output.write('{}\n'.format(token)) def cmd_get_user(self, args): - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) - + api, token = self.get_api() for username in args: path = '/users/{}'.format(username) r = api.GET(token, path) @@ -98,22 +96,19 @@ class QvisqveTool(cliapp.Application): def cmd_create_user(self, args): username, password = args - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) - content_type = 'application/json' + api, token = self.get_api() user = { 'id': username, } path = '/users' - api.POST(token, path, json.dumps(user), content_type) + api.POST(token, path, json.dumps(user), json_content_type) secret = { 'secret': password, } path = '/users/{}/secret'.format(username) - api.PUT(token, path, json.dumps(secret), content_type) + api.PUT(token, path, json.dumps(secret), json_content_type) self.output.write('Created {}\n'.format(username)) @@ -127,26 +122,21 @@ class QvisqveTool(cliapp.Application): } kind = kinds.get(kind, kind) - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) - content_type = 'application/json' + api, token = self.get_api() secret = { 'secret': secret, } path = '/{}/{}/secret'.format(kind, name) - api.PUT(token, path, json.dumps(secret), content_type) + api.PUT(token, path, json.dumps(secret), json_content_type) self.output.write('Set secret for {}\n'.format(name)) def cmd_GET(self, args): - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) + api, token = self.get_api() r = api.GET(token, args[0]) ct = r.headers['Content-Type'] - if ct == 'application/json': + if ct == json_conent_type: json.dump(r.json(), self.output, indent=4) self.output.write('\n') else: @@ -154,9 +144,7 @@ class QvisqveTool(cliapp.Application): def cmd_POST(self, args): path, resource = self.get_path_resource(args) - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) + api, token = self.get_api() content_type = self.settings['content-type'] r = api.POST(token, path, resource, content_type) json.dump(r.json(), self.output, indent=4) @@ -164,18 +152,14 @@ class QvisqveTool(cliapp.Application): def cmd_PUT(self, args): path, resource = self.get_path_resource(args) - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) + api, token = self.get_api() content_type = self.settings['content-type'] r = api.PUT(token, path, resource, content_type) json.dump(r.json(), self.output, indent=4) self.output.write('\n') def cmd_DELETE(self, args): - api = self.get_api() - scopes = self.settings['scopes'] - token = api.get_token(scopes) + api, token = self.get_api() r = api.DELETE(token, args[0]) def get_api(self): @@ -184,7 +168,11 @@ class QvisqveTool(cliapp.Application): if self.settings['token']: api.set_token(self.settings['token']) api.lookup_credentials(self.settings['secrets']) - return api + + scopes = self.settings['scopes'] + token = api.get_token(scopes) + + return api, token def get_path_resource(self, args): logging.debug('get_path_resource: args=%r', args) |